User inputs often need to be filtered or preprocessed. For example, you may want to trim the spaces around the
`username` input. You may use validation rules to achieve this goal. The following rule declaration shows
how to trim the spaces in the input by using the [trim](tutorial-core-validators.md#trim) core validator:
This guide describes all of Yii's validators and their parameters.
```php
[
['username','trim'],
]
```
You may also use the more general [filter](tutorial-core-validators.md#filter) validator if your data filtering
need is more complex than space trimming.
As you can see, these validation rules do not really validate the inputs. Instead, they will process the values
and save them back to the attributes being validated.
## Ad Hoc Validation
## Ad Hoc Validation <a name="ad-hoc-validation"></a>
Sometimes you need to do *ad hoc validation* for values that are not bound to any model.
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:
If you only need to perform one type of validation (e.g. validating email addresses), you may call
the [[yii\validators\Validator::validate()|validate()]] method of the desired validator, like the following:
```php
$email='test@example.com';
$validator=newyii\validators\EmailValidator();
if($validator->validate($email,$error)){
echo'Email is valid.';
}else{
...
...
@@ -200,9 +203,11 @@ if ($validator->validate($email, $error)) {
}
```
DynamicModel is a model class primarily used to support ad hoc data validation.
> Note: Not all validators support such kind of validation. An example is the [unique](tutorial-core-validators.md#unique)
core validator which is designed to work with a model only.
The typical usage of DynamicModel is as follows,
If you need to perform multiple validations against several values, you can use [[yii\base\DynamicModel]]
which supports declaring both attributes and rules on the fly. Its usage is like the following:
```php
publicfunctionactionSearch($name,$email)
...
...
@@ -211,6 +216,7 @@ public function actionSearch($name, $email)
[['name','email'],'string','max'=>128],
['email','email'],
]);
if($model->hasErrors()){
// validation fails
}else{
...
...
@@ -219,23 +225,123 @@ public function actionSearch($name, $email)
}
```
The above example shows how to validate `$name` and `$email` with the help of DynamicModel.
The [[validateData()]] method creates an instance of DynamicModel, defines the attributes
using the given data (`name` and `email` in this example), and then calls [[Model::validate()]].
The [[yii\base\DynamicModel::validateData()]] method creates an instance of `DynamicModel`, defines the attributes
using the given data (`name` and `email` in this example), and then calls [[yii\base\Model::validate()]]
with the given rules.
Alternatively, you may use the following more "classic" syntax to perform ad hoc data validation:
You can check the validation result by [[hasErrors()]], like you do with a normal model.