【发布时间】:2014-03-13 20:58:39
【问题描述】:
我有一个通过 ajax 调用的 validate_form() 函数,它应该从属于主模型的操作中验证关联模型的输入字段。
这是验证方法
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Schedule'][$this->request->data['field']] = $this->request->data['value'];
$this->Schedule->ScheduleContact->set($this->request->data);
if($this->Schedule->ScheduleContact->validates()){
$this->autoRender = false;
}else{
$error = $this->validateErrors($this->Schedule->ScheduleContact); //this pulls off all validation
//$this->set('debug_param', $error);
$this->layout= 'ajax';
echo $error[$this->request->data['field']][0]; //for the ScheduleContact Model, and puts it in an array
$this->autoRender = false;
}
}
}
每当触发模糊事件时都会发出 ajax 请求:
$(document).ready(function(){
$('#name, #surname, #email, #tel_no, #address, #city').blur(function(e){
var self = e.target.id;
$.post(
'/name_project/schedules/validate_form', // it will submit to the validate_form action
{field: $(this).attr('id'), value: $(this).val()},function(data){
handleValidation(data,self) // data is the error.
}
);
});
});
发出请求并且没有错误。但是验证没有发生,并且 validates() 条件每次都返回 true 。任何想法为什么?谢谢
[编辑]
这是请求数据的 var_dump
array (size=3)
'field' => string 'name' (length=4)
'value' => string '' (length=0)
'Schedule' =>
array (size=1)
'name' => string '' (length=0)
【问题讨论】:
-
@MattDiamant 感谢您的编辑。但是你能帮我解决这个问题吗?非常感谢
标签: php jquery ajax validation cakephp