Skip to content

Instantly share code, notes, and snippets.

@Tuan-T-Nguyen
Last active October 11, 2017 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tuan-T-Nguyen/4d8ca578bfb985cf88c261c974cfb34d to your computer and use it in GitHub Desktop.
Save Tuan-T-Nguyen/4d8ca578bfb985cf88c261c974cfb34d to your computer and use it in GitHub Desktop.
Rules Validator Yii Examples

Yii 1.1

Validates that the attribute value is either trueValue or falseValue.

  1. trueValue, the value representing true status. Defaults to '1'
  2. falseValue, the value representing false status. Defaults to '0'
  3. strict, when this is true, the attribute value and type must both match those of {trueValue} or {falseValue}. Defaults to false, meaning only the value needs to be matched.
  4. allowEmpty, whether the attribute value can be null or empty. Defaults to true. Example:
array('name', 'boolean', 
        'allowEmpty'=>false,
        'trueValue'=> 'Tuan',
        'falseValue'=>'Tai', 
        'message'=>'The value must be "Tuan" or "Tai" and not empty',
)

Validates that the attribute value is the same as the verification code displayed in the CAPTCHA.

  1. allowEmpty, whether the attribute value can be null or empty. Defaults to false, meaning the attribute is invalid if it is empty.
  2. caseSensitive, whether the comparison is case sensitive. Defaults to false
  3. captchaAction, string ID of the action that renders the CAPTCHA image. Defaults to 'captcha', meaning the 'captcha' action declared in the current controller. This can also be a route consisting of controller ID and action ID. Example
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements())

Full example about captcha in here

Compares the specified attribute value with another value and validates if they are equal.

  1. allowEmpty, whether the attribute value can be null or empty. Defaults to false
  2. compareAttribute, the name of the attribute to be compared with
  3. compareValue, the constant value to be compared with
  4. strict, whether the comparison is strict (both value and type must be the same.). Defaults to false.
  5. operator, the operator for comparison. Defaults to '=' '=' or '==', '!=', '>', '>=', '<', '<=' Example
array ('usernameA', 'compare', 'compareValue' => '10', 'operator' => '>=', 'message' => 'must be greater than 10'),
array ('usernameB', 'compare', 'compareAttribute' => 'usernameC', 'operator’ => '>', 'message' => 'must be greater than usernameC'),

Validates that the attribute value is a valid date, time or datetime.

  1. allowEmpty, whether the attribute value can be null or empty. Defaults to true
  2. format, the format pattern that the date value should follow. This can be either a string or an array representing multiple formats. Defaults to 'MM/dd/yyyy'. Please see CDateTimeParser for details about how to specify a date format.
  3. timestampAttribute, the name of the attribute to receive the parsing result. When this property is not null and the validation is successful, the named attribute will receive the parsing result.

Yii 2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment