More details for input-validation.md

This commit is contained in:
scorchsoft
2015-08-27 11:10:47 +01:00
committed by Alexander Makarov
parent dfc8991eae
commit 2e65d0ce28

View File

@ -373,7 +373,7 @@ class MyForm extends Model
A standalone validator is a class extending [[yii\validators\Validator]] or its child class. You may implement 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 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 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, with [inline validators](#inline-validators). For example you will need to create the above validator into [[components/valdators/CountryValidator]],
```php ```php
namespace app\components; namespace app\components;
@ -391,11 +391,40 @@ class CountryValidator extends Validator
} }
``` ```
If you want your validator to support validating a value without a model, you should also override 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()]] [[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 instead of `validateAttribute()` and `validate()` because by default the latter two methods are implemented
by calling `validateValue()`. by calling `validateValue()`.
Here is an example of how you could set up the above within your model
```php
namespace app\models;
use Yii;
use yii\base\Model;
use app\components\validators\CountryValidator;
class EntryForm extends Model {
public $name;
public $email;
public $country;
public function rules(){
$r = [
[['name', 'email'], 'required'],
['country',CountryValidator::className()],
['email','email'],
];
return $r;
}
}
```
## Client-Side Validation <span id="client-side-validation"></span> ## Client-Side Validation <span id="client-side-validation"></span>