Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Form filters are not working #15438

Closed
wurst-hans opened this issue Apr 28, 2021 · 3 comments · Fixed by #15759
Closed

[BUG]: Form filters are not working #15438

wurst-hans opened this issue Apr 28, 2021 · 3 comments · Fixed by #15759
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium

Comments

@wurst-hans
Copy link

wurst-hans commented Apr 28, 2021

Using Phalcon 4.1.0 on PHP 7.4.16 there's an issue with forms using filters.

Sample code:

class SignupController extends Controller
{
    public function indexAction()
    {
        $form = new Form();
        $form->add(
            (new Text('firstname'))
                ->addValidator(
                    new StringLength(
                        [
                            'min' => 5,
                            'messageMinimum' => 'The value is too short',
                        ]
                    )
                )
                ->setFilters([
                    'upper',
                    'trim',
                ])
                ->setLabel('Firstname')
        );
        $form->add(
            new Submit('submit', ['value' => 'Submit'])
        );
        if ($this->request->isPost() && $form->isValid($_POST)) {
            var_dump($this->request->getPost());
            echo "<br>";
            var_dump($form->getValue('firstname'));
            echo "<br>";
        }
        return $this->view
            ->setVars(['form' => $form])
            ->render('sso/signup');
    }
}

When using a short firstname, for example "tom" the validation works and returns an error:

image

But when using a longer firstname, for example " michael" (with two leading whitespaces!), the validator is satisfied, but the filters aren't applied:

image

I'm using Di\FactoryDefault and the filters have been executed, see print_r($this->di->get('filter')):

Phalcon\Filter Object
(
    [mapper:protected] => Array
        (
            [absint] => Phalcon\Filter\Sanitize\AbsInt
            [alnum] => Phalcon\Filter\Sanitize\Alnum
            [alpha] => Phalcon\Filter\Sanitize\Alpha
            [bool] => Phalcon\Filter\Sanitize\BoolVal
            [email] => Phalcon\Filter\Sanitize\Email
            [float] => Phalcon\Filter\Sanitize\FloatVal
            [int] => Phalcon\Filter\Sanitize\IntVal
            [lower] => Phalcon\Filter\Sanitize\Lower
            [lowerFirst] => Phalcon\Filter\Sanitize\LowerFirst
            [regex] => Phalcon\Filter\Sanitize\Regex
            [remove] => Phalcon\Filter\Sanitize\Remove
            [replace] => Phalcon\Filter\Sanitize\Replace
            [special] => Phalcon\Filter\Sanitize\Special
            [specialFull] => Phalcon\Filter\Sanitize\SpecialFull
            [string] => Phalcon\Filter\Sanitize\StringVal
            [striptags] => Phalcon\Filter\Sanitize\Striptags
            [trim] => Phalcon\Filter\Sanitize\Trim
            [upper] => Phalcon\Filter\Sanitize\Upper
            [upperFirst] => Phalcon\Filter\Sanitize\UpperFirst
            [upperWords] => Phalcon\Filter\Sanitize\UpperWords
            [url] => Phalcon\Filter\Sanitize\Url
        )
    [services:protected] => Array
        (
            [upper] => Phalcon\Filter\Sanitize\Upper Object
                (
                )
            [trim] => Phalcon\Filter\Sanitize\Trim Object
                (
                )
        )
)

But how to get the filtered values then? Wether in $_POST nor in $form->getValue('firstname') the raw input value has been sanitized.

@wurst-hans wurst-hans added bug A bug report status: unverified Unverified labels Apr 28, 2021
@Jeckerson Jeckerson added 5.0 The issues we want to solve in the 5.0 release and removed 5.0 The issues we want to solve in the 5.0 release labels May 6, 2021
@BeMySlaveDarlin
Copy link
Contributor

@wurst-hans
First of all, thanks for your report.
Currently Phalcon\Forms\Form::getValue(key) returns raw value and doesn't applies filters you provided.
Filtering is triggered in Phalcon\Forms\Form::bind(data, modelClass, whitelist), when you binding request data to model entity.

@niden we can add (v.5.0.0) new method Phalcon\Forms\Form::getFilteredValue(key), which will give filtered values of request data without binding models.

@wurst-hans
Copy link
Author

Thanks for clarification. But that would be great, if filtered values are accessible in any way. In most cases my forms are related to multiple models, so I don't use bind()

@Jeckerson Jeckerson added the 5.0 The issues we want to solve in the 5.0 release label Nov 3, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 3, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 3, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 3, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 3, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 3, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Nov 3, 2021
@niden niden linked a pull request Nov 3, 2021 that will close this issue
5 tasks
@niden niden added status: medium Medium and removed status: unverified Unverified labels Nov 3, 2021
@niden
Copy link
Member

niden commented Nov 3, 2021

Resolved in #15759

@niden niden closed this as completed Nov 3, 2021
@niden niden moved this to Released in Phalcon v5 Aug 25, 2022
@niden niden added this to Phalcon v5 Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants