Ask your Symfony questions! Pay money and get answers fast! (more info)

Silently format columns after form input and before output Symfony



Here is my situation: I am using symfony 1.3 and doctrine 1.2.

I have phone numbers in my project. I wish to allow any formatting for phone numbers on input to form. Before actually saving to database, I want to have all non numeric characters stripped. The problem here is not the regular expression (don't get hung up on formatting etc of #), I just need to know where in the sfForm/doctrine saving process of the form to override a function to format this input.

So, for example:
Customer enters 555-555-5555.
Customer clicks save, form validates, phone number is scrubbed, phone number is stored in database as 5555555555.

Then, on the flip side, is there a way to automatically (via the object) to format output.
So, the database has 5555555555.
If i call contact->getPhone() i would want it to output (555) 555-5555 or something.


Answers (2)


Wojciech Sznapka answers:

The second case is easier:
in lib/model/doctrine/Contact.class.php define getPhone method:

public function getPhone()
$phone = $this->_get('phone');
$phone = doWhateverYouWantWithPhone($phone);
return $phone;

similary to this you can override Contact::setPhone($value);


Martin Palacio answers:

According to the [[LINK href=""]]Symfony forms book[[/LINK]], the best place is to override the updateObject() method. Take a look there.