开发者

Generalize validation with jQuery AJAX in CakePHP

I stumbled across a very 'interesting' problem, I might say. I am developing a method which will help me validate forms with jQuery in CakePHP. A generalized method, I might say. So, instead of having a separate event for each form, I will have a general event which will handle all forms. But the problem is: how can I fetch the URL to which I need to send the data to be validated? In a very simple way, I could use the form开发者_如何学Go's action, but with complex routing, it is useless.

So what will be the best way to validate data? Have a separate function to handle each form, or a generalized function to handle all forms (and also how to get the URL to which the data should be sent)?

Thank you very much!


Use a generalized method for simple validation. Use class names of elements to tell your validator which rule to apply. This is simple to implement for non-empty values, but when it comes to complex rules such as: (user much select a, b and c or d) you need custom code.

For this a two-phased approach can work. A basic "non empty" validation and then pass it on to a custom one. You can use a class name on the form itself to identify which set of custom rules to apply.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜