开发者

Live Jquery Form Validation and CakePHP Validation

I know that CakePHP has very good validation out of the box, but I want to perform live JS validation on my form before it is submitted.

I've played with the jquery validation plugin, which is exactly the kind of thing I want for the front end. (Its for a newsletter subscribe form in a modal box).

The problem is, CakePHP's validation will redisplay the form containing the error messages if my validation fails. I don't need this, I just want to开发者_如何转开发 return false or redirect should it fail server side.

Does anyone know of any jquery validation that will sit nicely alongside the cakephp automagic form validation, and give me the best of both worlds? Thanks.


I have written a CakePHP plugin that does client side form validation on CakePHP forms.

It uses the CakePHP model's validation rules and generates a jQuery script that will enable validation of form inputs pretty much automatically.

Only downside (for now) is that supports only a limited number of data types for automatic validation. But on the other-hand it's hosted on gitHub and GPL licensed, so anyone can add as much validation as they need.


You could remove the automatic validation on the Cake side of things, and just use your jquery validation to perform your client side work. Of course, you'll need to secure the controller so that non-valid fields aren't saved. To do this, I think you want to turn off the automatic validation in Cake, and make a call in your controller to $this->validates($this->data), which will return false on an invalid data submission. Check out the arguments to Model::save(), as one of them allows a save to complete even with bad data (this is what you want, because otherwise it'll redisplay the form).

To sum up, just go ahead and perform your client side validation with jquery. In your controller, check to make sure the data validates before saving it (if Controller::validates returns false, you'll need to redisplay the form anyhow).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜