@@ -1763,33 +1763,40 @@ You can create your own validators by implementing the [Phalcon\Filter\Validatio
1763
1763
``` php
1764
1764
<?php
1765
1765
1766
- use Phalcon\Messages\Message;
1767
1766
use Phalcon\Filter\Validation;
1768
1767
use Phalcon\Filter\Validation\AbstractValidator;
1769
1768
1770
1769
class IpValidator extends AbstractValidator
1771
1770
{
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
+
1772
1783
/**
1773
1784
* Executes the validation
1774
1785
*
1775
- * @param Validation $validator
1776
- * @param string $attribute
1786
+ * @param Validation $validation
1787
+ * @param string $field
1777
1788
*
1778
1789
* @return boolean
1779
1790
*/
1780
- public function validate(Validation $validator , $attribute )
1791
+ public function validate(Validation $validation , $field )
1781
1792
{
1782
- $value = $validator ->getValue($attribute );
1793
+ $value = $validation ->getValue($field );
1783
1794
1784
1795
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];
1790
1797
1791
- $validator ->appendMessage(
1792
- $this->messageFactory($message , $attribute, 'Ip' )
1798
+ $validation ->appendMessage(
1799
+ $this->messageFactory($validation , $field, $replacements )
1793
1800
);
1794
1801
1795
1802
return false;
0 commit comments