mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Merge pull request #3549 from creocoder/range-validator-enh
Improve `in` validator to properly support array attributes.
This commit is contained in:
@ -61,6 +61,7 @@ Yii Framework 2 Change Log
|
||||
- Enh: Added support for array attributes in `exist` validator (creocoder)
|
||||
- Enh: Added support for using path alias with `FileDependency::fileName` (qiangxue)
|
||||
- Enh: Added param `hideOnSinglePage` to `yii\widgets\LinkPager` (arturf)
|
||||
- Enh: Added support for array attributes in `in` validator (creocoder)
|
||||
- Chg #2913: RBAC `DbManager` is now initialized via migration (samdark)
|
||||
- Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue)
|
||||
- Chg #3175: InvalidCallException, InvalidParamException, UnknownMethodException are now extended from SPL BadMethodCallException (samdark)
|
||||
|
||||
@ -35,6 +35,10 @@ class RangeValidator extends Validator
|
||||
* the attribute value should NOT be among the list of values defined via [[range]].
|
||||
*/
|
||||
public $not = false;
|
||||
/**
|
||||
* @var boolean whether to allow array type attribute.
|
||||
*/
|
||||
public $allowArray = false;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
@ -55,8 +59,20 @@ class RangeValidator extends Validator
|
||||
*/
|
||||
protected function validateValue($value)
|
||||
{
|
||||
$valid = !$this->not && in_array($value, $this->range, $this->strict)
|
||||
|| $this->not && !in_array($value, $this->range, $this->strict);
|
||||
if (!$this->allowArray && is_array($value)) {
|
||||
return [$this->message, []];
|
||||
}
|
||||
|
||||
$valid = false;
|
||||
|
||||
foreach ((array)$value as $v) {
|
||||
$valid = !$this->not && in_array($v, $this->range, $this->strict)
|
||||
|| $this->not && !in_array($v, $this->range, $this->strict);
|
||||
|
||||
if (!$valid) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $valid ? null : [$this->message, []];
|
||||
}
|
||||
|
||||
@ -41,6 +41,17 @@ class RangeValidatorTest extends TestCase
|
||||
$this->assertTrue($val->validate("5"));
|
||||
}
|
||||
|
||||
public function testValidateArrayValue()
|
||||
{
|
||||
$val = new RangeValidator(['range' => range(1, 10, 1)]);
|
||||
$val->allowArray = true;
|
||||
$this->assertTrue($val->validate([1, 2, 3, 4, 5]));
|
||||
$this->assertTrue($val->validate([6, 7, 8, 9, 10]));
|
||||
$this->assertFalse($val->validate([0, 1, 2]));
|
||||
$this->assertFalse($val->validate([10, 11, 12]));
|
||||
$this->assertTrue($val->validate(["1", "2", "3", 4, 5, 6]));
|
||||
}
|
||||
|
||||
public function testValidateValueStrict()
|
||||
{
|
||||
$val = new RangeValidator(['range' => range(1, 10, 1), 'strict' => true]);
|
||||
@ -52,6 +63,14 @@ class RangeValidatorTest extends TestCase
|
||||
$this->assertFalse($val->validate("5.5"));
|
||||
}
|
||||
|
||||
public function testValidateArrayValueStrict()
|
||||
{
|
||||
$val = new RangeValidator(['range' => range(1, 10, 1), 'strict' => true]);
|
||||
$val->allowArray = true;
|
||||
$this->assertFalse($val->validate(["1", "2", "3", "4", "5", "6"]));
|
||||
$this->assertFalse($val->validate(["1", "2", "3", 4, 5, 6]));
|
||||
}
|
||||
|
||||
public function testValidateValueNot()
|
||||
{
|
||||
$val = new RangeValidator(['range' => range(1, 10, 1), 'not' => true]);
|
||||
|
||||
Reference in New Issue
Block a user