phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.76k stars 1.96k forks source link

[BUG]: Forms - `ctype_*` default values #16064

Closed niden closed 1 year ago

niden commented 1 year ago

I'm trying v5.0.0RC4 with PHP8.1. In the class that extends \Phalcon\Forms\Form, we specified validation when defining the element.

            $elm->addValidators([
                new Validator\Digit([
                    'message' => 'Invalid number of reservations',
                    'allowEmpty' => false,
                ]),
                new Validator\Between([
                    'minimum' => 0,
                    'maximum' => 15
                ])
            ]);

I expect it to be a number from 0 to 15, but I want to make an error for other numbers, strings, and unset (null or ''). But with the above configuration, I get this error. Deprecated: ctype_digit(): Argument of type null will be interpreted as string in the future

This is probably because the specification of ctype_*() has changed since PHP8.1, but Phalcon currently does not support it.

It would allow an unexpected value, but at the moment it's unavoidable, so even if I change it to Validator\Numericality, it won't recognize the posted value. After trying various things, it seems that there is a problem with the name of the element.

I receive the form data in an array because it takes less time to process it after it is received. For example:

<input type="number" name="foo[1]" value="" />
<input type="number" name="foo[2]" value="" />
<input type="number" name="foo[3]" value="" />

It is often used in select/option instead of input. (There are cases where multiple checkboxes are collectively acquired as name="cb[]") So I write the following in form::initialize().

        $optionAry = array(0,1,2,3);
        $elm = new Element\Select("order[1]", $optionAry, [
            'id' => 'order11',
            'useEmpty' => false,
        ]);

With this, you can get the HTML element with the name with [ ] in the view, but it doesn't seem to understand it during validation. If you change "order[1]" to "order1", it will be processed without problems, but considering the processing after receiving it, I would like to receive it as an array. Is it a problem that you can set it and output HTML tags, but you can't validate it?

Originally posted by @s-ohnishi in https://github.com/phalcon/cphalcon/discussions/16057

niden commented 1 year ago

Resolved in https://github.com/phalcon/cphalcon/pull/16068