Skip to content

Fix: Form field filters not applied when no validators are specified#16855

Draft
Copilot wants to merge 6 commits into5.0.xfrom
copilot/fix-field-filters-validation-issue
Draft

Fix: Form field filters not applied when no validators are specified#16855
Copilot wants to merge 6 commits into5.0.xfrom
copilot/fix-field-filters-validation-issue

Conversation

Copy link

Copilot AI commented Mar 22, 2026

In Form::isValid(), elements with no validators were skipped entirely via an early continue, preventing any registered filters from being applied to the bound entity. Regression introduced in 5.10.0.

// test1 has filters but NO validators — trim was silently skipped
$field = new Text('test1');
$field->setFilters([Filter::FILTER_TRIM]);

// test2 has filters AND a validator — trim worked correctly
$field2 = new Text('test2');
$field2->setFilters([Filter::FILTER_TRIM]);
$field2->addValidator(new Numericality(['allowEmpty' => true]));

Before fix: entity->test1 === ' ' (spaces preserved), entity->test2 === '' (trimmed)
After fix: both fields correctly trimmed to ''

Changes

  • phalcon/Forms/Form.zep — In isValid(), move getFilters() before the early-continue guard and change the condition to skip only when both validators and filters are absent:
    // Before
    if count(validators) == 0 {
        continue;
    }
    
    // After
    let filters = element->getFilters();
    if count(validators) == 0 && typeof filters != "array" {
        continue;
    }
  • tests/integration/Forms/Form/IsValidCest.php — Add formsFormIsValidAppliesFiltersWithoutValidators regression test covering both the no-validator and with-validator cases.

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines
  • I have checked that another pull request for this purpose does not exist
  • I wrote some tests for this PR
  • I have updated the relevant CHANGELOG
  • I have created a PR for the documentation about this change

Thanks

Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG]: Field filters do not work if validators are not specified.</issue_title>
<issue_description>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

</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI changed the title [WIP] Fix field filters not working without validators Fix: Form field filters not applied when no validators are specified Mar 22, 2026
Copilot AI requested a review from niden March 22, 2026 13:20
@niden niden changed the base branch from master to 5.0.x March 22, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants