You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. use yii\base\InvalidConfigException;
  9. use Yii;
  10. use yii\base\Model;
  11. /**
  12. * EachValidator validates an array by checking each of its elements against an embedded validation rule.
  13. *
  14. * ```php
  15. * class MyModel extends Model
  16. * {
  17. * public $categoryIDs = [];
  18. *
  19. * public function rules()
  20. * {
  21. * return [
  22. * // checks if every category ID is an integer
  23. * ['categoryIDs', 'each', 'rule' => ['integer']],
  24. * ]
  25. * }
  26. * }
  27. * ```
  28. *
  29. * > Note: This validator will not work with inline validation rules in case of usage outside the model scope,
  30. * e.g. via [[validate()]] method.
  31. *
  32. * > Note: EachValidator is meant to be used only in basic cases, you should consider usage of tabular input,
  33. * using several models for the more complex case.
  34. *
  35. * @author Paul Klimov <klimov.paul@gmail.com>
  36. * @since 2.0.4
  37. */
  38. class EachValidator extends Validator
  39. {
  40. /**
  41. * @var array|Validator definition of the validation rule, which should be used on array values.
  42. * It should be specified in the same format as at [[yii\base\Model::rules()]], except it should not
  43. * contain attribute list as the first element.
  44. * For example:
  45. *
  46. * ```php
  47. * ['integer']
  48. * ['match', 'pattern' => '/[a-z]/is']
  49. * ```
  50. *
  51. * Please refer to [[yii\base\Model::rules()]] for more details.
  52. */
  53. public $rule;
  54. /**
  55. * @var boolean whether to use error message composed by validator declared via [[rule]] if its validation fails.
  56. * If enabled, error message specified for this validator itself will appear only if attribute value is not an array.
  57. * If disabled, own error message value will be used always.
  58. */
  59. public $allowMessageFromRule = true;
  60. /**
  61. * @var Validator validator instance.
  62. */
  63. private $_validator;
  64. /**
  65. * @inheritdoc
  66. */
  67. public function init()
  68. {
  69. parent::init();
  70. if ($this->message === null) {
  71. $this->message = Yii::t('yii', '{attribute} is invalid.');
  72. }
  73. }
  74. /**
  75. * Returns the validator declared in [[rule]].
  76. * @param Model|null $model model in which context validator should be created.
  77. * @return Validator the declared validator.
  78. */
  79. private function getValidator($model = null)
  80. {
  81. if ($this->_validator === null) {
  82. $this->_validator = $this->createEmbeddedValidator($model);
  83. }
  84. return $this->_validator;
  85. }
  86. /**
  87. * Creates validator object based on the validation rule specified in [[rule]].
  88. * @param Model|null $model model in which context validator should be created.
  89. * @throws \yii\base\InvalidConfigException
  90. * @return Validator validator instance
  91. */
  92. private function createEmbeddedValidator($model)
  93. {
  94. $rule = $this->rule;
  95. if ($rule instanceof Validator) {
  96. return $rule;
  97. } elseif (is_array($rule) && isset($rule[0])) { // validator type
  98. if (!is_object($model)) {
  99. $model = new Model(); // mock up context model
  100. }
  101. return Validator::createValidator($rule[0], $model, $this->attributes, array_slice($rule, 1));
  102. } else {
  103. throw new InvalidConfigException('Invalid validation rule: a rule must be an array specifying validator type.');
  104. }
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function validateAttribute($model, $attribute)
  110. {
  111. $value = $model->$attribute;
  112. if (!is_array($value)) {
  113. $this->addError($model, $attribute, $this->message, []);
  114. return;
  115. }
  116. $validator = $this->getValidator($model); // ensure model context while validator creation
  117. $originalErrors = $model->getErrors($attribute);
  118. $filteredValue = [];
  119. foreach ($value as $k => $v) {
  120. $model->$attribute = $v;
  121. if (!$validator->skipOnEmpty || !$validator->isEmpty($v)) {
  122. $validator->validateAttribute($model, $attribute);
  123. }
  124. $filteredValue[$k] = $model->$attribute;
  125. if ($model->hasErrors($attribute)) {
  126. $validationErrors = $model->getErrors($attribute);
  127. $model->clearErrors($attribute);
  128. if (!empty($originalErrors)) {
  129. $model->addErrors([$attribute => $originalErrors]);
  130. }
  131. if ($this->allowMessageFromRule) {
  132. $model->addErrors([$attribute => $validationErrors]);
  133. } else {
  134. $this->addError($model, $attribute, $this->message, ['value' => $v]);
  135. }
  136. $model->$attribute = $value;
  137. return;
  138. }
  139. }
  140. $model->$attribute = $filteredValue;
  141. }
  142. /**
  143. * @inheritdoc
  144. */
  145. protected function validateValue($value)
  146. {
  147. if (!is_array($value)) {
  148. return [$this->message, []];
  149. }
  150. $validator = $this->getValidator();
  151. foreach ($value as $v) {
  152. if ($validator->skipOnEmpty && $validator->isEmpty($v)) {
  153. continue;
  154. }
  155. $result = $validator->validateValue($v);
  156. if ($result !== null) {
  157. if ($this->allowMessageFromRule) {
  158. $result[1]['value'] = $v;
  159. return $result;
  160. } else {
  161. return [$this->message, ['value' => $v]];
  162. }
  163. }
  164. }
  165. return null;
  166. }
  167. }