background preloader

Yii Data Validation

Facebook Twitter

CValidator. Look up a class, method, property or event Public Properties Hide inherited properties Property Details public array $attributes; list of attributes to be validated. public static array $builtInValidators; list of built-in validators (name=>class) public boolean $enableClientValidation; whether to perform client-side validation.

CValidator

Public array $except; list of scenarios that the validator should not be applied to. CRequiredValidator. Look up a class, method, property or event CRequiredValidator validates that the specified attribute does not have null or empty value.

CRequiredValidator

When using the message property to define a custom error message, the message may contain additional placeholders that will be replaced with the actual content. In addition to the "{attribute}" placeholder, recognized by all validators (see CValidator), CRequiredValidator allows for the following placeholders to be specified: {value}: replaced with the desired value requiredValue. Property Details public mixed $requiredValue; the desired value that the attribute must have. Public boolean $strict; whether the comparison to requiredValue is strict.

Public boolean $trim; whether the value should be trimmed with php trim() function when comparing strings. CUniqueValidator. Look up a class, method, property or event CUniqueValidator validates that the attribute value is unique in the corresponding database table.

CUniqueValidator

When using the message property to define a custom error message, the message may contain additional placeholders that will be replaced with the actual content. In addition to the "{attribute}" placeholder, recognized by all validators (see CValidator), CUniqueValidator allows for the following placeholders to be specified: UniqueValidator on update. "Safe" Validation Rules. A common source of confusion among new Yii users is how the 'safe' validator works, how it works with other validators, and why it's necessary in the first place.

"Safe" Validation Rules

This article means to clear up this confusion, as well as explain the notion of Massive Assignment. Summary: A model's validation rules serve two purposes: Ensure that fields entered in a form are entered properlyDefine which form fields are allowed to be assigned to a $model variable These are related, but not the same, and the distinction is important. Looking at a set of validation rules ¶ To get started, we'll revisit what validation rules look like in a common model, and our example is taken from the Blog Tutorial "User" model (found in protected/models/User.php). ... public function rules() { return array( array('username, password, salt, email', 'required'), array('username, password, salt, email', 'length', 'max'=>128), array('profile', 'safe'), ); } ... Validation Rules ¶ Massive Assignment ¶ In reality this is calling. CDefaultValueValidator. Look up a class, method, property or event CDefaultValueValidator sets the attributes with the specified value.

CDefaultValueValidator

It does not do validation but rather allows setting a default value at the same time validation is performed. Usually this happens when calling either $model->validate() or $model->save(). Public Properties Hide inherited properties. CEmailValidator. Look up a class, method, property or event CEmailValidator validates that the attribute value is a valid email address.

CEmailValidator

CDateValidator. Look up a class, method, property or event CDateValidator verifies if the attribute represents a date, time or datetime.

CDateValidator

By setting the format property, one can specify what format the date value must be in. If the given date value doesn't follow the format, the attribute is considered as invalid. CDateTimeParser. Custom Validation Rules. In the scenarios where validation rules provided by the Yii framework won’t handle your requirements, you can create custom validation rules.

One example is the "authenticate" validation rule provided in "LoginForm" model of Yii blog demo. You can create your own by following these steps. You may also like: Change CGridView CSS file for entire application Change CGridView CSS file for entire application Verify HTTP URL with PHP Verify HTTP URL with PHP SALT implementation in YII SALT implementation in YII Upload and resize image using YII Upload and resize image using YII Specifying nested relations for eager loading Specifying nested relations for eager loading How to use PDO-MySQL with PHP How to use PDO-MySQL with PHP [ what's this ]