Boolean Validator And True Value

Hello,

how can I make the boolean validator set the attribute to true only when the value equals the trueValue?

It sets it to true even when it is empty.

Thank you

Hi,

try this


array('yourattribute','boolean','allowEmpty'=>false),

or


array('yourattribute','compare','compareValue'=>true),

Hi

This doesn’t work. I get a error when the posted value is empty.

This doesn’t work either. It always sets the attribute to true.

Is there really no way to use a simple checkbox with Yii?

Regards,

Jiri

I tested both of two validators and work fine!

Are you sure threr is no any other suspected code? post your model/action/view code…

View is a simple form with a standard checkbox. It is hand written no Yii involved.


<input type="checkbox" name="NewItemForm[riapproved"] value="on"/>

Model and controller are attached.

Thank you,

Jiri


array('rememberMe','boolean', 'trueValue' => 'on','allowEmpty' => false), //false not true!

Hi,

yes I tried that, but then I get a validation error that the value must be either ‘on’ or ‘off’.

I actually tried nearly ever possible permutation of validators and configuration options. None has worked.

So now I ended up doing manual validation in controller. Ugly but works.

Jiri

Did you modify your code? I can’t find the ‘off’ in your code?

Also check the


<input type="checkbox" name="NewItemForm[riapproved"] value="on"/>

must be


<input type="checkbox" name="NewItemForm[riapproved]" value="on"/>

Finaly, how assign the inputs of form? RequestItem not same of NewItemForm

give us the controller/action code

Browsers don’t send any value when the checkbox is unchecked, causing your validator to error. CHtml::checkBox() has the “uncheckValue” option, which creates a hidden input that’s sent when the checkbox is unchecked.