Form Listbox Validation Oddness

I’m having a problem with some odd listbox validation when used on a form.

The issue is that I have one set of dependent dropdown’s that work fine, both are mapped to Varchar data by the model.

The second set of drop downs are mapped to Integer fields.

Both of the dependent dropdowns are disabled until the the parent dropdown has a selection.

On form validate IF the child of the Varchar mapped dropdown is not selected it validates and Yii does it’s magic with the error indicators being set (text and field background color)

However the child of the dropdown that is mapped to the integer field in the model does not show as an error for being empty.

What’s odd is that this is only the case on entry to the page, for example if I come in fresh to the page and just hit submit, all fields are empty and show up as error conditions EXCEPT the one dependent listbox that is mapped to an integer field.

Last test was on the listbox for the integer field that doesn’t seem to validate was to make it enabled. At that point it works and shows the error conditions from not being set.

It seems that if the field type is Integer, and the dropdown is DISABLED the validation does not happen? Anyone else run into odd things like this?

Sorry for the complex description, hope it’s understandable!


Hi sganz,

What are the default values for those 2 fields?

When a field is disabled, the post data doesn’t contain it, and Yii will not overwrite the default value for it.

Don’t you have zero as the default value for you integer field and isn’t zero a valid value for it?

I think that is the gist of the problem. The db table has a default value of ‘0’ for the integer fields, and and empty string (not a null) for the character fields. The operations for the disabled integer dropdown may be the correct operation, but the odd part is trying to make both of the dropdown behave the same irrespective of datatype. Both have the same and single entry into the listbox options html which is something like this -

// for the int field

<option value="">Select Model</option>

// for the varchar field

<option value="">Select City</option>

In some testing I found when I removed the listbox and just put up a simple text field the value initially was 0 (zero), which really didn’t shock me. I guess for the integer field it has a value of something which is in this case 0 and the validation rule is numeric and required so it can never be wrong. I think the solution to make it match the validation would be to come up with a rule that says NOT ZERO. I had made a conscious effort to not use a value of 0 for this type of data and I think it may pay off as I can validate that as a special case I guess, just need to read more on how to do that, and given how Yii seems to work it’s likely very simple ;)


Let’s say that after a good night sleep and writing the last post it’s clear how it works!

I added this to my models validators and all is well in the universe!

   		array('int_modell','compare','compareValue'=>'0', 'operator'=>'!=', 'message'=>Yii::t('LeadGen','Please Select a Model')),

The more I use Yii the more I like!


P.S softark Thanks for the reply!