ajt
ajt

Reputation: 662

how to validate select option cakephp

In Cakephp I want to detect whether the user has selected an option and not the default option of Please Select. It is a simple problem but I cant fix it because a default option means that the field is not left blank and this is hard to detect. I cant find the solution and i did try a notEmpty rule. Do I need to create my own rule?

  //model
     public $validate = array(
        'level' => array(
               'rule' => array(1,2,3),
                'message' => 'You need to Select an Option',
                'allowEmpty' => false

        ),
    ); 

 //view
   $level = array( 0 => 'Please Select', 1 => 'Primary',2 => 'Secondary', 3 => 'Both');
   echo $this->Form->input('level', array_merge($formHorizontalHtmlOptions, array('type' => 'select', 'options' => $level)));


http://stackoverflow.com/questions/12533145/how-to-validate-a-dropdown-select

 http://stackoverflow.com/questions/12533145/how-to-validate-a-dropdown-select
  http://book.cakephp.org/2.0/en/models/data-validation.html

Upvotes: 1

Views: 1377

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8606

You need to modify your "rule" field this way:

  'rule' => array('inList', array(1, 2, 3))

This will ensure that user selects one of these three options.

Hope this solves your issue.

Peace! xD

Upvotes: 3

Related Questions