Skip to content

Commit

Permalink
phalcon#15515 - Added allowEmpty checks in common validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Muzafarov committed Nov 7, 2021
1 parent 0ff5b6f commit c9896e8
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 18 deletions.
18 changes: 18 additions & 0 deletions phalcon/Validation/AbstractValidator.zep
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ abstract class AbstractValidator implements ValidatorInterface
return label;
}

/**
* Checks if field can be empty.
*
* @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: 3 additions & 0 deletions phalcon/Validation/Validator/Callback.zep
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class Callback extends AbstractValidator
}

let returnedValue = call_user_func(callback, data);
if this->allowEmpty(field, returnedValue) {
return true;
}

if typeof returnedValue == "boolean" {
if !returnedValue {
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

0 comments on commit c9896e8

Please sign in to comment.