danatel
danatel

Reputation: 4982

How do I add a 'Choose One' option to a combobox selector and then validate it in CakePHP?

I am a Cake PHP novice.

I want to edit the table "Issue". This table contains a field "priority_id" related to another table called "Priority" by an foreign key. This table contains three values "Severe", "Disaster", "ToDo". User can select the priority using a combobox (input select).

The priorities are loaded like this:

$priorities = $this->Issue->Priority->find('list');

This works for me.

I need to add a fourth option to the combobox called "Choose". This value will be a default one. The user cannot submit the form when this value is selected. The motivation is to force the user to select one of the meaningful values instead of submitting the first one randomly.

1) How can I fill the array $priorities ? 2) How can i validate the form?

Thanks

Upvotes: 1

Views: 1909

Answers (1)

neilcrookes
neilcrookes

Reputation: 4203

In the issues/add.ctp and issues/edit.ctp views, add an empty key to the options array sent as the 2nd param to the $form->input() method, e.g.

echo $form->input('priority_id', array('empty' => 'Choose'));

This will add an option at the top of the combo box with text 'Choose' and the value of the option will be an empty string.

The in your Issue model, you can add a validation for the priority_id field, e.g.

var $validate = array(
  'priority_id' => array('numeric')
);

Upvotes: 2

Related Questions