Skip to content

Commit

Permalink
Merge pull request #15772 from BeMySlaveDarlin/issue/15515-fix-allow-…
Browse files Browse the repository at this point in the history
…empty-validation

T15515 Fix allow empty validation
  • Loading branch information
niden authored Nov 7, 2021
2 parents cfcb101 + 371e8d0 commit 25999c6
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
- Added `Phalcon\Forms\Form::getFilteredValue()` to get filtered value without providing entity [#15438](https://github.com/phalcon/cphalcon/issues/15438)
- Added `Phalcon\Encryption\Security::getHashInformation()` to return information for a hash [#15731](https://github.com/phalcon/cphalcon/issues/15731)
- Added constants `Phalcon\Encryption\Security::CRYPT_ARGON2I` and `Phalcon\Encryption\Security::CRYPT_ARGON2ID` [#15731](https://github.com/phalcon/cphalcon/issues/15731)
- Added `allowEmpty` checks to common validators [#15515](https://github.com/phalcon/cphalcon/issues/15515)

## Fixed
- Fixed `Query::getExpression()` return type [#15553](https://github.com/phalcon/cphalcon/issues/15553)
Expand Down
21 changes: 21 additions & 0 deletions phalcon/Validation/AbstractValidator.zep
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ abstract class AbstractValidator implements ValidatorInterface
return label;
}

/**
* Checks if field can be empty.
*
* @param mixed field
* @param mixed value
*
* @return bool
*/
protected function allowEmpty(var field, var value) -> bool
{
var allowEmpty;

let allowEmpty = this->getOption("allowEmpty", false);

if typeof allowEmpty === "array" {
let allowEmpty = isset allowEmpty[field] ? allowEmpty[field] : false;
}

return allowEmpty && empty value;
}

/**
* Create a default message by factory
*
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Alnum.zep
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Alnum extends AbstractValidator
var value;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if !ctype_alnum(value) {
validation->appendMessage(
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Alpha.zep
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class Alpha extends AbstractValidator
var value;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if preg_match("/[^[:alpha:]]/imu", value) {
validation->appendMessage(
Expand Down
4 changes: 4 additions & 0 deletions phalcon/Validation/Validator/Between.zep
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class Between extends AbstractValidator
minimum = this->getOption("minimum"),
maximum = this->getOption("maximum");

if this->allowEmpty(field, value) {
return true;
}

if typeof minimum == "array" {
let minimum = minimum[field];
}
Expand Down
3 changes: 1 addition & 2 deletions phalcon/Validation/Validator/Callback.zep
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ class Callback extends AbstractValidator
* @param array options = [
* 'message' => '',
* 'template' => '',
* 'callback' => null,
* 'allowEmpty' => false
* 'callback' => null
* ]
*/
public function __construct(array! options = [])
Expand Down
3 changes: 1 addition & 2 deletions phalcon/Validation/Validator/Confirmation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class Confirmation extends AbstractValidator
* 'template' => '',
* 'with' => '',
* 'labelWith' => '',
* 'ignoreCase' => false,
* 'allowEmpty' => false
* 'ignoreCase' => false
* ]
*/
public function __construct(array! options = [])
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/CreditCard.zep
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class CreditCard extends AbstractValidator
var value, valid;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

let valid = this->verifyByLuhnAlgorithm(value);

Expand Down
5 changes: 4 additions & 1 deletion phalcon/Validation/Validator/Date.zep
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ class Date extends AbstractValidator
var value, format;

let value = validation->getValue(field);
let format = this->getOption("format");
if this->allowEmpty(field, value) {
return true;
}

let format = this->getOption("format");
if typeof format == "array" {
let format = format[field];
}
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Digit.zep
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class Digit extends AbstractValidator
public function validate(<Validation> validation, var field) -> bool
{
var value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if is_int(value) || ctype_digit(value) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Email.zep
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class Email extends AbstractValidator
public function validate(<Validation> validation, var field) -> bool
{
var value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if !filter_var(value, FILTER_VALIDATE_EMAIL) {
validation->appendMessage(
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/ExclusionIn.zep
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class ExclusionIn extends AbstractValidator
var value, domain, replacePairs, strict, fieldDomain;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

/**
* A domain is an array with a list of valid values
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Identical.zep
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Identical extends AbstractValidator
bool valid;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if this->hasOption("accepted") {
let accepted = this->getOption("accepted");
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/InclusionIn.zep
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class InclusionIn extends AbstractValidator
var value, domain, replacePairs, strict, fieldDomain;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

/**
* A domain is an array with a list of valid values
Expand Down
21 changes: 6 additions & 15 deletions phalcon/Validation/Validator/Ip.zep
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,28 @@ class Ip extends AbstractValidator
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, version, allowPrivate, allowReserved, allowEmpty, options;
var value, version, allowPrivate, allowReserved, options;

let value = validation->getValue(field),
version = this->getOption("version", FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

let version = this->getOption("version", FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
if typeof version == "array" {
let version = version[field];
}

let allowPrivate = this->getOption("allowPrivate") ? 0 : FILTER_FLAG_NO_PRIV_RANGE;

if typeof allowPrivate == "array" {
let allowPrivate = allowPrivate[field];
}

let allowReserved = this->getOption("allowReserved") ? 0 : FILTER_FLAG_NO_RES_RANGE;

if typeof allowReserved == "array" {
let allowReserved = allowReserved[field];
}

let allowEmpty = this->getOption("allowEmpty", false);

if typeof allowEmpty == "array" {
let allowEmpty = isset allowEmpty[field] ? allowEmpty[field] : false;
}

if allowEmpty && empty value {
return true;
}

let options = [
"options": [
"default": false
Expand Down
4 changes: 4 additions & 0 deletions phalcon/Validation/Validator/Numericality.zep
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class Numericality extends AbstractValidator
value = str_replace(" ", "", value),
pattern = "/((^[-]?[0-9,]+(.[0-9]+)?$)|(^[-]?[0-9.]+(,[0-9]+)?$))/";

if this->allowEmpty(field, value) {
return true;
}

if !preg_match(pattern, value) {
validation->appendMessage(
this->messageFactory(validation, field)
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/PresenceOf.zep
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class PresenceOf extends AbstractValidator
public function validate(<Validation> validation, var field) -> bool
{
var value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if value === null || value === "" {
validation->appendMessage(
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Regex.zep
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Regex extends AbstractValidator
// Check if the value match using preg_match in the PHP userland
let matches = null;
let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

let pattern = this->getOption("pattern");

Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/StringLength/Max.zep
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class Max extends AbstractValidator
var value, length, maximum, replacePairs, included, result;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

// Check if mbstring is available to calculate the correct length
if function_exists("mb_strlen") {
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/StringLength/Min.zep
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class Min extends AbstractValidator
var value, length, minimum, replacePairs, included, result;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

// Check if mbstring is available to calculate the correct length
if function_exists("mb_strlen") {
Expand Down
3 changes: 3 additions & 0 deletions phalcon/Validation/Validator/Url.zep
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class Url extends AbstractValidator
var options, result, value;

let value = validation->getValue(field);
if this->allowEmpty(field, value) {
return true;
}

if fetch options, this->options["options"] {
let result = filter_var(value, FILTER_VALIDATE_URL, options);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/Mvc/Router/HandleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function mvcRouterHandleShortSyntax(IntegrationTester $I)
);

$I->assertEquals(
'about',
'About',
$router->getControllerName()
);

Expand Down Expand Up @@ -299,7 +299,7 @@ public function mvcRouterHandleShortSyntax(IntegrationTester $I)
);

$I->assertEquals(
'about',
'About',
$router->getControllerName()
);

Expand Down
52 changes: 52 additions & 0 deletions tests/integration/Validation/AllowEmptyCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Tests\Integration\Validation;

use IntegrationTester;
use Phalcon\Messages\Messages;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Alpha;

class AllowEmptyCest
{
/**
* Tests Phalcon\Validation :: allowEmpty()
*
* @author Phalcon Team <[email protected]>
* @since 2021-11-07
*/
public function validationAllowEmptyFalse(IntegrationTester $I)
{
$I->wantToTest('Validation - allowEmpty() - false');

$data = ['name' => ''];
$validation = new Validation();
$validator = new Alpha(['allowEmpty' => false]);
$validation->add('name', $validator);

$I->assertInstanceOf(Messages::class, $validation->validate($data));
}

public function validationAllowEmptyTrue(IntegrationTester $I)
{
$I->wantToTest('Validation - allowEmpty() - true');

$data = ['name' => ''];
$validation = new Validation();
$validator = new Alpha(['allowEmpty' => true]);
$validation->add('name', $validator);

$I->assertTrue($validation->validate($data));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function validationValidatorCallbackValidateSingleFieldBoolean(Integratio
return empty($data['admin']);
},
'message' => 'You cant provide both admin and user.',
'allowEmpty' => true,
]
)
);
Expand Down

0 comments on commit 25999c6

Please sign in to comment.