cakePHP helper for jQuery Validation plugin

Jan 17, 2008
I've been working on a page that needs some heavy validation. I decided to take advantage of the Improved Advanced Validation method in cakePHP for server-side and the jQuery Validation plugin for client-side. After changing error messages in both the Model and inside my View, I decided to make a way for the messages defined in the Model to automatically define the messages in the View.  So, I wrote a helper that makes this possible.

How To

First, download the helper:
After putting jquery_validation.php in your helpers folder, and your model follows the advanced validation convention for the Model::$validate array, you can use the following code when setting up the validation plugin in your view: <script>
    $(document).ready(function(){
        $("#registerForm").validate({
            messages: {
                <?=$jqueryValidation->messages(Inflector::singularize($this->controller->name), $validationRules)?>
            }
        });
    });
</script>
Now all your validation messages you define in the model will be defined for your client-side validation as well. Make sure your validation labels in the Model match the names used in the Validation plugin (ie: required, minlength, maxlength, email, etc).
2 comments

Abba Bryant

Mar 5, 2008
Just curious, do you use the 1.2 branch or are you using the 1.1 stable? The advanced validation article you say to use is aimed at the 1.1 branch and I was wondering if this helper would still work for 1.2 I am testing this all right now so I might answer my own questions.

Bret KUhns

Mar 10, 2008
@Abba: My apologies for not mentioning the cakePHP version I'm using. As 1.2 has yet to release a stable version and most of my development is for business clients, I am using a stable release of 1.1. I've only recently started to tinker with 1.2, so I'm not sure if this will work with that branch.

More Posts

Other Places

Recent Photos

image image image image image image