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.

Validate form based on multiple values

Hi

I'm trying to create a validator that validates a user and its password..

How do I pass two arguements to a validator??

I was thinking something like:

            new LoginValidator(array(
                'message' => 'Login was unsuccessful',
                'field' => array('user', 'passwd')
            ))

or ``` new LoginValidator(array( 'message' => 'Login was unsuccessful'), $user,$passwd )) ``` How do I read these values in the Validator class.. and is there a better way??



64.0k

in latest 2.1.x build you can

    $validator = new \Phalcon\Validation();

    $validator->add(["name", "type", "brand"], new PresenceOf([
    "message" : [
    "name" : "enter name",
    "type" : "choose your destiny!",
    "brand" : "aweosome phalcon"
    ]
    ]));

    return $this->validate($validator);

Ok.. how do I use that with the addValidators syntax???

        $user = new Text("user");
        $user->setLabel("Username");
        $user->setFilters(array('striptags', 'string'));
        $user->addValidators(array(
            new PresenceOf(array(
                'message' => 'Username is required'
            ))
        ));
        $this->add($user);

        $passwd = new Password('passwd');
        $passwd->setLabel('Password');
        $passwd->addValidators(['user','passwd'],array(
            new PresenceOf(array(
                'message' => 'Password is required'
            )),
            new LoginValidator(array(
                "message" => array(
                "user" => $user,
                "passwd" => $passwd
                )
            ))
        ));
        $this->add($passwd);

Gives me a "One of the validators is not valid", error....

==> /var/log/httpd/error_log <==
[Tue Jul 26 08:00:23.980737 2016] [:error] [pid 19813] [client 192.168.60.2:39798] One of the validators is not valid\n#0 [internal function]: Phalcon\\Validation->validate(Array, Object(Hosts))\n#1 /var/www/invo/app/controllers/HostsController.php(98): Phalcon\\Forms\\Form->isValid(Array, Object(Hosts))\n#2 [internal function]: HostsController->createAction()\n#3 [internal function]: Phalcon\\Dispatcher->callActionMethod(Object(HostsController), 'createAction', Array)\n#4 [internal function]: Phalcon\\Dispatcher->_dispatch()\n#5 [internal function]: Phalcon\\Dispatcher->dispatch()\n#6 /var/www/invo/public/index.php(32): Phalcon\\Mvc\\Application->handle()\n#7 {main}, referer: http://192.168.60.100/hosts/new
[Tue Jul 26 08:00:24.336515 2016] [core:notice] [pid 19811] AH00052: child pid 19813 exit signal Segmentation fault (11)


64.0k

is this LoginValidator working ? perhaps there a problem with itself ?



64.0k

did you check this one here to make sure you follow the right path https://docs.phalconphp.com/en/latest/reference/validation.html#validators ?

Right now the only thing LoginValidator is doing is:

<?php

use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;

class LoginValidator extends Validator implements ValidatorInterface
{
    public function validate(Validation $validator, $attribute)
    {
            $validator->appendMessage(new Message("test", $attribute, 'Login'));
            return false;
    }
}

The segfault occurs when I change:

$passwd->addValidators(array(

to

$passwd->addValidators(['user','passwd'],array(

Maybe addValidators doesn't support this???



64.0k

i think it has to be

addValidators( [ new LoginValidator("user", ["message'] => "whatever" ), new LoginValidator("passwd", message =? lala) ] )

But then I will create two validators, with 1 input each?? instead of one validator with 2 inputs???

Maybe something like:???? https://github.com/phalcon/cphalcon/blob/2.1.x/phalcon/validation/validator/uniqueness.zep#L106

Probably in the CombinedFieldsValidator....

edited Jul '16

addValidators has the reversed syntax:

        $passwd->addValidators(array(
            new PresenceOf(array(
                'message' => 'Password is required'
            )),
            new LoginValidator(array(
                'passwd' => $passwd->getValue(),
                'user' => $user->getValue()

                )
            ))
        ,['user','passwd']);

So this works :)..

even though getValue on a password element doesn't seem to work... works for the Text element...



64.0k
Accepted
answer

Jurigag made changes in model validation, you can see it here https://github.com/phalcon/cphalcon/pull/11826 but this is in model validator. YOu can check tests he commited.

I also put my validators in the model, so no idea if this is the same for the forms, but it doesnt look like that based on what i see in the code ( but i could be wrong )