More details for input-validation.md

This commit is contained in:
Alexander Makarov
2015-09-01 01:42:02 +03:00
parent 2e65d0ce28
commit ae2dea6b20

View File

@ -373,7 +373,10 @@ class MyForm extends Model
A standalone validator is a class extending [[yii\validators\Validator]] or its child class. You may implement
its validation logic by overriding the [[yii\validators\Validator::validateAttribute()]] method. If an attribute
fails the validation, call [[yii\base\Model::addError()]] to save the error message in the model, like you do
with [inline validators](#inline-validators). For example you will need to create the above validator into [[components/valdators/CountryValidator]],
with [inline validators](#inline-validators).
For example the inline validator above could be moved into new [[components/valdators/CountryValidator]] class.
```php
namespace app\components;
@ -391,14 +394,13 @@ class CountryValidator extends Validator
}
```
If you want your validator to support validating a value without a model, you should also override
[[yii\validators\Validator::validate()]]. You may also override [[yii\validators\Validator::validateValue()]]
instead of `validateAttribute()` and `validate()` because by default the latter two methods are implemented
by calling `validateValue()`.
Here is an example of how you could set up the above within your model
Below is an example of how you could use the above validator class within your model.
```php
namespace app\models;
@ -406,22 +408,20 @@ use Yii;
use yii\base\Model;
use app\components\validators\CountryValidator;
class EntryForm extends Model {
class EntryForm extends Model
{
public $name;
public $email;
public $country;
public function rules(){
$r = [
public function rules()
{
return [
[['name', 'email'], 'required'],
['country',CountryValidator::className()],
['email','email'],
['country', CountryValidator::className()],
['email', 'email'],
];
return $r;
}
}
```