Skip to content

Commit c4d0575

Browse files
authored
Merge pull request #249 from raicabogdan/5.8.x
Update 5.8.x custom validator example
2 parents 3c09fb8 + 7f59005 commit c4d0575

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

docs/filter-validation.md

+19-12
Original file line numberDiff line numberDiff line change
@@ -1763,33 +1763,40 @@ You can create your own validators by implementing the [Phalcon\Filter\Validatio
17631763
```php
17641764
<?php
17651765

1766-
use Phalcon\Messages\Message;
17671766
use Phalcon\Filter\Validation;
17681767
use Phalcon\Filter\Validation\AbstractValidator;
17691768

17701769
class IpValidator extends AbstractValidator
17711770
{
1771+
/**
1772+
* Adding the default template error message
1773+
*
1774+
* @param array $options
1775+
*/
1776+
public function __construct(array $options = [])
1777+
{
1778+
$this->template = 'The IP :ip_address is not valid';
1779+
1780+
parent::__construct($options);
1781+
}
1782+
17721783
/**
17731784
* Executes the validation
17741785
*
1775-
* @param Validation $validator
1776-
* @param string $attribute
1786+
* @param Validation $validation
1787+
* @param string $field
17771788
*
17781789
* @return boolean
17791790
*/
1780-
public function validate(Validation $validator, $attribute)
1791+
public function validate(Validation $validation, $field)
17811792
{
1782-
$value = $validator->getValue($attribute);
1793+
$value = $validation->getValue($field);
17831794

17841795
if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
1785-
$message = $this->getOption('message');
1786-
1787-
if (!$message) {
1788-
$message = 'The IP is not valid';
1789-
}
1796+
$replacements = [':ip_address' => $value];
17901797

1791-
$validator->appendMessage(
1792-
$this->messageFactory($message, $attribute, 'Ip')
1798+
$validation->appendMessage(
1799+
$this->messageFactory($validation, $field, $replacements)
17931800
);
17941801

17951802
return false;

0 commit comments

Comments
 (0)