#17424·cphalcon

[NFR]: New filters

Author: dev-fanCreated Jul 31, 2026Updated Sep 17, 2026
Labelsnew feature request7.0

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']);