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

Using form element filters

Hello, i have login page and, for example, i need get data from this form, filter and validate.

Im creating loginform class that extends Phalcon\Forms\Form and adding following element in initialize function:

    $email = new Text('email', 
          [
              'placeholder' => 'e-mail',
          ]
      );

      $email->setFilters(
          [
              'email',
          ]
      );

      $email->addValidators(
              new Email(
                  [
                      'message' => 'text'
                  ]
              ),
          ]
      );

In controller action that handle data from this form i have the following code:

$loginForm = new LoginForm();
if ($loginForm->isValid($this->request->getPost())) {
    ...
}

And my question: How I can filter data for this element or at once for all elements? I read documentation and i know that I can create Filter class and use function "sanitize", but is it correct solution for my example? If this is correct solution so then for what Im adding filters in form initialize?

As I think, it must work before validation automatically, but its not working.



455

I found solution, but is this the best approach to filters input values? I cant do it in more simpler way?

    $postData = [];

    foreach ($loginForm as $element) {
        $postData[$element->getName()] = $this->request->getPost(
            $element->getName(),
            $element->getFilters()
        );
    }


13.8k

Have a look here in the source code for validation examples: https://github.com/Videles/PhalconTime/