phalcon / phalcon/cphalcon

[NFR]: New filters

Open
#17,424 1 comment 0 reactions 1 assignee View on GitHub

@niden is already working on this.

Since Aug 1, 2026.

7.0 new feature request
Dominant language
PHP
Stars
10.8k
Forks
1.9k
Avg merge
7h 2m
Merged PRs (30d)
47

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.