mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Remove user-defined optional values
Since we have NotOptional rule now, we must keep the same opposite behaviour between these two rules. This commit also improve and increase tests from Optional rule and updates the documentation. No tests was made for messages on MODE_DEFAULT because it's not possible for a while since we don't have such much granularity control, but after some fixes on Not rule, maybe we will be able to do that, that's why I kept the messages on OptionalException.
This commit is contained in:
parent
15b0a937b2
commit
d53811f7b7
|
@ -1,28 +1,21 @@
|
|||
# Optional
|
||||
|
||||
- `v::optional(v $rule)`
|
||||
- `v::optional(v $rule, array $optionalValues)`
|
||||
|
||||
Validates if the given input is optional or not. By _optional_ you may interpret
|
||||
as `null` or an empty string (`''`).
|
||||
Validates if the given input is optional or not. By _optional_ we consider `null`
|
||||
or an empty string (`''`).
|
||||
|
||||
```php
|
||||
v::optional(v::alpha())->validate(''); // true
|
||||
v::optional(v::digit())->validate(null); // true
|
||||
```
|
||||
|
||||
Also you can defined what values you want as optional values:
|
||||
|
||||
```php
|
||||
v::optional(v::alpha(), array(null))->validate(''); // false
|
||||
```
|
||||
|
||||
The example bellow returns false because only `null` is accepted as optional
|
||||
value, and `''` is not.
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NoWhitespace](NoWhitespace.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NotOptional](NotOptional.md)
|
||||
* [NullType](NullType.md)
|
||||
|
|
|
@ -72,7 +72,7 @@ v::optional(v::alpha())->validate(''); // true
|
|||
v::optional(v::alpha())->validate(null); // true
|
||||
```
|
||||
|
||||
By _optional_ you may interpret as `null` or an empty string (`''`).
|
||||
By _optional_ we consider `null` or an empty string (`''`).
|
||||
|
||||
See more on [Optional](Optional.md).
|
||||
|
||||
|
|
|
@ -15,14 +15,15 @@ class OptionalException extends ValidationException
|
|||
{
|
||||
const STANDARD = 0;
|
||||
const NAMED = 1;
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => 'The value must be optional',
|
||||
self::NAMED => '{{name}} must be optional',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => 'The value is required',
|
||||
self::NAMED => '{{name}} is required',
|
||||
self::STANDARD => 'The value must not be optional',
|
||||
self::NAMED => '{{name}} must not be optional',
|
||||
),
|
||||
);
|
||||
|
||||
|
|
|
@ -12,21 +12,17 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Optional extends AbstractWrapper
|
||||
{
|
||||
public $optionalValues;
|
||||
|
||||
public function __construct(Validatable $rule, array $optionalValues = array(null, ''))
|
||||
public function __construct(Validatable $rule)
|
||||
{
|
||||
$this->validatable = $rule;
|
||||
$this->optionalValues = $optionalValues;
|
||||
}
|
||||
|
||||
private function isOptional($input)
|
||||
{
|
||||
return in_array($input, $this->optionalValues, true);
|
||||
return in_array($input, array(null, ''), true);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
|
|
|
@ -96,7 +96,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator objectType()
|
||||
* @method static Validator odd()
|
||||
* @method static Validator oneOf()
|
||||
* @method static Validator optional(Validatable $rule, array $optionalValues = array(null, ''))
|
||||
* @method static Validator optional(Validatable $rule)
|
||||
* @method static Validator perfectSquare()
|
||||
* @method static Validator phone()
|
||||
* @method static Validator positive()
|
||||
|
|
18
tests/integration/optional_1.phpt
Normal file
18
tests/integration/optional_1.phpt
Normal file
|
@ -0,0 +1,18 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
var_dump(
|
||||
v::alpha()->validate(''),
|
||||
v::alpha()->validate(null),
|
||||
v::optional(v::alpha())->validate(''),
|
||||
v::optional(v::alpha())->validate(null)
|
||||
);
|
||||
?>
|
||||
--EXPECTF--
|
||||
bool(false)
|
||||
bool(false)
|
||||
bool(true)
|
||||
bool(true)
|
16
tests/integration/optional_2.phpt
Normal file
16
tests/integration/optional_2.phpt
Normal file
|
@ -0,0 +1,16 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\OptionalException;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::not(v::optional(v::equals('foo')))->check(null);
|
||||
} catch (OptionalException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
null must not be optional
|
16
tests/integration/optional_3.phpt
Normal file
16
tests/integration/optional_3.phpt
Normal file
|
@ -0,0 +1,16 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\OptionalException;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::not(v::optional(v::equals('foo'))->setName('My field'))->check(null);
|
||||
} catch (OptionalException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
My field must not be optional
|
|
@ -11,13 +11,43 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\Optional
|
||||
* @covers Respect\Validation\Exceptions\OptionalException
|
||||
*/
|
||||
class OptionalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function providerForOptional()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(''),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotOptional()
|
||||
{
|
||||
return array(
|
||||
array(1),
|
||||
array(array()),
|
||||
array(' '),
|
||||
array(0),
|
||||
array('0'),
|
||||
array(0),
|
||||
array('0.0'),
|
||||
array(false),
|
||||
array(array('')),
|
||||
array(array(' ')),
|
||||
array(array(0)),
|
||||
array(array('0')),
|
||||
array(array(false)),
|
||||
array(array(array(''), array(0))),
|
||||
array(new stdClass()),
|
||||
);
|
||||
}
|
||||
|
||||
public function testShouldAcceptInstanceOfValidatobleOnConstructor()
|
||||
{
|
||||
$validatable = $this->getMock('Respect\\Validation\\Validatable');
|
||||
|
@ -26,25 +56,10 @@ class OptionalTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertSame($validatable, $rule->getValidatable());
|
||||
}
|
||||
|
||||
public function testShouldAcceptOptionalValuesOnConstructor()
|
||||
{
|
||||
$validatable = $this->getMock('Respect\\Validation\\Validatable');
|
||||
$optionalValues = array(null, '', ' ', '0');
|
||||
$rule = new Optional($validatable, $optionalValues);
|
||||
|
||||
$this->assertSame($optionalValues, $rule->optionalValues);
|
||||
}
|
||||
|
||||
public function testShouldHaveDefaultOptionalValues()
|
||||
{
|
||||
$validatable = $this->getMock('Respect\\Validation\\Validatable');
|
||||
$expectedOptionalValues = array(null, '');
|
||||
$rule = new Optional($validatable);
|
||||
|
||||
$this->assertSame($expectedOptionalValues, $rule->optionalValues);
|
||||
}
|
||||
|
||||
public function testShouldNotValidateRuleWhenInputIsOptional()
|
||||
/**
|
||||
* @dataProvider providerForOptional
|
||||
*/
|
||||
public function testShouldNotValidateRuleWhenInputIsOptional($input)
|
||||
{
|
||||
$validatable = $this->getMock('Respect\\Validation\\Validatable');
|
||||
$validatable
|
||||
|
@ -53,13 +68,14 @@ class OptionalTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
$rule = new Optional($validatable);
|
||||
|
||||
$this->assertTrue($rule->validate(''));
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
public function testShouldValidateRuleWhenInputIsNotOptional()
|
||||
/**
|
||||
* @dataProvider providerForNotOptional
|
||||
*/
|
||||
public function testShouldValidateRuleWhenInputIsNotOptional($input)
|
||||
{
|
||||
$input = 'foo';
|
||||
|
||||
$validatable = $this->getMock('Respect\\Validation\\Validatable');
|
||||
$validatable
|
||||
->expects($this->once())
|
||||
|
|
Loading…
Reference in a new issue