Cakephp:根据从下拉列表中选择的选项验证输入字段 [英] Cakephp: Validating an input field depending on an option selected from a dropdown list

查看:195
本文介绍了Cakephp:根据从下拉列表中选择的选项验证输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助我的代码我不知道如何做到。我有一个表格,其中学生选择一个检查主体,如果选择的检查主体是zimsec标记应该是空的,如果检查主体是剑桥标记不应该是空的,应根据年级取一个范围。 validMarks是我用来验证标记的函数,当我允许标记为空以容纳Zimsec时,它停止工作。

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.

我的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>'));

我的控制器

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

我的模型

'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..
 }


推荐答案


如果所选的考试正文是zimsec,则标记应为空,是剑桥标记不应该是空的,应该取一个范围...

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...

在这种情况下,你应该将验证分为两个函数: / p>

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 将导致验证错误,如果代码是ZIMSEC和mark不为空。和 validMarks 将检查CAMBRIDGE标记(并跳过如果ZIMSEC)

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.

希望这有助于。

这篇关于Cakephp:根据从下拉列表中选择的选项验证输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆