I have a basic ajax form on my site utilizing the Js helper, it works fine but when there are validation errors the update callback duplicates the entire page inside of my success div.
Success div:
<div id="success"></div>
Submit button:
echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success',
'class'=>'btn btn-primary'
));
Javascript:
$(document).ready(function(){
$('#postkey, #gender, #hair, #location, #message').blur(function(){
$.post(
'/Cake_ajax/Posts/validate_form',
{ field: $(this).attr('id'), value: $(this).val() }
//handleNameValidation
);
});
});
Validate form function in my controller:
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Post'][$this->request['data']['field']] = $this->request['data']['value'];
$this->Post->set($this->request->data);
if($this->Post->validates()){
$this->autorender = FALSE; // don't render a view
$this->set('error','');
}else{
$this->layout ="ajax";
$this->autoRender = FALSE;
$error = $this->validateErrors($this->Post);
$this->set('error',$this->Post->validationErrors[$this->request['data']['field']][0]);
}
}
}
I don't know if this is enough information to go on, If I need to post more code, just let me know.
thanks.
$this->autoRender = false(with a capital R)? Also, which layout is used as the default?