CakePHP中的hasMany + HABTM表单验证

时间:2015-08-20 12:59:31

标签: php forms validation cakephp

我有一个与hasMany关系中的学生表链接的帖子表。
学生表与hasAndBelongsToMany关系中的主题表链接:

posts hasMany students
students HABTM subjects

PostController提交了一个表单。表格有几个字段(多个复选框)供学生分别选择他们的科目:

<?php echo $this->Form->input('Student.0.Subject'); ?>
<?php echo $this->Form->input('Student.1.Subject'); ?>
<?php echo $this->Form->input('Student.2.Subject'); ?>

我需要确保每个学生至少选择一个科目,但蛋糕验证不会像它应该的那样工作。我尝试根据我的需要调整this approach,但它似乎不起作用。不知道我做错了什么?

function beforeValidate($options = array()) {
    $test = Router::getParams();
    if ($test['pass'][0] == '3') {
    //check the field only if it's in step 3
        if (empty($this->data['Student'][0]['Subject'])) {
            $this->invalidate('non_existent_field'); // fake validation error
            $this->invalidate('Student.0.Subject', 'Please select at least one subject');
        }
        return true;
    }
}

fyi $this->request->data['Student'][0]['Subject']确实返回空''字符串..

编辑:

$ this-&gt; invalidate似乎不起作用。我调试它并返回null

0 个答案:

没有答案
相关问题