chinni776
chinni776

Reputation: 165

How to use jquery.validate plugin in cakephp form

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?

Upvotes: 9

Views: 10488

Answers (3)

Amar Ranjan Das
Amar Ranjan Das

Reputation: 1

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));  ?>

Upvotes: 0

Leo
Leo

Reputation: 6571

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.

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630389

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
        }
    }
  });
});

Upvotes: 15

Related Questions