[solved]How to validate CForm::dropdownlist

according to input-element

i used CForm::dropdownlist




<select name="MyForm[gender]" id="MyForm_gender">

<option value="0">Gender</option>

<option value="male">Male</option>

<option value="female">Female</option>

</select>



because it always selected by default with "0" value (not empty).

how i can validate with rules?

thanks

you could use CRangeValidator

http://www.yiiframework.com/doc/api/1.1/CRangeValidator#range-detail

thanks for your suggestion

is there a validation that except "0" as default value.

i mean if its value is not "0", it means not empty.

Why not user the prompt element command in your dropdown list call. That way there is no value attached to default




'gender'=>array(

    'type'=>'dropdownlist',

    'prompt'=>'Please select gender:',

    

 	... rest of your stuff

,



or




<?php echo $form->dropDownList($model,'gender', array('0'=>'Male', '1'=>'Female'), array('prompt'=>'Please Select')); ?>




it works…

thanks angelo.

no problem. It’s nice to be on the giving end of help once in a while:D

Thanks @Angelo

You are indeed the man with a hammer today. Just like your avatar ::)

Have been struggling for a while trying to find a solution for this.

I was trying all kind of regular expressions. but all this time I just needed