In the [Models](structure-models.md#validation) section, we have described how data validation works
in general. In this section, we will mainly focus on describing core validators, how to define your
own validators, and different ways of using validators.
As a model both represents data and defines the business rules to which that data must adhere, comprehending data validation is key to using Yii. In order to learn model validation basics, please refer to [Model, Validation subsection](model.md#Validation).
## Error Messages
### Creating your own validators (Inline validators)
## Core Validators
If none of the built in validators fit your needs, you can create your own validator by creating a method in you model class.
This method will be wrapped by an [[yii\validators\InlineValidator|InlineValidator]] an be called upon validation.
You will do the validation of the attribute and [[yii\base\Model::addError()|add errors]] to the model when validation fails.
Yii provides a set of commonly used validators, found primarily within the `yii\validators` namespace.
The method has the following signature `public function myValidator($attribute, $params)` while you are free to choose the name.
Here is an example implementation of a validator validating the age of a user:
Instead of using lengthy validator class names, you may use *aliases* to specify the use of these core
validators. For example, you can use the alias `required` to refer to the [[yii\validators\RequiredValidator]] class:
This guide describes all of Yii's validators and their parameters.
> Note: Because data input submitted via HTML forms are all strings, you normally should leave the
[[yii\validators\BooleanValidator::strict|strict]] property as false.
Standard Yii validators
-----------------------
The standard Yii validators are defined in many Yii classes, found primarily within the `yii\validators` namespace. But you do not need to specify the full namespace for the standard Yii validators as Yii can recognize them from defined aliases.
Here's the list of all validators bundled with the Yii framework, including their most useful properties. The default value for each property is indicated in parentheses. Note that this does not present an exhaustive list of each validator's properties.
This validator is usually used together with [[yii\captcha\CaptchaAction]] and [[yii\captcha\Captcha]]
to make sure an input is the same as the verification code displayed by [[yii\captcha\Captcha|CAPTCHA]] widget.
Validates that the attribute value is a valid email address. By default, this validator checks if the attribute value is a syntactical valid email address, but the validator can be configured to check the address's domain for the address's existence.
...
...
@@ -150,7 +132,7 @@ Validates that the attribute value is a valid email address. By default, this va
-`checkPort`, whether to check port 25 for the email address. _(false)_
-`enableIDN`, whether the validation process should take into account IDN (internationalized domain names). _(false)_
This guide describes all of Yii's validators and their parameters.
## Data Filtering
## Ad Hoc Validation
Sometimes you need to validate a value that is not bound to any model, such as a standalone email address. The `Validator` class has a
`validateValue` method that can help you in these scenarios. Not all validator classes have implemented this method, but the ones that have implemented `validateValue` can be used without a model. For example, to validate an email stored in a string, you can do the following:
...
...
@@ -289,4 +354,42 @@ if ($validator->validate($email, $error)) {
}
```
TBD: refer to http://www.yiiframework.com/wiki/56/ for the format
DynamicModel is a model class primarily used to support ad hoc data validation.