How To Get Select Option Values

Hi,

My form have select option and check box field. i m not getting the values of those fields.

  1. <?php echo $form->dropDownList($model,‘vehicle_type’,array(’’=>‘select’,1=>‘Tempo’,2=>‘Jeep’,3=>‘Lorry’,4=>‘Bus’,5=>‘Tipper’,6=>‘Tanker’,7=>‘Tractor’,8=>‘Pick up van’));?>

when select the options it will display 1,2 like .how do change it.

2)<?php echo $form->checkBoxList($model, ‘general_condition_of_vehicle’, array(‘Ava fitting’ => ‘Ava fitted’, ‘Ava working’ => ‘Ava working’, ‘Brake Condition’ =>‘Brake Condition’));?>

For checkbox when i choose the options and hit submit button it doesnot accept it. gives a validation error.

When you select an option from the dropDownList, the returned value is the first part of the array assignment.

In the above, when you select ‘Tempo’ you will get a ‘1’ back. You will need to translate that ‘1’ back to ‘Tempo’ elsewhere in your code. What I usually do is create a couple of function in the model:




public function getListLabel($id) {

   $list = $this->getDataList();

   return $list[$id];

}

public function getDataList() {

   return array(

      1=>'Tempo',

      2=>'Jeep',

      3=>'Lorry',

      4=>'Bus',

      5=>'Tipper',

      6=>'Tanker',

      7=>'Tractor',

      8=>'Pick up van'

   );

}



This way you can add another vehicle type at any time.

Then you can change the _form.php file:




 1) <?php echo $form->dropDownList($model,'vehicle_type', Vehicle::model()->dataList), array('empty'=>'Select'));?>



The ‘empty’ option may not be in the right place, or exact right code. This is off the top of my head, but I have seen it in other postings/Google searches.

I have not used the CheckBoxList, so I can’t help with that.

Thanks for your suggestion. I have tried but it will not work… still it returns the same . Is there any specific rules i need to change in model.

Yes the dropDown WILL return a number Tempo=1, so 'vehicle_type’will store a ‘1’. To display ‘Tempo’ instead, as I indicated, will will need to manually retrieve the ‘label’;

In view.php




<?php echo CHtml::encode(Model::model()->getListLabel($model->vehicle_type)); ?>



for display in a GridColumn:


array(

   'name' => 'vehicle_type',

   'value' = > 'Model::model()->getListLabel($data->vehicle_type)',

),