"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "framework/validators/EachValidator.php" between
yii2-2.0.35.tar.gz and yii2-2.0.36.tar.gz

About: Yii 2 is a high-performance component-based PHP framework for developing large-scale Web applications (source).

EachValidator.php  (yii2-2.0.35):EachValidator.php  (yii2-2.0.36)
skipping to change at line 75 skipping to change at line 75
* @var bool whether to stop validation once first error among attribute val ue elements is detected. * @var bool whether to stop validation once first error among attribute val ue elements is detected.
* When enabled validation will produce single error message on attribute, w hen disabled - multiple * When enabled validation will produce single error message on attribute, w hen disabled - multiple
* error messages mya appear: one per each invalid value. * error messages mya appear: one per each invalid value.
* Note that this option will affect only [[validateAttribute()]] value, whi le [[validateValue()]] will * Note that this option will affect only [[validateAttribute()]] value, whi le [[validateValue()]] will
* not be affected. * not be affected.
* @since 2.0.11 * @since 2.0.11
*/ */
public $stopOnFirstError = true; public $stopOnFirstError = true;
/** /**
* @var Validator validator instance.
*/
private $_validator;
/**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function init() public function init()
{ {
parent::init(); parent::init();
if ($this->message === null) { if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} is invalid.'); $this->message = Yii::t('yii', '{attribute} is invalid.');
} }
} }
/** /**
* Returns the validator declared in [[rule]].
* @param Model|null $model model in which context validator should be creat
ed.
* @return Validator the declared validator.
*/
private function getValidator($model = null)
{
if ($this->_validator === null) {
$this->_validator = $this->createEmbeddedValidator($model);
}
return $this->_validator;
}
/**
* Creates validator object based on the validation rule specified in [[rule ]]. * Creates validator object based on the validation rule specified in [[rule ]].
* @param Model|null $model model in which context validator should be creat ed. * @param Model|null $model model in which context validator should be creat ed.
* @param mixed|null $current value being currently validated.
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
* @return Validator validator instance * @return Validator validator instance
*/ */
private function createEmbeddedValidator($model) private function createEmbeddedValidator($model = null, $current = null)
{ {
$rule = $this->rule; $rule = $this->rule;
if ($rule instanceof Validator) { if ($rule instanceof Validator) {
return $rule; return $rule;
} elseif (is_array($rule) && isset($rule[0])) { // validator type }
if (is_array($rule) && isset($rule[0])) { // validator type
if (!is_object($model)) { if (!is_object($model)) {
$model = new Model(); // mock up context model $model = new Model(); // mock up context model
} }
return Validator::createValidator($rule[0], $model, $this->attribute $params = array_slice($rule, 1);
s, array_slice($rule, 1)); $params['current'] = $current;
return Validator::createValidator($rule[0], $model, $this->attribute
s, $params);
} }
throw new InvalidConfigException('Invalid validation rule: a rule must b e an array specifying validator type.'); throw new InvalidConfigException('Invalid validation rule: a rule must b e an array specifying validator type.');
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateAttribute($model, $attribute) public function validateAttribute($model, $attribute)
{ {
$arrayOfValues = $model->$attribute; $arrayOfValues = $model->$attribute;
if (!is_array($arrayOfValues) && !$arrayOfValues instanceof \ArrayAccess ) { if (!is_array($arrayOfValues) && !$arrayOfValues instanceof \ArrayAccess ) {
$this->addError($model, $attribute, $this->message, []); $this->addError($model, $attribute, $this->message, []);
return; return;
} }
$dynamicModel = new DynamicModel($model->getAttributes());
$dynamicModel->addRule($attribute, $this->getValidator($model));
$dynamicModel->setAttributeLabels($model->attributeLabels());
foreach ($arrayOfValues as $k => $v) { foreach ($arrayOfValues as $k => $v) {
$dynamicModel = new DynamicModel($model->getAttributes());
$dynamicModel->setAttributeLabels($model->attributeLabels());
$dynamicModel->addRule($attribute, $this->createEmbeddedValidator($m
odel, $v));
$dynamicModel->defineAttribute($attribute, $v); $dynamicModel->defineAttribute($attribute, $v);
$dynamicModel->validate(); $dynamicModel->validate();
$arrayOfValues[$k] = $dynamicModel->$attribute; // filtered values l ike 'trim' $arrayOfValues[$k] = $dynamicModel->$attribute; // filtered values l ike 'trim'
if (!$dynamicModel->hasErrors($attribute)) { if (!$dynamicModel->hasErrors($attribute)) {
continue; continue;
} }
if ($this->allowMessageFromRule) { if ($this->allowMessageFromRule) {
skipping to change at line 175 skipping to change at line 160
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function validateValue($value) protected function validateValue($value)
{ {
if (!is_array($value) && !$value instanceof \ArrayAccess) { if (!is_array($value) && !$value instanceof \ArrayAccess) {
return [$this->message, []]; return [$this->message, []];
} }
$validator = $this->getValidator(); $validator = $this->createEmbeddedValidator();
foreach ($value as $v) { foreach ($value as $v) {
if ($validator->skipOnEmpty && $validator->isEmpty($v)) { if ($validator->skipOnEmpty && $validator->isEmpty($v)) {
continue; continue;
} }
$result = $validator->validateValue($v); $result = $validator->validateValue($v);
if ($result !== null) { if ($result !== null) {
if ($this->allowMessageFromRule) { if ($this->allowMessageFromRule) {
$result[1]['value'] = $v; $result[1]['value'] = $v;
return $result; return $result;
} }
 End of changes. 9 change blocks. 
29 lines changed or deleted 14 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)