Browse Source

Fixes #7581: Added ability to specify range using anonymous function in `RangeValidator`

9899-cache-bug
RomeroMsk 9 years ago committed by Alexander Makarov
parent
commit
e93b797098
  1. 1
      framework/CHANGELOG.md
  2. 28
      framework/validators/RangeValidator.php

1
framework/CHANGELOG.md

@ -19,6 +19,7 @@ Yii Framework 2 Change Log
- Bug #9681: `Json::encode()` was erroring under CYGWIN (samdark)
- Bug #9714: Fixed `yii\rbac\PhpManager::updateItem()` unable to save users assignments (rezident1307)
- Bug #9754: Fixed `yii\web\Request` error when path info is empty (dynasource)
- Enh #7581: Added ability to specify range using anonymous function in `RangeValidator` (RomeroMsk)
- Enh #8613: `yii\widgets\FragmentCache` will not store empty content anymore which fixes some problems related to `yii\filters\PageCache` (kidol)
- Enh #9476: Added DI injection via controller action method signature (mdmunir)
- Enh #9635: Added default CSS class for `\yii\grid\ActionColumn` header (arogachev, dynasource)

28
framework/validators/RangeValidator.php

@ -23,7 +23,15 @@ use yii\base\InvalidConfigException;
class RangeValidator extends Validator
{
/**
* @var array list of valid values that the attribute value should be among
* @var mixed list of valid values that the attribute value should be among or an anonymous function that returns
* such list. The signature of the anonymous function should be as follows,
*
* ```php
* function foo($model, $attribute) {
* // compute range
* return $range;
* }
* ```
*/
public $range;
/**
@ -47,7 +55,7 @@ class RangeValidator extends Validator
public function init()
{
parent::init();
if (!is_array($this->range)) {
if (!is_array($this->range) && !($this->range instanceof \Closure)) {
throw new InvalidConfigException('The "range" property must be set.');
}
if ($this->message === null) {
@ -79,8 +87,24 @@ class RangeValidator extends Validator
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
if ($this->range instanceof \Closure) {
$this->range = call_user_func($this->range, $model, $attribute);
}
parent::validateAttribute($model, $attribute);
}
/**
* @inheritdoc
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->range instanceof \Closure) {
$this->range = call_user_func($this->range, $model, $attribute);
}
$range = [];
foreach ($this->range as $value) {
$range[] = (string) $value;

Loading…
Cancel
Save