Sumit Parakh
Sumit Parakh

Reputation: 1143

CakePHP Model validation not working properly

I am new to CakePHP and have created a normal form to submit first name. My table name is "registers. I have created a controller named RegistersController (RegistersController.php) and a model named Register (Register.php) . Each time i submit after entering first name, it still displays error (First name is must) which it should only if i submit it without entering anything. Next i added validation for having minimum 6 characters. That validation is also not working. I mean, cakephp is not validating that rule. Could anyone please tell me where i have done anything wrong?

Model:-

class Register extends AppModel {
//put your code here

//public $useTable = "registers";

public $validate = array(
  'first'=>array(                   
       'minLength' => array(
          'rule' => array('minlength','6'),
          'field' => 'first',
          'message' => 'Minimum 6 characters required'
       ),
       'required' => array(
           'rule'=>array('notEmpty'),
           'required' => true,
           'message' => array('First name is must')
       )        
   )
);

}

Controller:-

class RegistersController extends AppController {

public $uses = array("Register");
//put your code here
public function index() {
    if($this->request->is('post')){
        //echo "Data";           
        if($this->Register->validates()){
            //$this->Register->create();
            //echo "Data validated";
            print_r($this->Register->validationErrors);                
        }else{
            //echo "Data not validated";
            print_r($this->Register->validationErrors);
        } 
    }
}

My view is as follows:-

<?php

echo $this->Form->create('Register');

echo $this->Form->input('first');

echo $this->Form->end("Submit");

?>

Upvotes: 0

Views: 632

Answers (1)

Pradeep Singh
Pradeep Singh

Reputation: 1280

You are missing this line

$this->Register->set($this->request->data);

Put it before the validation call, i.e. $this->Register->validates()

Upvotes: 2

Related Questions