Enforcing "You must agree" requirements in forms

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

Checkboxes have two possible values, true and false, but some applications require that the user select one particular value before proceeding. Examples might be:

  • I agree to the terms and conditions
  • I agree not to ship these goods into a terrorist country
  • I agree that Yii is the best MVC framework ever
  • I am over 18 years of age

In this case, the checkbox (implemented in the usual way in a form) warrants a validator that enforces this condition, and it can be done with the built-in compare validator along with a custom message:

Here, the model's iagree attribute can be attached to a form, and validation will be applied to it:

class SomeModel extends CActiveRecord {
    ...
    public function rules()
    {
        array('iagree', 'compare', 'compareValue' => true, 
              'message' => 'You must agree to the terms and conditions' ),
        ...
    }
    ...
}

Be sure to change the message to more precisely reflect what the user is agreeing to, as well as to provide an error label in the form.

25 0
17 followers
Viewed: 33 768 times
Version: Unknown (update)
Category: How-tos
Written by: Steve Friedl
Last updated by: Steve Friedl
Created on: Jan 20, 2011
Last updated: 13 years ago
Update Article

Revisions

View all history

Related Articles