Activecheckbox Ignoring Validation Rule

I have an activeCheckbox on a form that needs to be checked before submitting the page. Pretty simple stuff. But, it is not working as one would expect. I’ve read numerous posts and have also read as much as I could find in the documentation but still can’t get it to work. Even though the checkbox is checked, the page will not submit. I get the validation message notifying me that I should fix the input problems before I can proceed.

Model validation rule:


array('accept_deactivation_terms', 'required', 'requiredValue'=> 1, 'message' => 'Please accept the terms of de-activation.'),

Form:


<div class="row">

        <?php echo CHtml::activeCheckBox($model,'accept_deactivation_terms') ?>

        <?php echo CHtml::activeLabel($model,'accept_deactivation_terms'); ?>

    </div>

SiteController:


        $account = Account::model()->findByPk($user_id);


        if(isset($_POST['deactivate']))

        {

            if($account->validate())

            {

                $account->accept_deactivation_terms = $_POST['Account']['accept_deactivation_terms'];

                ...

                ...

                $account->update();

            }

In addition to this, I’ve tried using the ‘uncheckValue.’ I’ve tried the compare validator, too.

Thanks for any help. Hopefully I’m just overlooking something very simple.

Mike

The static version of validate() doesn’t work either. This always validates true regardless of whether or not the checkbox is checked.


if(CActiveForm::validate($account)) [...] //this doesn't work either

http://www.yiiframework.com/forum/index.php/topic/16189-validation-not-working/

Back to the drawing board.

Thanks, Mike

More testing…

I created a custom validator and I see why the page isn’t validating. $this->accept_deactivation_terms always returns a value of 0 whether the checkbox is checked or not.


  array('accept_deactivation_terms', 'acceptTerms', 'accepted'=> 1),


   public function acceptTerms($attribute,$params)

    {

        if ($this->accept_deactivation_terms != $params['accepted'])

        {

            $this->addError($attribute, 'Please accept de-activation terms and conditions!');

        }

    }




Where do you assign the form input values to $account? And is $account the same model as $model in the view?

Would you please post your code of the controller method as a whole?

This simple rule works for me:


array('terms', 'length', 'max' => 1),

You might also try this:


array('terms', 'compare', 'compareValue' => true, 

              'message' => 'You must agree to the terms and conditions' ),

Bingo!!

I was just looking at the $_POST values and was going to assign


$this->accept_deactivation_terms = $_POST['Account]['accept_deactivation_terms'];


if($account->validate())

{

...

}



and then I saw your post.

Man, that was it. Unfortunately, I was thinking that yii was doing that behind the scenes. Oh well…live and learn.

I thought that $account and the Account model was the same. Well it is in form but it didn’t contain the form variables/values. We do have to manually assign the form variable/values to the model before validating, correct?

Also, I do see that there are shortcuts to assign form variables to the model.

Thanks.

Mike