[NFR]: New filters
@niden is already working on this.
Since Aug 1, 2026.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.