logo

$5
Silently format columns after form input and before output

Hi,

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.

Thanks.

webguy | 05/20/10 at 4:40pm | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    05/20/10
    5:08pm
    Wojciech Sznapka says:

    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);

    Previous versions of this answer: 05/20/10 at 4:48pm

  • avatar
    Last edited:
    05/20/10
    5:08pm
    Martin Palacio says:

    According to the Symfony forms book, the best place is to override the updateObject() method. Take a look there.

This question has expired.





Current status of this question: Completed