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

Update 5.7.x custom validator example #251

Merged
merged 1 commit into from
Feb 20, 2025
Merged
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
31 changes: 19 additions & 12 deletions docs/filter-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1763,33 +1763,40 @@ You can create your own validators by implementing the [Phalcon\Filter\Validatio
```php
<?php

use Phalcon\Messages\Message;
use Phalcon\Filter\Validation;
use Phalcon\Filter\Validation\AbstractValidator;

class IpValidator extends AbstractValidator
{
/**
* Adding the default template error message
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->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;
Expand Down