I've a form for creating a new Customer
. A customer may have a mobile number. Mobile number should be persisted without +
or 00
prefix that user can type. This can be accomplished easly with:
$customer->setMobile(preg_replace("/^(\+|00)/", '', $customer->getMobile()));
Which is the best place to put this code?
CustomerController
prior to call entity manager and persist the entity. Is this really a matter of a controller in MVC pattern?SanitizeCustomerSubscriber
and listening to FormEvents:POST_BIND
eventCustomerSanitizer
serviceAny other idea? Of course i'm speaking of data manipulation in general, mobile number is just an example: fields to be sanitized could be more than just one.
You should do this in the PRE_BIND
event, where you can access the submitted data before it is being processed.
$builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) {
$data = $event->getData();
if (isset($data['mobile'])) {
$data['mobile'] = preg_replace("/^(\+|00)/", '', $data['mobile']);
}
$event->setData($data);
});
For the record, as of Symfony 2.3, this event is called PRE_SUBMIT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With