Skip to content

[BUG]: Field filters do not work if validators are not specified. #16830

@dev-fan

Description

@dev-fan

Field filters do not work if validators are not specified.

Steps to reproduce the behavior:
I used the Filter::FILTER_TRIM filter for this example. If addValidator is missing, the filters aren't applied. I enter spaces in the field and submit the form. The DTO object is then assigned a value containing spaces.

<?php

declare(strict_types=1);

require_once '../init.php';

use Phalcon\Filter\Filter;
use Phalcon\Filter\Validation\Validator\Numericality;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Form;

class ExampleForm extends Form
{

    public function initialize(?object $entity = null, array $options = [])
    {
        $field = new Text('test1', ['size' => 10, 'maxlength' => 10]);
        $field->setLabel('Test');
        $field->setFilters([Filter::FILTER_TRIM]);
        $this->add($field);

        $field = new Text('test2', ['size' => 10, 'maxlength' => 10]);
        $field->setLabel('Test');
        $field->setFilters([Filter::FILTER_TRIM]);
        $field->addValidator(new Numericality(['message' => _('Test failed'), 'allowEmpty' => true]));
        $this->add($field);

        $submit = new Submit('submit', ['id' => 'register', 'value' => 'Send']);
        $this->add($submit);
    }
}

$dto = new \stdClass();
$dto->test = null;
$form = new ExampleForm($dto);
$form->bind($di['request']->getQuery(), $dto);
if ($di['request']->isPost()) {
    echo '<div style="border: 2px solid blue;">$_POST: ';var_dump($di['request']->getPost());echo '</div>';
    if ($form->isValid($di['request']->getPost(), $dto)) {
        echo '<div style="border: 2px solid blue;">DTO after isValid: ';var_dump($dto);echo '</div>';
    }
}
?>
<form method="POST" ><?php
foreach ($form as $element) {
    echo $element->render();
}
?></form>
// Result
$_POST:
array (size=3)
  'test1' => string '   ' (length=3)
  'test2' => string '   ' (length=3)
  'submit' => string 'Send' (length=4)

DTO after isValid:
object(stdClass)[123]
  public 'test' => null
  public 'test1' => string '   ' (length=3)
  public 'test2' => string '' (length=0)
  public 'submit' => string 'Send' (length=4)

Expected behavior
I think the expected behavior is to apply filters regardless of whether validators are specified for this field or not. This behavior existed before version 5.10.0.

Screenshots

Image

Details

  • Phalcon version: 5.10.0
  • PHP Version: PHP 8.4.16 (cli) (built: Dec 18 2025 23:38:53) (NTS)
  • Operating System: Ubuntu 24.04
  • Server: Nginx

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions