From 7f59005b097269fcb1def47a23e93a6c0bb4dd4d Mon Sep 17 00:00:00 2001 From: Vasile Bogdan Raica Date: Thu, 20 Feb 2025 17:11:45 +0200 Subject: [PATCH] update custom validator example --- docs/filter-validation.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/docs/filter-validation.md b/docs/filter-validation.md index 337b5ea8e..584edf38c 100644 --- a/docs/filter-validation.md +++ b/docs/filter-validation.md @@ -1763,33 +1763,40 @@ You can create your own validators by implementing the [Phalcon\Filter\Validatio ```php template = 'The IP :ip_address is not valid'; + + parent::__construct($options); + } + /** * Executes the validation * - * @param Validation $validator - * @param string $attribute + * @param Validation $validation + * @param string $field * * @return boolean */ - public function validate(Validation $validator, $attribute) + public function validate(Validation $validation, $field) { - $value = $validator->getValue($attribute); + $value = $validation->getValue($field); if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) { - $message = $this->getOption('message'); - - if (!$message) { - $message = 'The IP is not valid'; - } + $replacements = [':ip_address' => $value]; - $validator->appendMessage( - $this->messageFactory($message, $attribute, 'Ip') + $validation->appendMessage( + $this->messageFactory($validation, $field, $replacements) ); return false;