Which is the suggested place to modify binded form data in Symfony?

Viewed 8505

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?

  • Inside a CustomerController prior to call entity manager and persist the entity. Is this really a matter of a controller in MVC pattern?
  • Using a SanitizeCustomerSubscriber and listening to FormEvents:POST_BIND event
  • Using a CustomerSanitizer service

Any 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.

2 Answers
Related