Cakephp: Validating an input field depending on an

2019-08-20 19:12发布

Hie guys i need help with my code I don't know how to do it. I have a form where a student selects an exam body, if the exam body selected is zimsec marks should be empty and if the exam body is cambridge marks should not be empty and should take a range depending on grade. validMarks is the function I am using to validate marks and it stopped working when i allowed marks to be empty so as to accomodate Zimsec.

My add.ctp

echo "<td>"; 
echo $this->Form->label('Mark(%): ');
echo "</td><td>";   
echo $this->Form->input("ApplicantOlevelQualification.mark.$s",array('label'=>''));
echo "</td></tr>";
echo $this->Form->label('Exam Body<font color=red>*</font>');
$exambody=array(
    'ZIMSEC'=>'ZIMSEC',
    'CAMBRIDGE'=>'CAMBRIDGE'
);
echo $this->Form->select('exam_body_code',$exambody,array('empty'=>'Please Select','selected'=>false,'label'=>'Exam Body<font color="red">*</font>'));

My Controller

$exam_body_code = $this->data['ApplicantOlevelQualification']['exam_body_code'];
'mark' => $this->data['ApplicantOlevelQualification']['mark'][$i],

My model

'exam_body_code' => array(
    'notempty' => array(
        'rule' => array('notempty'),
    ),
),
'mark' => array(
    //'numeric' => array(
    //'rule' => array('numeric'),
    'rule' => array('validMarks'),
        'message' => 'Wrong mark for this grade, please try again.',
        'allowEmpty' => true,
    //  ),
),

public function validMarks($check) {
    $grade=($this->data['ApplicantOlevelQualification']['grade']);
    $mark=($this->data['ApplicantOlevelQualification']['mark']);
    //var_dump($mark);
    if($grade== 'A' && $mark>74) {
        // $this->validationError( 'grade', 'Grade A must be greater than or equal to 75%' );
        //Access $this->data and $check to compare your marks and grade;
        return true;
    } elseif( ($grade)== 'B' && ($mark>64)) {
        return true;   
    } elseif( ($grade)== 'C' && ($mark)>50) {
        return true;   
    } elseif( ($grade)== 'D' && ($mark)>40) {
        return true;   
    } elseif( ($grade)== 'E' && ($mark)>30) {
        return true;   
    } elseif( ($grade)== 'U' && ($mark)>0) {
        return true;   
    } else {
        return false;
    }

    //Access $this->data and $check to compare your marks and grade..
 }

1条回答
甜甜的少女心
2楼-- · 2019-08-20 19:48

if the exam body selected is zimsec marks should be empty and if the exam body is cambridge marks should not be empty and should take a range...

In that case you should split validation into 2 functions:

function emptyIfZimsec($data) {
    return $this->data['ApplicantOlevelQualification']['exam_body_code'] != 'ZIMSEC'
        || empty($this->data['ApplicantOlevelQualification']['mark']);
}

function validMarks($data) {
    if ($this->data['ApplicantOlevelQualification']['exam_body_code'] != 'CAMBRIDGE')
        return true;

    ...

emptyIfZimsec will result in a validation error if the code is ZIMSEC and mark is not empty. and validMarks will check CAMBRIDGE marks (and skip if ZIMSEC)

This way you can also output separate validation error messages for each case.

Hope this helps.

查看更多
登录 后发表回答