Hi folks,
I’ve got a form that uses a Select element with the empty_option set.
The form is bound to an object.
When the POST comes in with the empty option selected, that’s a value of “” (empty string) in HTML-land. Single-stepping through bind, populateValues, and friends, that value stays all the way through the process, and my object’s setProperty gets called with the empty string.
Is there a canonical way to convert that empty string to null so that by the time setProperty gets called, a null is passed in? Is that the hydrator’s job?
Ah, or maybe Zend\Filter\ToNull
But seems like too common a need to not be built-in somewhere.
No, at the moment the empty option is only used for the output. It would be a feature request, but with a BC break. The current behaviour with the empty string is different.
Would it be a BC break if it were just an option like ‘use_null_for_empty’ ?
The resulting code in my entity doesn’t seem to be too different from just testing for an empty string. My type hints can stay intact, which adds to expressiveness I guess…
public function setSupersedes(Competency $competency = null)
{
if (null === $competency) {
if ($this->supersedes) {
$this->supersedes->setSupersededBy();
}
$this->supersedes = null;
} else {
$this->supersedes = $competency;
$competency->setSupersededBy($this);
}
}
Please keep in mind, bloating the class with more options means:
- more code to maintain
- more tests
- more documentation
- more areas for problems
- more which the user has to know
- …
At the moment the current behaviour adapted the normal usage with POST values in PHP. Compare with all other text elements.
If you need something different, then add your own input-filter specification.
Beaut, thanks for the discussion @froschdesign