0

大家好,我的代码需要帮助。我有一个表格,学生可以选择他们获得的科目、分数和成绩。科目和成绩是一个下拉菜单,它们处于循环中。我希望学生至少输入五门科目,包括英语。每个主题都由一个主题代码引用。你能帮我做这件事吗?

我的控制器功能是

subject_code' => $this->data['ApplicantOlevelQualification']['subject_code'][$i],
'grade' =>  $this->data['ApplicantOlevelQualification']['grade'][$i],

我的模型如下

public $validate = array(
'grade' => array(
        'notempty' => array(
        'rule' => array('notempty'),
        // extra keys like on, required, etc. go here...
    ),
    'ruleName2' => array(
        'rule' => array('inList', array('A', 'B','C','D','E')),
        'message' => 'Not in range.',
    ),
),
'subject_code' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
4

1 回答 1

0

您需要创建自定义验证。

看看这里: http ://book.cakephp.org/1.3/view/1179/Custom-Validation-Rules

一个例子:

<?php
class User extends AppModel {
    var $name = 'User';

    var $validate = array(
        'promotion_code' => array(
            'rule' => array('limitDuplicates', 25),
            'message' => 'This code has been used too many times.'
        )
    );

    function limitDuplicates($check, $limit){
        //$check will have value: array('promomotion_code' => 'some-value')
        //$limit will have value: 25
        $existing_promo_count = $this->find( 'count', array('conditions' => $check, 'recursive' => -1) );
        return $existing_promo_count < $limit;
    }
}
?>
于 2012-07-02T05:15:11.747 回答