Automagic JavaScript Validation Helper - Beta

Posted by Matt on Sun, Feb 17 2008

Way back when CakePHP had a wiki there were a series of articles on "Advanced Validation." One aspect of these articles was using the Model rules in JavaScript to validate on the client side. The code (I believe this is it) was originally for CakePHP version .10 (iirc). I had been using a heavily modified version of this code even in my 1.2 projects, but it was due for a ground up re-write.

I wrote an article for PHPArch not too long ago with a generic PHP approach for JavaScript validation. Using that JavaScript as a base, I created a helper to convert the model rules into JavaScript.

Here are the links:
Download
Instructions
Demo

This is all very beta, so please let me know if you find an issue. The helper should work with the three formats for defining validation in your model (output from bake, one rule per field, multiple rules per field).

If you are worried about security, there is an option to only include certain rules. See the tips section at the end of the instructions. Thanks to Marc for suggesting this.

Posted in CakePHP, Code

42 Comments

Luke said on Feb 27, 2008
I have been needing to check out JQuery with Cake - seems 90% use it now!

but, I should add I have used with some success a js library which complements the CakePHP method - you just add a class name and it is very similar / simple to get going.

http://tetlaw.id.au/view/javascript/really-easy-field-validation
George said on Feb 17, 2010
I am interested in a helper for that javascript validation.. have you done one? Are you interested in making one?
Matt said on Feb 27, 2008
Hey Luke,
Thanks for the link. I'm sure you could write a helper that takes your model validation rules and converts them into a format suitable for this lib.

-Matt
Travis Cline said on Apr 02, 2008
I dig this approach -- the big argument against such is when the validation logic to too complex or too sensitive to convert to or expose in javascript.

The best solution would be one that can convert some of the cake validation logic into javascript for the sake of speed and then seamlessly use xhr calls to check the other fields (and ultimately falling back to server-side checks if js is not available).

This is doable with http://docs.jquery.com/Plugins/Validation but there are a couple snags with the current implementation plugin:
- it doesn't currently allow returning of error messages (should be trivial)
- (as far as I can tell) it will use a http request for each field which also means they're submitted separately

Regardless,
Thanks for your work Matt!
Pascal said on Aug 20, 2008
Hi,

I am facing a problem with your Helper.
I have followed your instructions but nothing happens, no error, no message.

Do you know if it is compatible with jquery-1.2.6 ?

Would it be possible that you also give your model/controller/view samples ?

Thank you in advance
Pascal said on Aug 20, 2008
Ok, me again, I just find the errors.

The div on the view must be called " messages ".
And theses lines where missing :

validation.js :

$("#"+field).addClass("form-error");
$("#"+field).after(''+this['message']+'');

So you must have :

if (!valid)
{
//add the error message
$("#messages").append("" + this['message'] + "");

//highlight the label
$("label[for='" + field + "']").addClass("error");
// Display the message after the input
$("#"+field).addClass("form-error");
$("#"+field).after(''+this['message']+'');
}

Hope it will help :-)
Matt said on Sep 02, 2008
Hey Pascal,
The helper does work with Jquery 1.2.6. Everything you need to get going should be at http://sandbox2.pseudocoder.com/demo/validation
Dan said on Oct 06, 2008
Thanks for the helper. My feedback:

Fields with the "blank" rule should be excluded since they are typically not on the forms. I added this to the line that checks for the "on" rule.

It doesn't work with any multiple field inputs such as date, datetime, etc. To deal with this I changed the rules to use field names instead of ids. I added a check for the column type ($model->getColumnType($field)) and add month, day, year, etc to the names as appropriate.
bookme said on Oct 12, 2008
Hi Matt!

Is this helper works in CakePHP 1.2?

I am working on cakephp 1.2. After following every step of this tutorial, then nothing is happening, form is submitting without showing javascript validation error? same like your Demo example. (why not your Demo example showing Java script error before form submitting )

$(document).ready(function(){ $("#UserSignupForm").submit( function() { return validateForm(this, rules); }); });
var rules = .................. don't writing complete code


when I use alert('hello '); in validation java script then above generated code is not calling validateForm() function, I am trying it from last two days but not working.
please help me
Matt said on Oct 21, 2008
Hey bookme,
The helper does work in 1.2. You can get the latest version at:
http://github.com/mcurry/cakephp/tree/master/helpers/validation
evilbloodydemon said on Feb 02, 2009
What about i18n/l10n?
I use this sort of code in view:

echo $form->input('nickname', array(
'label'=>__('Nickname', true),
'error' => array(
'len' => __('Lenth must be 8..15 chars', true),
'uniq' => __('This nickname is not unique', true),
)
));

i.e. model contains no messages.
Matt said on Feb 03, 2009
It's not really going to work for that scenario. I don't see how the helper would be able to pull the error messages from the view and associate it in the JavaScript.
evilbloodydemon said on Feb 03, 2009
my question is where put error messages to make your helper work?
Matt said on Feb 04, 2009
If you set them in the models validation it'll work. Check here http://book.cakephp.org/view/125/Data-Validation
evilbloodydemon said on Feb 05, 2009
no, i can't, because
'message' => __('error', true)
in $validate array is not valid.
Matt said on Feb 05, 2009
Just put in the error string and the helper will run it through the internationalization function (line 99).

You'll have to do the same when you output the error if the JavaScript validation is bypassed.