mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Doc improvement [skip ci]
This commit is contained in:
@ -175,6 +175,30 @@ This validator checks if the input value is a double number. It is equivalent to
|
||||
- `min`: the lower limit (inclusive) of the value. If not set, it means the validator does not check the lower limit.
|
||||
|
||||
|
||||
## [[yii\validators\EachValidator|each]] <span id="each"></span>
|
||||
|
||||
> Info: This validator has been available since version 2.0.4.
|
||||
|
||||
```php
|
||||
[
|
||||
// checks if every category ID is an integer
|
||||
['categoryIDs', 'each', 'rule' => ['integer']],
|
||||
]
|
||||
```
|
||||
|
||||
This validator only works with an array attribute. It validates if *every* element of the array can be successfully
|
||||
validated by a specified validation rule. In the above example, the `categoryIDs` attribute must take an array value
|
||||
and each array element will be validated by the `integer` validation rule.
|
||||
|
||||
- `rule`: an array specifying a validation rule. The first element in the array specifies the class name or
|
||||
the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object.
|
||||
- `allowMessageFromRule`: whether to use the error message returned by the embedded validation rule. Defaults to true.
|
||||
If false, it will use `message` as the error message.
|
||||
|
||||
> Note: If the attribute value is not an array, it is considered validation fails and the `message` will be returned
|
||||
as the error message.
|
||||
|
||||
|
||||
## [[yii\validators\EmailValidator|email]] <span id="email"></span>
|
||||
|
||||
```php
|
||||
|
@ -12,8 +12,7 @@ use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* EachValidator serves validation of the array attributes.
|
||||
* It perform validation of each array element using any other validator specified by [[rule]].
|
||||
* EachValidator validates an array by checking each of its elements against an embedded validation rule.
|
||||
*
|
||||
* ~~~php
|
||||
* class MyModel extends Model
|
||||
@ -23,16 +22,14 @@ use yii\base\Model;
|
||||
* public function rules()
|
||||
* {
|
||||
* return [
|
||||
* ['arrayAttribute', 'each', 'rule' => ['trim']],
|
||||
* ['arrayAttribute', 'each', 'rule' => ['integer']],
|
||||
* // checks if every category ID is an integer
|
||||
* ['categoryIDs', 'each', 'rule' => ['integer']],
|
||||
* ]
|
||||
* }
|
||||
* }
|
||||
* ~~~
|
||||
*
|
||||
* Note: this validator will not work with validation declared via model inline method. If you declare inline
|
||||
* validation rule for attribute, you should avoid usage of this validator and iterate over array attribute
|
||||
* values manually inside your code.
|
||||
* > Note: This validator will not work with inline validation rules.
|
||||
*
|
||||
* @property Validator $validator related validator instance. This property is read only.
|
||||
*
|
||||
@ -86,7 +83,7 @@ class EachValidator extends Validator
|
||||
public function getValidator()
|
||||
{
|
||||
if ($this->_validator === null) {
|
||||
$this->_validator = $this->createValidators();
|
||||
$this->_validator = $this->createEmbeddedValidator();
|
||||
}
|
||||
return $this->_validator;
|
||||
}
|
||||
@ -96,7 +93,7 @@ class EachValidator extends Validator
|
||||
* @return Validator validator instance
|
||||
* @throws InvalidConfigException if any validation rule configuration is invalid
|
||||
*/
|
||||
private function createValidators()
|
||||
private function createEmbeddedValidator()
|
||||
{
|
||||
$rule = $this->rule;
|
||||
if ($rule instanceof Validator) {
|
||||
@ -147,4 +144,4 @@ class EachValidator extends Validator
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user