Skip to content

Commit f210c22

Browse files
authored
Merge pull request #2083 from MohKari/mohkari/validation
WIP Update Validation.php
2 parents 30b02c6 + 237f44c commit f210c22

3 files changed

Lines changed: 58 additions & 11 deletions

File tree

system/Validation/Validation.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ public function check($value, string $rule, array $errors = []): bool
208208
* the error to $this->errors and moves on to the next,
209209
* so that we can collect all of the first errors.
210210
*
211-
* @param string $field
212-
* @param string|null $label
213-
* @param string $value
214-
* @param array|null $rules
215-
* @param array $data // All of the fields to check.
211+
* @param string $field
212+
* @param string|null $label
213+
* @param string|array $value Value to be validated, can be a string or an array
214+
* @param array|null $rules
215+
* @param array $data // All of the fields to check.
216216
*
217217
* @return boolean
218218
*/
@@ -291,7 +291,14 @@ protected function processRules(string $field, string $label = null, $value, $ru
291291
// Set the error message if we didn't survive.
292292
if ($passed === false)
293293
{
294-
$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param) : $error;
294+
// if the $value is an array, convert it to as string representation
295+
if (is_array($value))
296+
{
297+
$value = '[' . implode(', ', $value) . ']';
298+
}
299+
300+
$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param, $value)
301+
: $error;
295302

296303
return false;
297304
}
@@ -682,10 +689,11 @@ public function setError(string $field, string $error): ValidationInterface
682689
* @param string $field
683690
* @param string|null $label
684691
* @param string $param
692+
* @param string $value The value that caused the validation to fail.
685693
*
686694
* @return string
687695
*/
688-
protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null): string
696+
protected function getErrorMessage(string $rule, string $field, string $label = null, string $param = null, string $value = null): string
689697
{
690698
// Check if custom message has been defined by user
691699
if (isset($this->customErrors[$field][$rule]))
@@ -702,6 +710,7 @@ protected function getErrorMessage(string $rule, string $field, string $label =
702710

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

706715
return $message;
707716
}

tests/system/Validation/ValidationTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,42 @@ public function testSplitRegex()
576576

577577
$this->assertEquals('regex_match[/^[0-9]{4}[\-\.\[\/][0-9]{2}[\-\.\[\/][0-9]{2}/]', $result[1]);
578578
}
579+
580+
//--------------------------------------------------------------------
581+
582+
public function testTagReplacement()
583+
{
584+
// data
585+
$data = [
586+
'Username' => 'Pizza',
587+
];
588+
589+
// rules
590+
$this->validation->setRules([
591+
'Username' => 'min_length[6]',
592+
], [
593+
'Username' => [
594+
'min_length' => 'Supplied value ({value}) for {field} must have at least {param} characters.',
595+
],
596+
]);
597+
598+
// run validation
599+
$this->validation->run($data);
600+
601+
// $errors should contain an associative array
602+
$errors = $this->validation->getErrors();
603+
604+
// if "Username" doesn't exist in errors
605+
if (! isset($errors['Username']))
606+
{
607+
$this->fail('Unable to find "Username"');
608+
}
609+
610+
// expected error message
611+
$expected = 'Supplied value (Pizza) for Username must have at least 6 characters.';
612+
613+
// check if they are the same!
614+
$this->assertEquals($expected, $errors['Username']);
615+
}
616+
579617
}

user_guide_src/source/libraries/validation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@ Or as a labeled style::
461461
);
462462

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

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

468-
On a field with the human name Username and a rule of min_length[5], an error would display: “Username must have
469-
at least 5 characters.”
468+
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
469+
at least 6 characters.”
470470

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

0 commit comments

Comments
 (0)