Skip to content

[NFR]: New filters #17424

Open

Description

@dev-fan

I have a form with drop-down fields that reference another entity, but this field is optional (for example, an employee's deputy). If I don't select anything in this field, the POST data returns an empty string, which is converted to 0 using the form filters $deputy->setFilters([Filter::FILTER_ABSINT]);. The table's foreign key constraints prohibit inserting a zero, because there is no such record.
I propose adding a new filter, "absintornull", which, if the input is an empty string, will output null instead of zero.
And I would also like similar behavior for intornull, floatornull, and emptytonull, that is, where the difference between null and zero (an empty string) is important.

Describe the solution you'd like
Phalcon\Filter\Filter::FILTER_ABSINTORNULL,
Phalcon\Filter\Filter::FILTER_INTORNULL,
Phalcon\Filter\Filter::FILTER_FLOATORNULL, ...

I currently use this solution for form filters.*

$di->setShared('filter', function () {
    $filterF = new FilterFactory();
    $filter = $filterF->newInstance();
    $filter->set('emptytonull', function ($value) {
        return $value !== '' ? $value : null;
    });
    $filter->set('intornull', function ($value) {
        $options = ['options' => ['default' => null]];
        return filter_var($value, FILTER_VALIDATE_INT, $options);
    });
    $filter->set('absintornull', function ($value) {
        $options = ['options' => ['default' => null]];
        $value = filter_var($value, FILTER_VALIDATE_INT, $options);
        return $value ? abs($value) : $value;
    });
    $filter->set('floatornull', function ($value) {
        $options = ['options' => ['default' => null]];
        $value = is_string($value) ? str_replace(',', '.', $value) : $value;
        return filter_var($value, FILTER_VALIDATE_FLOAT, $options);
    });
    return $filter;
});
// Example of use
$deputy->setFilters(['absintornull']);

Activity

  1. added
    new feature request
    on Jul 31, 2026
  2. added
    5.0
    on Jul 31, 2026
  3. added theissue type on Jul 31, 2026
  4. self-assigned this
    on Aug 1, 2026
  5. moved this from Backlog to Active Sprint in Phalcon v5on Aug 1, 2026
  6. niden commented on Sep 17, 2026

    @niden
    SponsorMember

    @dev-fan I thought about this one and I agree it is a great addition.

    However, I don't want to start bloating the filters. As such I am moving this to v7 where we can address it properly, since this will require additional parameters to be passed into the filters.

  7. added this to Phalcon v7 and removed this from Phalcon v5on Sep 17, 2026
  8. added and removed
    5.0
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

7.0new feature request

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions