cakephp validation using jquery validator

Friday, December 9, 2011

print this page
send email
In my project client was asked to do client side validation.I decided to use jquery validator in my Cakephp project.I tried find out "how to do" with my project..I got to read jquery validator plugin DOC and tried some demo version.After many fat burning session i decided to post in my blog so that starter like me can go through you it and find a solution.

Steps
--------
1.download jquery validator plugin from this link.

2.Copy jquery.validate.js and jquery.js in cake/app/webroot/js folder.

3.Use this code in your view to load script inline in your view

$options = array('inline'=>'inline');
echo $javascript->link('validator/jquery.validate.js',null,$options);

4.Try this was in your view

$(function(){
$('#admin_register').validate({
debug: false,
errorClass: "authError",
errorElement: "span",
rules: {
"data[User][email]": {
required: true,
email: true
},
"data[User][telephone]": {
number: true,
minlength: 10
}
},
highlight: function(element, errorClass) {
$(element).removeClass(errorClass);
}
});
});

5.Your form is going to be something like this.

echo $form->create('User', array('action' => 'admin_register','inputDefaults'=> array('label'=>false),'type' => 'file','id'=>'admin_register'));


POINTS TO GIVE SOME ATTENTION
-------------------------------------------------------------

$('#admin_register').validate({ ----------its using form element's ID .

"data[User][telephone]": {-----------------Give form input field's name attribute as mentioned here.(I made mistake in this section, that is i used data[User][telephone] instead of "data[User][telephone]" )

0 comments:

Post a Comment