thecodedeveloper.com
thecodedeveloper.com

Reputation: 3240

validation is not working in cake php if retrieve data from other model

i have table "car_types" ,a Controller users_controller, model Car_type and action url

localhost/carsdirectory/users/dashboard

dashboard.ctp(view)

 <?php echo $this->Form->create('Users', array('type' => 'file', 'action' => 'dashboard')); ?>
 <select>
 <?php foreach($car_type as $key => $val) { ?>
 <option value="" selected="selected">select</option>
 <option value="<?php echo $val['Car_type']['id']; ?>">
 <?php echo $val['Car_type']['car_type']; ?>
 </option>
 <?php } ?>
 </select>
 <?php echo $this->Form->end(array('label' => 'Submit', 'name' => 'Submit', 'div' => array('class' => 'ls-submit')));?>

Car_type.php(model)

 class Car_type extends AppModel
   {
   var $name = 'Car_type';

   var $validate = array(

   'car_type' => array(

       'rule' =>'notEmpty',
       'message' => 'Plz select type.'
         )
     ); 
    }

users_controller.php(controller)

  public function dashboard(){

      $this->loadModel('Car_type'); // your Model name => Car_type

      $this->set('car_type', $this->Car_type->find('all'));

   }

but when i click submit button i want to show msg(Plz select type) and right now it's not working i know i have problem in my code not i m not able to sort out it so plz help me

thanks in advance, vikas tyagi

Upvotes: 0

Views: 666

Answers (2)

Paulo Rodrigues
Paulo Rodrigues

Reputation: 5303

This validation rule is to validate when you add some car type, not user.

For this, you need put validation in User model from car_type_id field:

class User extends AppModel {
    var $name = 'User';

    var $validate = array(
        'car_type_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Please, select car type.'
        )
    );
}

And your form:

$this->Form->input('car_type_id', array('options' => $car_type, 'empty' => '- select -'));

Your Controller can be simply:

$this->set('car_type', $this->User->Car_type->find('all'));

However, do not know if this is your entire code to confirm the relationship between these two models are correct.

Upvotes: 1

Naveen Kumar
Naveen Kumar

Reputation: 4601

Considering that it's data, you should store the list of valid choices in the model.

var $carType= array('a' => 'Honda', 'b' => 'Toyota', 'c' => 'Ford');

You can get that variable in the Controller simply like this:

$this->set('fieldAbcs', $this->MyModel->carType);

Unfortunately you can't simply use that variable in the rule declaration for the inList rule, since rules are declared as instance variables and those can only be initialized statically (no variables allowed). The best way around that is to set the variable in the Constructor:

var $validate = array(
    'carType' => array(
        'allowedChoice' => array(
            'rule' => array('inList', array()),
            'message' => 'Pls select type.'
        )
    )
);

function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);

    $this->validate['carType']['allowedChoice']['rule'][1] =
    array_keys($this->fieldAbcChoices);
}

Upvotes: 0

Related Questions