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
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:
And your form:
Your Controller can be simply:
However, do not know if this is your entire code to confirm the relationship between these two models are correct.