我在cakephp验证中遇到了问题。下面我描述我想要做的事情:
<select>
<option> Value 1</option>
<option> Value 2</option>
</select>
Input 1 : <input type="text" name="val1" value="" />
Input 2 : <input type="text" name="val2" value="" />
假如我从下拉列表中选择“值1”,则“输入1”文本框将被验证,如果我从下拉列表中选择“值2”,则“输入2”文本框将被验证。
我如何在cakephp验证中执行此操作?请帮帮我..这是非常紧急的.plsssss
答案 0 :(得分:0)
尝试使用Cake FormHelper类来构建表单 - 它几乎可以为您完成所有操作并且可以自定义http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
HTML
<select name='select_val'>
<option value='val_1'>Value 1</option>
<option value='val_2'>Value 2</option>
</select>
Input 1: <input type="text" name="val1" value="" />
Input 2: <input type="text" name="val2" value="" />
MyModel.php
public $validate = array(
'select_val' => array(
'rule' => 'customValidationForInputs',
'message' => 'Invalid value'
)
);
public function customValidationForInputs($check) {
if ($check['select_val'] == 'val_1') {
//access the value in 'val1' through $this->data[$this->alias]['val1']
//perform some checks on the value
if (the value in 'val1' passes your checks) {
return TRUE;
}
else {
return FALSE;
}
}
if ($check['select_val'] == 'val_2') {
//access the value in 'val2' through $this->data[$this->alias]['val2']
//perform some checks on the value
if (the value in 'val2' passes your checks) {
return TRUE;
}
else {
return FALSE;
}
}
//if we reach here then the value of the select was not either of the ones specified so presume this is invalid
return FALSE;
}
有关验证的更多信息,请查看http://book.cakephp.org/2.0/en/models/data-validation.html