Check the value of a Form from model

I’m wondering in forum but I dont see a right threat to solve my problems. I need to place a checkbox in a form and check if this checkbox is selected. Otherwise I cant save data in a database.

I mean something like this one for email:


        array('email', 'email', 'allowEmpty' => false),

or this one for password:


        array('password', 'compare', 'compareAttribute' => 'confermapassword'),

What if I’ve a checkbox?

And what if I’ve 2 radio (like []accept [*]decline) and I want that user check “accept” choice?

I already answered in the other thread but I’ll post it here for better overview

You can use it in the rules as required if they need to check it, another way is to use the parameter checked on your checkbox, this way it’s auto checked and users have to select or deselect it.


<?php echo $form->checkBox($model,'name',  array('checked'=>'checked')); ?>

If you want to only save the data if the accept box is checked you should use the beforeSave method and check the input of the checkbox and act accordingly.

I want to set some roles in the model.

Mmmm I need to set some roles in the model?

No, the post about roles was about a different subject.

You should add your checkbox in the required rules of your model. You should then create a beforeSave() function wich gets the data from the checkbox before it is saved in the database. Then based on the value of checkbox you should do what you want to do with the data.