Doc improvement [skip ci]

This commit is contained in:
Qiang Xue
2015-04-12 20:52:05 -04:00
parent ba0fd22c66
commit d8848ebf25
2 changed files with 31 additions and 10 deletions

View File

@ -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. - `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> ## [[yii\validators\EmailValidator|email]] <span id="email"></span>
```php ```php

View File

@ -12,8 +12,7 @@ use Yii;
use yii\base\Model; use yii\base\Model;
/** /**
* EachValidator serves validation of the array attributes. * EachValidator validates an array by checking each of its elements against an embedded validation rule.
* It perform validation of each array element using any other validator specified by [[rule]].
* *
* ~~~php * ~~~php
* class MyModel extends Model * class MyModel extends Model
@ -23,16 +22,14 @@ use yii\base\Model;
* public function rules() * public function rules()
* { * {
* return [ * return [
* ['arrayAttribute', 'each', 'rule' => ['trim']], * // checks if every category ID is an integer
* ['arrayAttribute', 'each', 'rule' => ['integer']], * ['categoryIDs', 'each', 'rule' => ['integer']],
* ] * ]
* } * }
* } * }
* ~~~ * ~~~
* *
* Note: this validator will not work with validation declared via model inline method. If you declare inline * > Note: This validator will not work with inline validation rules.
* validation rule for attribute, you should avoid usage of this validator and iterate over array attribute
* values manually inside your code.
* *
* @property Validator $validator related validator instance. This property is read only. * @property Validator $validator related validator instance. This property is read only.
* *
@ -86,7 +83,7 @@ class EachValidator extends Validator
public function getValidator() public function getValidator()
{ {
if ($this->_validator === null) { if ($this->_validator === null) {
$this->_validator = $this->createValidators(); $this->_validator = $this->createEmbeddedValidator();
} }
return $this->_validator; return $this->_validator;
} }
@ -96,7 +93,7 @@ class EachValidator extends Validator
* @return Validator validator instance * @return Validator validator instance
* @throws InvalidConfigException if any validation rule configuration is invalid * @throws InvalidConfigException if any validation rule configuration is invalid
*/ */
private function createValidators() private function createEmbeddedValidator()
{ {
$rule = $this->rule; $rule = $this->rule;
if ($rule instanceof Validator) { if ($rule instanceof Validator) {
@ -147,4 +144,4 @@ class EachValidator extends Validator
return null; return null;
} }
} }