0

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

我的控制器功能如下

public function add($app_id = null) {
        if($app_id != null){

            if ($this->request->is('post')) {

        //saving the data in variables
                $applicant_id = $this->data    ['ApplicantOlevelQualification']['applicants_detail_id'];
            $centre_number = $this->data['ApplicantOlevelQualification']['centre_number'];
            $candidate_number = $this->data['ApplicantOlevelQualification']['candidate_number'];
            $exam_body_code = $this->data['ApplicantOlevelQualification']['exam_body_code'];
            $year_written = $this->data['ApplicantOlevelQualification']['year_written'];
        $sittings=$this->request->data['ApplicantOlevelQualification']['number_of_sittings'];
    //  debug($sittings);die();

        for ($i = 1; $i < sizeof($this->data['ApplicantOlevelQualification']['olevel_subject_code']); $i++) {

                if ($this->data['ApplicantOlevelQualification']['olevel_subject_code'][$i] != "") {
                    $this->ApplicantOlevelQualification->create();
                    $this->ApplicantOlevelQualification->id = null;
                    $this->ApplicantOlevelQualification->set(array(
                            'applicants_detail_id' => $app_id,
                            'olevel_subject_code' => $this->data['ApplicantOlevelQualification']['olevel_subject_code'][$i],
                            'grade' => $this->data['ApplicantOlevelQualification']['grade'][$i],
                            'centre_number'=> $centre_number, 
                            'candidate_number'=> $candidate_number, 
                            'exam_body_code'=> $exam_body_code,
                            'year_written'=> $year_written,

                        )
                    );

                    //$appeng = $this->ApplicantOlevelQualification->find('list',array('fields'=> array('olevel_subject_code','grade'), 'conditions'=>array('ApplicantOlevelQualification.olevel_subject_code'=>'8900',)));
                 //debug(($this->data->ApplicantOlevelQualification);die();
                 /*    if($appeng !="8900"){
                   $this->Session->setFlash(__('English is a prerequisite for application to procceed'));
                   $this->redirect(array('action' => 'add',$app_id));

                     }  */                   

                    if ($this->ApplicantOlevelQualification->save()) {      
                        $this->Session->setFlash(__('Your O\'level Qualifications have been saved'));
                    }   else {
                        $this->Session->setFlash(__('Your O\'level Qualifications failed to save.'));
                    }
                }
            }

我的模型如下

public $validate = array(
'grade' => array(
        'notempty ddd' => array(
            'rule' => array('notempty'),
            //'rule' => '/^[A-Z]{1}$/i',
            'message' => 'Enter a valid grade, in capital letters!',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
'olevel_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
        ),
    ),
);

目前,此代码捕获科目和成绩以及详细信息,而不检查学生是否输入了所需的最少 5 门科目及其成绩。如果可以,请你帮助我。谢谢你。

4

1 回答 1

0

您应该编写自己的自定义验证规则。为此,请将其添加到$validate

public $validate = array(
'grade' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Enter a valid grade, in capital letters!',
        ),
        'mycustomrule' => array(
            'rule' => array('mycustomgraderule'),
            'message' => 'One or more of your subjects do not have grades'
        )
    ),
'olevel_subject_code' => array(
        'numeric' => array(
            'rule' => array('numeric'),
        ),
    ),
);

mycustomgraderule同一模型中的另一种方法实际上在哪里:

public function mycustomgraderule($check) {
//   Your logic here    
}

此方法应TRUE在检查验证和FALSE未验证时返回。您可以对 执行相同的操作olevel_subject_code,但如果您希望验证输入的至少 5 个科目,以便每个科目都有一个输入的成绩,我建议您为两者创建一种方法并将其作为自定义验证规则'olevel_subject_code'放入$validate. $check 参数保存正在验证的字段的数据。在这个(以及每一个)自定义规则中,您可以使用模型的$this->data属性,该属性包含所有当前模型数据。

于 2012-06-30T07:28:13.207 回答