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

[2.1.x] Form input array invalidate

Hi. I try to validate my form. But get errors.

// Captcha
        $captchaId = new Hidden('captcha[id]');
        $captchaId->setAttributes(array(
            'name' => 'captcha[id]',
            'value' => $captchaIdCode,
            'required' => true,
            'autocomplete' => 'off',
        ));
        $captchaId->setFilters(array('striptags', 'trim', 'string'));
        $captchaId->addValidators(array(
            new PresenceOf(array(
                'message' => 'Name is required'
            )),
            new StringLength(array(
                'min' => 32,
                'max' => 32,
                'messageMinimum' => 'Minimum',
                'messageMaximum' => 'Maximum',
            ))
        ));
        $this->add($captchaId);

        // Captcha
        $captchaInput = new Text('captcha[input]');
        $captchaInput->setAttributes(array(
            'placeholder' => $this->translator->trans('Captcha'),
            'id' => 'captcha',
            'name' => 'captcha[input]',
            'required' => true,
            'autocomplete' => 'off',
        ));
        $captchaInput->setFilters(array('striptags', 'trim', 'string'));
        $captchaInput->addValidators(array(
            new PresenceOf(array(
                'message' => 'Name is required'
            )),
            new StringLength(array(
                'min' => 8,
                'max' => 8,
                'messageMinimum' => 'Minimum',
                'messageMaximum' => 'Maximum',
            ))
        ));
        $this->add($captchaInput);

This is my post data

Array
(
    [captcha] => Array
        (
            [id] => 7e0ab94c212006dec468e01f86e0babb
            [input] => fd1dfDGS
        )

    [csrf] => TwEJe8k5UQmjB2Ut
)

Get these messages

Phalcon\Validation\Message\Group Object
(
    [_position:protected] => 0
    [_messages:protected] => Array
        (
            [0] => Phalcon\Validation\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => Name is required
                    [_field:protected] => captcha[id]
                    [_code:protected] => 0
                )

            [1] => Phalcon\Validation\Message Object
                (
                    [_type:protected] => TooShort
                    [_message:protected] => Minimum
                    [_field:protected] => captcha[id]
                    [_code:protected] => 0
                )

            [2] => Phalcon\Validation\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => Name is required
                    [_field:protected] => captcha[input]
                    [_code:protected] => 0
                )

            [3] => Phalcon\Validation\Message Object
                (
                    [_type:protected] => TooShort
                    [_message:protected] => Minimum
                    [_field:protected] => captcha[input]
                    [_code:protected] => 0
                )

        )

)


14.4k

I got this answer on question like yours ... my be it help you ))

Validation was rewritten in version 2.1 and model validation is deprecated https://blog.phalcon.io/post/phalcon-2-1-beta-released it will be surely reflected in Invo when 2.1 will be out