Improved Form Listener
markitosgv opened this issue · 2 comments
When you try to get purified some array elements, default listener doesn't work.
I override listener to do something like this:
public function purifySubmittedData(FormEvent $event)
{
$event->setData($this->purifyResult($event->getData()));
}
protected function purifyResult($data)
{
if (\is_array($data)) {
array_walk($data, function (&$item) {
$item = $this->purifyResult($item);
});
}
if (\is_string($data)) {
return $this->getPurifier()->purify($data);
}
return $data;
}
What do you think about this?
Hello @makasim, could you explain what is the use case? Because actually the listener is used for a TextType
extension, which should not have to deal with non scalar values.
This usage has also been constrained is Symfony core for security reasons. Only compound forms should deal with array values (some exceptions exist in core for file and select input that can be multiple but they do handle trim differently though).
Then for compound forms each inner field is responsible for trimming its own value.
I would prefer to be sure the use case is common enough before implementing a solution like the one you proposed. Thanks!
Closing here for lack of feedback. Don't hesitate to reopen if needed, thanks!