Tutorial :How to use jquery.validate plugin in cakephp form



Question:

My jQuery code is:

$(document).ready(function(){       $('#StudentRegisterForm').validate({                rules: {        email: {          required:true,          email:true        }      }    });      });  

and in my form email:

<td><?php echo $form->input('email',array('class required email')); ?></td>  

The problem is jquery validate plugin works on the input fields attribute 'name' but cakephp names it as data[Student][email]. If I use this name in jquery its throwing an error. If I rename the field in cakephp the email value is not passed to the database. Is there any other round about way?


Solution:1

You need just need a minor tweak, set the rule using a string, like this:

$(function(){ //short for $(document).ready(function(){    $('#RegisterForm').validate({      rules: {          "data[Student][email]": {              required:true,              email:true          }      }    });  });  


Solution:2

I had exactly this problem yesterday. The answer is to 'force' the name on the input field, like:

echo $form->input('cheque_number',array('name'=>'InvoiceChequeNumber','value'=>''));  

I spent a while trying to avoid doing that, but I couldn't find any alternative. There are no problems for CakePHP when you do it like this.


Solution:3

The first one is better to use, because this will maintain cake this->data structure, but second one is not. This is to remind that, when you will use the data[modelname][fieldname] give the class name in the input aray like:

<?php echo $form->input('email',array('type'=>'text','class' => array('required','email'),'error'=>false,'label'=>false,'div'=>false));  ?>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »