Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Update Validation.php #2083

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ public function check($value, string $rule, array $errors = []): bool
* the error to $this->errors and moves on to the next,
* so that we can collect all of the first errors.
*
* @param string $field
* @param string|null $label
* @param string $value
* @param array|null $rules
* @param array $data // All of the fields to check.
* @param string $field
* @param string|null $label
* @param string|array $value Value to be validated, can be a string or an array
* @param array|null $rules
* @param array $data // All of the fields to check.
*
* @return boolean
*/
Expand Down Expand Up @@ -291,7 +291,14 @@ protected function processRules(string $field, string $label = null, $value, $ru
// Set the error message if we didn't survive.
if ($passed === false)
{
$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param) : $error;
// if the $value is an array, convert it to as string representation
if (is_array($value))
{
$value = '[' . implode(', ', $value) . ']';
}

$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value)
: $error;

return false;
}
Expand Down Expand Up @@ -682,10 +689,11 @@ public function setError(string $field, string $error): ValidationInterface
* @param string $field
* @param string|null $label
* @param string $param
* @param string $value The value that caused the validation to fail.
*
* @return string
*/
protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null): string
protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null, string $value = null): string
{
// Check if custom message has been defined by user
if (isset($this->customErrors[$field][$rule]))
Expand All @@ -702,6 +710,7 @@ protected function getErrorMessage(string $rule, string $field, string $label =

$message = str_replace('{field}', $label ?? $field, $message);
$message = str_replace('{param}', $this->rules[$param]['label'] ?? $param, $message);
$message = str_replace('{value}', $value, $message);

return $message;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,42 @@ public function testSplitRegex()

$this->assertEquals('regex_match[/^[0-9]{4}[\-\.\[\/][0-9]{2}[\-\.\[\/][0-9]{2}/]', $result[1]);
}

//--------------------------------------------------------------------

public function testTagReplacement()
{
// data
$data = [
'Username' => 'Pizza',
];

// rules
$this->validation->setRules([
'Username' => 'min_length[6]',
], [
'Username' => [
'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.',
],
]);

// run validation
$this->validation->run($data);

// $errors should contain an associative array
$errors = $this->validation->getErrors();

// if "Username" doesn't exist in errors
if (! isset($errors['Username']))
{
$this->fail('Unable to find "Username"');
}

// expected error message
$expected = 'Supplied value (Pizza) for Username must have at least 6 characters.';

// check if they are the same!
$this->assertEquals($expected, $errors['Username']);
}

}
8 changes: 4 additions & 4 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ Or as a labeled style::
);

If you’d like to include a field’s “human” name, or the optional parameter some rules allow for (such as max_length),
you can add the ``{field}`` and ``{param}`` tags to your message, respectively::
or the value that was validated you can add the ``{field}``, ``{param}`` and ``{value}`` tags to your message, respectively::

'min_length' => '{field} must have at least {param} characters.'
'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.'

On a field with the human name Username and a rule of min_length[5], an error would display: “Username must have
at least 5 characters.”
On a field with the human name Username and a rule of min_length[6] with a value of “Pizza”, an error would display: “Supplied value (Pizza) for Username must have
at least 6 characters.”

.. note:: If you pass the last parameter the labeled style error messages will be ignored.

Expand Down