I do it by having jQuery make a post call when each field is changed-
In this case it sends it only does it if the form has a class "ajax".
The post request includes the field id... I do this to make it simpler
to perform the .post callback, although it could be done another way.
The post response returns the error message if there is one, which
jquery inserts into the page.
$(".ajax input:not('.hasDatepicker')").change(function (){
var action = $(this).closest("form").attr("action");
var inputId = this.id;
$("#" + inputId).removeClass("form-error form-valid");
$("#" + inputId).next(".error-message").remove();
$("#" + inputId).addClass("form-check");
$.post(
action,
$("#" +inputId ).serialize() + "&data[Input]
[id]="+inputId,
function (xml){
var message = $(xml).find('message').text();
var inputId = $(xml).find('input').text();
if(message){
$("#" + inputId).addClass("form-error");
$("#" + inputId).after("" +message+"");
} else {
$("#" + inputId).addClass("form-valid");
}
},
"xml"
);
});
I have the action of my form check for an ajax request- if it is it
performs the _validate function
function edit($id = null){
if ($this->RequestHandler->isAjax() && !empty($this->data)){
$this->_validate();
//function will save if validates
}
//rest of code
}
The validate function validates the field. In this case I'm validating
a multiple model field.
function _validate(){
$this->set('inputId', $this->data['Input']['id']);
// Validate the Affiliate, if it's ok, show no
errors. If not ok, show errors
if ($this->Affiliate->create() && $this->Affiliate-
>saveAll($this->data, array('validate' => 'only'))) {
$this->set('error', '0');
$this->set('message', '');
} else {
if(isset($this->data['Affiliate'])){
$errorMessages = $this-
>validateErrors($this->Affiliate);
}else if(isset($this->data['Contact'])){
$errorMessages = $this-
>validateErrors($this->Affiliate->Contact);
} else if(isset($this-
>data['AffiliateApplication'])){
$errorMessages = $this-
>validateErrors($this->Affiliate->AffiliateApplication);
}
if(isset($errorMessages[0])){
$errorMessages = $errorMessages[0];
}
//$errorMessages = $this->validateErrors();
$this->set('error', '1');
$this->set('message',
array_shift($errorMessages));
}
$this->render('/elements/validation', 'xml');
}
validation.ctp
I return the response as xml, which is parsed by my javascript
function
On Jul 12, 12:48 pm, Jonas wrote:
> Hello,
>
> I am using Ajax calls to save/remove entries from a database. When
> saving I use validation rules from my Model which works fine (they
> used to be called by a post submit before). Then I used $form->isFieldError
> to determine which field caused the error in the
>
> validation. But how do I do this when validating through Ajax?
>
> Thanks,
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en