We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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::VERSION_4 | IP::VERSION_6,
                '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: https://192.168.60.100/hosts/new

In the logs...

What to do??



93.7k
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.phalcon.io/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...