mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +02:00
Create "NotBlank" rule
This commit is contained in:
parent
b28e97c70f
commit
880cdb5f09
|
@ -17,6 +17,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
|
|||
- Create "KeySet" rule (#374)
|
||||
- Create "KeyValue" rule (#441)
|
||||
- Create "Mimetype" rule (#361)
|
||||
- Create "NotBlank" rule (#443)
|
||||
- Create "Optional" rule (#423)
|
||||
- Create "ResourceType" rule (#397)
|
||||
- Create "ScalarVal" rule (#397)
|
||||
|
|
34
docs/NotBlank.md
Normal file
34
docs/NotBlank.md
Normal file
|
@ -0,0 +1,34 @@
|
|||
# NotBlank
|
||||
|
||||
- `v::notBlank()`
|
||||
|
||||
Validates if the given input is not a blank value (`null`, zeros, empty strings
|
||||
or empty arrays, recursively).
|
||||
|
||||
```php
|
||||
v::notBlank()->validate(null); // false
|
||||
v::notBlank()->validate(''); // false
|
||||
v::notBlank()->validate(array()); // false
|
||||
v::notBlank()->validate(' '); // false
|
||||
v::notBlank()->validate(0); // false
|
||||
v::notBlank()->validate('0'); // false
|
||||
v::notBlank()->validate(0); // false
|
||||
v::notBlank()->validate('0.0'); // false
|
||||
v::notBlank()->validate(false); // false
|
||||
v::notBlank()->validate(array('')); // false
|
||||
v::notBlank()->validate(array(' ')); // false
|
||||
v::notBlank()->validate(array(0)); // false
|
||||
v::notBlank()->validate(array('0')); // false
|
||||
v::notBlank()->validate(array(false)); // false
|
||||
v::notBlank()->validate(array(array(''), array(0))); // false
|
||||
v::notBlank()->validate(new stdClass()); // false
|
||||
```
|
||||
|
||||
It's similar to [NotEmpty](NotEmpty.md) but it's way more strict.
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [NoWhitespace](NoWhitespace.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NullType](NullType.md)
|
|
@ -165,6 +165,7 @@
|
|||
* [Json](Json.md)
|
||||
* [MacAddress](MacAddress.md)
|
||||
* [NfeAccessKey](NfeAccessKey.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [Phone](Phone.md)
|
||||
* [Sf](Sf.md)
|
||||
* [Url](Url.md)
|
||||
|
@ -249,6 +250,7 @@
|
|||
* [NoWhitespace](NoWhitespace.md)
|
||||
* [NoneOf](NoneOf.md)
|
||||
* [Not](Not.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NullType](NullType.md)
|
||||
* [Numeric](Numeric.md)
|
||||
|
|
34
library/Exceptions/NotBlankException.php
Normal file
34
library/Exceptions/NotBlankException.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotBlankException extends ValidationException
|
||||
{
|
||||
const STANDARD = 0;
|
||||
const NAMED = 1;
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => 'The value must not be blank',
|
||||
self::NAMED => '{{name}} must not be blank',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => 'The value must be blank',
|
||||
self::NAMED => '{{name}} must be blank',
|
||||
),
|
||||
);
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
return $this->getName() == '' ? static::STANDARD : static::NAMED;
|
||||
}
|
||||
}
|
38
library/Rules/NotBlank.php
Normal file
38
library/Rules/NotBlank.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use stdClass;
|
||||
|
||||
class NotBlank extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
{
|
||||
if (is_numeric($input)) {
|
||||
return $input != 0;
|
||||
}
|
||||
|
||||
if (is_string($input)) {
|
||||
$input = trim($input);
|
||||
}
|
||||
|
||||
if ($input instanceof stdClass) {
|
||||
$input = (array) $input;
|
||||
}
|
||||
|
||||
if (is_array($input)) {
|
||||
$input = array_filter($input, __METHOD__);
|
||||
}
|
||||
|
||||
return !empty($input);
|
||||
}
|
||||
}
|
|
@ -87,6 +87,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator no($useLocale = false)
|
||||
* @method static Validator noneOf()
|
||||
* @method static Validator not(Validatable $rule)
|
||||
* @method static Validator notBlank()
|
||||
* @method static Validator notEmpty()
|
||||
* @method static Validator noWhitespace()
|
||||
* @method static Validator nullType()
|
||||
|
|
99
tests/unit/Rules/NotBlankTest.php
Normal file
99
tests/unit/Rules/NotBlankTest.php
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\NotBlank
|
||||
* @covers Respect\Validation\Exceptions\NotBlankException
|
||||
*/
|
||||
class NotBlankTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForNotBlank
|
||||
*/
|
||||
public function testShouldValidateWhenNotBlank($input)
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForBlank
|
||||
*/
|
||||
public function testShouldNotValidateWhenBlank($input)
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\NotBlankException
|
||||
* @expectedExceptionMessage The value must not be blank
|
||||
*/
|
||||
public function testShouldThrowExceptionWhenFailure()
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
$rule->check(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\NotBlankException
|
||||
* @expectedExceptionMessage whatever must not be blank
|
||||
*/
|
||||
public function testShouldThrowExceptionWhenFailureAndDoesHaveAName()
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
$rule->setName('whatever');
|
||||
$rule->check(0);
|
||||
}
|
||||
|
||||
public function providerForNotBlank()
|
||||
{
|
||||
$object = new stdClass();
|
||||
$object->foo = true;
|
||||
|
||||
return array(
|
||||
array(1),
|
||||
array(' oi'),
|
||||
array(array(5)),
|
||||
array(array(1)),
|
||||
array($object),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForBlank()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(''),
|
||||
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()),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue