Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validator in a form to validate IP address

Hi guys

I'm trying to validate an IP address in a form:

.... ``` $ip = new Text("ip"); $ip->setLabel("IP/Hostname"); $ip->setFilters(array('striptags', 'string')); $ip->addValidators(array( new IP(array( 'version' => IP::VERSION4 | IP::VERSION6, 'allowReserved' => false,
'allowPrivate' => false,
'message' => 'IP address has to be correct' ))

    ));

$this->add($ip); ``` .....

But I keep getting:

[Fri Jul 22 11:46:58.447250 2016] [:error] [pid 7909] [client 192.168.60.2:55810] Wrong number of parameters\n#0 [internal function]: Phalcon\Mvc\Model\Validator\Ip->validate(Object(Phalcon\Validation), 'ip')\n#1 [internal function]: Phalcon\Validation->validate(Array, Object(Hosts))\n#2 /var/www/invo/app/controllers/HostsController.php(98): Phalcon\Forms\Form->isValid(Array, Object(Hosts))\n#3 [internal function]: HostsController->createAction()\n#4 [internal function]: Phalcon\Dispatcher->dispatch()\n#5 /var/www/invo/public/index.php(32): Phalcon\Mvc\Application->handle()\n#6 {main}, referer: http://192.168.60.100/hosts/new

In the logs...

What to do??



66.0k
Accepted
answer

Under the table in this link it is explained how to create your own validators. Lucky for you that the example is exactly on how to validate an IP address :)

Great...

an just because i'm really new to Phalcon, where do I place the file with the custom validator???

It depends on your folder structure, but basically you need just to set up the namespaces to the class. You can add it in your file where you declare your namepsaces/directories. More info here: https://docs.phalconphp.com/en/latest/reference/namespaces.html

Basically anywhere you want. Im using multimodule application and i have it under <ProjectName>/App/Validators

Great...

an just because i'm really new to Phalcon, where do I place the file with the custom validator???

Great.. thanks guys...