mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 22:30:27 +08:00
More details for input-validation.md
This commit is contained in:
@ -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
|
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 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
|
```php
|
||||||
namespace app\components;
|
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
|
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
|
Below is an example of how you could use the above validator class within your model.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
namespace app\models;
|
namespace app\models;
|
||||||
|
|
||||||
@ -406,22 +408,20 @@ use Yii;
|
|||||||
use yii\base\Model;
|
use yii\base\Model;
|
||||||
use app\components\validators\CountryValidator;
|
use app\components\validators\CountryValidator;
|
||||||
|
|
||||||
class EntryForm extends Model {
|
class EntryForm extends Model
|
||||||
|
{
|
||||||
public $name;
|
public $name;
|
||||||
public $email;
|
public $email;
|
||||||
public $country;
|
public $country;
|
||||||
|
|
||||||
public function rules(){
|
public function rules()
|
||||||
|
{
|
||||||
$r = [
|
return [
|
||||||
[['name', 'email'], 'required'],
|
[['name', 'email'], 'required'],
|
||||||
['country',CountryValidator::className()],
|
['country', CountryValidator::className()],
|
||||||
['email','email'],
|
['email', 'email'],
|
||||||
];
|
];
|
||||||
return $r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user