Skip to content

Commit

Permalink
Merge branch 'T13208-validation-messages' of https://github.com/emili…
Browse files Browse the repository at this point in the history
…odeg/cphalcon into emiliodeg-T13208-validation-messages

* 'T13208-validation-messages' of https://github.com/emiliodeg/cphalcon: (27 commits)
  [#13208] - Corrected more tests
  [#13208] - More test corrections
  [#13208] - Corrected test
  [#13208] - Corrected references
  [#13208] - Trying to fix the observed var
  [#13208] - Trying different scope
  [#13208] - Trying interface corrections
  Revert "[#13208] - Interface corrections"
  [#13208] - Interface corrections
  Revert "[#13208] - Interface corrections/docblocks"
  [#13208] - Interface corrections/docblocks
  Updated docblock
  Updated validators
  Fixed array fields
  Fixed some tests
  Changed advice to template
  Changed advice for template
  More string length tests
  String length tests
  Added a message factory
  ...
  • Loading branch information
niden committed Jun 21, 2019
2 parents 0a56c20 + 162fc84 commit 0d7fb93
Show file tree
Hide file tree
Showing 101 changed files with 3,331 additions and 886 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Moved `method` parameter in `Phalcon\Mvc\Model\Manager::getBelongsToRecords()` to the last position. [#14115](https://github.com/phalcon/cphalcon/issues/14115)
- Moved `method` parameter in `Phalcon\Mvc\Model\Manager::getHasOneRecords()` to the last position. [#14115](https://github.com/phalcon/cphalcon/issues/14115)
- Moved `method` parameter in `Phalcon\Mvc\Model\Manager::getHasManyRecords()` to the last position. [#14115](https://github.com/phalcon/cphalcon/issues/14115)
- Validator messages were moved into each validator. [#13208](https://github.com/phalcon/cphalcon/issues/13208)
- `Phalcon\Paginator\Repository::getProperty()` now uses `Phalcon\Helper\Arr::get()`.
- Renamed `Phalcon\Collection` to `Phalcon\Collection\Collection`. [#14154](https://github.com/phalcon/cphalcon/pull/14154)
- Refactored `Phalcon\Collection\Collection` to allow conditional key case sensitivity. [#14154](https://github.com/phalcon/cphalcon/pull/14154)
Expand Down
57 changes: 0 additions & 57 deletions phalcon/Validation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Validation extends Injectable implements ValidationInterface
{
protected combinedFieldsValidators;
protected data { get };
protected defaultMessages;
protected entity;
protected filters = [];
protected labels = [];
Expand All @@ -55,8 +54,6 @@ class Validation extends Injectable implements ValidationInterface
}
);

this->setDefaultMessages();

/**
* Check for an 'initialize' method
*/
Expand Down Expand Up @@ -132,20 +129,6 @@ class Validation extends Injectable implements ValidationInterface
return this;
}

/**
* Get default message for validator type
*/
public function getDefaultMessage(string! type) -> string
{
var defaultMessage;

if !fetch defaultMessage, this->defaultMessages[type] {
return "";
}

return defaultMessage;
}

/**
* Returns the bound entity
*/
Expand Down Expand Up @@ -344,46 +327,6 @@ class Validation extends Injectable implements ValidationInterface
return this;
}

/**
* Adds default messages to validators
*/
public function setDefaultMessages(array messages = []) -> array
{
var defaultMessages;

let defaultMessages = [
"Alnum": "Field :field must contain only letters and numbers",
"Alpha": "Field :field must contain only letters",
"Between": "Field :field must be within the range of :min to :max",
"Confirmation": "Field :field must be the same as :with",
"Digit": "Field :field must be numeric",
"Email": "Field :field must be an email address",
"ExclusionIn": "Field :field must not be a part of list: :domain",
"FileEmpty": "Field :field must not be empty",
"FileIniSize": "File :field exceeds the maximum file size",
"FileMaxResolution": "File :field must not exceed :max resolution",
"FileMinResolution": "File :field must be at least :min resolution",
"FileSize": "File :field exceeds the size of :max",
"FileType": "File :field must be of type: :types",
"FileValid": "Field :field is not valid",
"Identical": "Field :field does not have the expected value",
"InclusionIn": "Field :field must be a part of list: :domain",
"Numericality": "Field :field does not have a valid numeric format",
"PresenceOf": "Field :field is required",
"Regex": "Field :field does not match the required format",
"TooLong": "Field :field must not exceed :max characters long",
"TooShort": "Field :field must be at least :min characters long",
"Uniqueness": "Field :field must be unique",
"Url": "Field :field must be a url",
"CreditCard": "Field :field is not valid for a credit card number",
"Date": "Field :field is not a valid date"
];

let this->defaultMessages = array_merge(defaultMessages, messages);

return this->defaultMessages;
}

/**
* Sets the bound entity
*
Expand Down
140 changes: 126 additions & 14 deletions phalcon/Validation/Validator.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace Phalcon\Validation;

use Phalcon\Collection;
use Phalcon\Helper\Arr;
use Phalcon\Messages\Message;
use Phalcon\Validation;
use Phalcon\Validation\Exception;
use Phalcon\Validation\ValidatorInterface;
Expand All @@ -21,16 +24,111 @@ use Phalcon\Validation\ValidatorInterface;
*/
abstract class Validator implements ValidatorInterface
{
/**
* Message template
*
* @var string|null
*/
protected template;

/**
* Message templates
*
* @var array
*/
protected templates = [];

protected options;

/**
* Phalcon\Validation\Validator constructor
*/
public function __construct(array! options = []) -> void
{
var template;

let template = current(Arr::whiteList(options, ["template", "message", 0]));

if typeof template == "array" {
this->setTemplates(template);
} elseif typeof template == "string" {
this->setTemplate(template);
}

if (template) {
unset options["template"];
unset options["message"];
unset options[0];
}

let this->options = options;
}

/**
* Get the template message
*
* @return string
* @throw InvalidArgumentException When the field does not exists
*/
public function getTemplate(string! field = null) -> string
{
// there is a template in field
if field !== null && isset this->templates[field] {
return this->templates[field];
}

// there is a custom template
if (this->template) {
return this->template;
}

// default template message
return "The field :field is not valid for " . get_class(this);
}

/**
* Get templates collection object
*
* @return array
*/
public function getTemplates() -> array
{
return this->templates;
}

/**
* Clear current templates and set new from an array,
*
* @return Validator
*/
public function setTemplates(array! templates) -> <ValidatorInterface>
{
var field, template;

let this->templates = [];

for field, template in templates {
let field = (string) field;
let template = (string) template;

let this->templates[field] = template;
}

return this;
}

/**
* Set a new template message
*
* @return Validator
*/
public function setTemplate(string! template) -> <ValidatorInterface>
{
let this->template = template;

return this;
}

/**
* Returns an option in the validator's options
* Returns null if the option hasn't set
Expand Down Expand Up @@ -114,22 +212,36 @@ abstract class Validator implements ValidatorInterface
}

/**
* Prepares a validation message.
*/
protected function prepareMessage(<Validation> validation, string! field, string! type, string! option = "message") -> var
* Create a default message by factory
*
* @return Message
*
* @throw Exception
*/
public function messageFactory(<Validation> validation, var field, array! replacements = []) -> <Message>
{
var message;

let message = this->getOption(option);

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

if empty message {
let message = validation->getDefaultMessage(type);
var singleField;

if (is_array(field)) {
let singleField = implode(", ", field);
} elseif (is_string(field)) {
let singleField = field;
} else {
throw new Exception("The field can not be printed");
}

return message;
let replacements = array_merge(
[
":field" : this->prepareLabel(validation, singleField)
],
replacements
);

return new Message(
strtr(this->getTemplate(singleField), replacements),
field,
get_class(this),
this->prepareCode(singleField)
);
}
}
20 changes: 4 additions & 16 deletions phalcon/Validation/Validator/Alnum.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Phalcon\Validation\Validator;

use Phalcon\Messages\Message;
use Phalcon\Validation;
use Phalcon\Validation\Validator;

Expand Down Expand Up @@ -52,31 +51,20 @@ use Phalcon\Validation\Validator;
*/
class Alnum extends Validator
{
protected template = "Field :field must contain only letters and numbers";

/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, message, label, replacePairs, code;
var value;

let value = validation->getValue(field);

if !ctype_alnum(value) {
let label = this->prepareLabel(validation, field),
message = this->prepareMessage(validation, field, "Alnum"),
code = this->prepareCode(field);

let replacePairs = [
":field": label
];

validation->appendMessage(
new Message(
strtr(message, replacePairs),
field,
"Alnum",
code
)
this->messageFactory(validation, field)
);

return false;
Expand Down
19 changes: 4 additions & 15 deletions phalcon/Validation/Validator/Alpha.zep
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,20 @@ use Phalcon\Validation\Validator;
*/
class Alpha extends Validator
{
protected template = "Field :field must contain only letters";

/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, message, label, replacePairs, code;
var value;

let value = validation->getValue(field);

if preg_match("/[^[:alpha:]]/imu", value) {
let label = this->prepareLabel(validation, field),
message = this->prepareMessage(validation, field, "Alpha"),
code = this->prepareCode(field);

let replacePairs = [
":field": label
];

validation->appendMessage(
new Message(
strtr(message, replacePairs),
field,
"Alpha",
code
)
this->messageFactory(validation, field)
);

return false;
Expand Down
20 changes: 6 additions & 14 deletions phalcon/Validation/Validator/Between.zep
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ use Phalcon\Validation\Validator;
*/
class Between extends Validator
{
protected template = "Field :field must be within the range of :min to :max";

/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, minimum, maximum, message, label, replacePairs, code;
var value, minimum, maximum, replacePairs;

let value = validation->getValue(field),
minimum = this->getOption("minimum"),
maximum = this->getOption("maximum");
minimum = this->getOption("minimum"),
maximum = this->getOption("maximum");

if typeof minimum == "array" {
let minimum = minimum[field];
Expand All @@ -83,23 +85,13 @@ class Between extends Validator
}

if value < minimum || value > maximum {
let label = this->prepareLabel(validation, field),
message = this->prepareMessage(validation, field, "Between"),
code = this->prepareCode(field);

let replacePairs = [
":field": label,
":min": minimum,
":max": maximum
];

validation->appendMessage(
new Message(
strtr(message, replacePairs),
field,
"Between",
code
)
this->messageFactory(validation, field, replacePairs)
);

return false;
Expand Down
Loading

0 comments on commit 0d7fb93

Please sign in to comment.