(if you still need it)
create the form in your view. Add this jquery script:
var myAjaxSettings = {
type: "POST",
dataType: "json",
success: function(data) {
if (data['success'] == true) {
} else {
alert(data['message']);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Internal error: Couldn't submit form");
}
}
$(function() { // same as document.onready
$("#MyForm").submit(function(e) {
e.preventDefault();
myAjaxSettings.url = $("#MyForm").attr('action');
myAjaxSettings.data = $("#MyForm").serialize();
$.ajax(myAjaxSettings);
});
});
Then in the controller do your validation as such:
Configure::write('debug','0');
$this->autoRender = false;
if (!$this->Model->save($this->params['form'])) {
$message = '';
foreach ($this->Album->validationErrors as $e) {
$message .= $e;
}
return json_encode(array(
'success'=>false,
'message'=>$message
));
Thats the just of it. The jquery will intercept your form post and do
an ajax request. Your controller will catch the data from jquery and
try and save it (you may need to tweak it somewhat to get it to work.
Note that the data will be in $this->params['form'] not in $this-
>data). Then if it fails, create a json output to return it back to
your view. The javascript should then alert the user with any
validation errors.
On Feb 23, 4:59 pm, anl hp <anle...@gmail.com> wrote:
> please don't!! don't check that within the controller and don't even think
> about check it with javascript!! :'( use Model's custom validation, these
> don't need fields to be a field of your database, within a custom validation
> function you can just check if you have a 'terms' field and is true, 1. Let
> Cake help you!!
> --
> anl
>
> On Tue, Feb 23, 2010 at 1:20 PM, Jeremy Burns <jeremybu...@me.com> wrote:
> > Check for their presence/value in the controller, and then unset them from
> > the array before saving.
>
> > Jeremy Burns
> > jeremybu...@me.com
>
> > On 23 Feb 2010, at 12:16, Zac Tolley wrote:
>
> > > Ah but, the thing is I'm verifying fields that are not part of the
> > > model, I'm simply verifying that they agree to some terms and that the
> > > 2 passwords they entered match
>
> > > Check out the new CakePHP Questions sitehttp://cakeqs.organd 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+unsubscribe@googlegroups.com<cake-php%2Bunsubscribe@googlegroups.com>For more options, visit this group at
> >http://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd 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+unsubscribe@googlegroups.com<cake-php%2Bunsubscribe@googlegroups.com>For more options, visit this group at
> >http://groups.google.com/group/cake-php?hl=en
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment