This validator works for symfony 1.4 and is not necessarily backward compatible.
If you’d like to both validate and sanitize a phone number in symfony, it’s pretty easy. First, put the following code in lib/sfValidatorPhone.class.php:
<?php /** * sfValidatorPhone validates a phone number. * * @author Jason Swett (http://jasonswett.net/how-to-validate-a-phone-number-in-symfony) */ class sfValidatorPhone extends sfValidatorBase { protected function doClean($value) { $clean = (string) $value; $phone_number_pattern = '/^(^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$)*$/'; // If the value isn't a phone number, throw an error. if (!preg_match($phone_number_pattern, $clean)) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } // Take out anything that's not a number. $clean = preg_replace('/[^0-9]/', '', $clean); // Split the phone number into its three parts. $first_part = substr($clean, 0, 3); $second_part = substr($clean, 3, 3); $third_part = substr($clean, 6, 4); // Format the phone number. $clean = '('.$first_part.') '.$second_part.'-'.$third_part; return $clean; } }
Then, in the form where you have your phone number field, add the following line to your configure() method:
$this->validatorSchema['phone'] = new sfValidatorPhone(array('required' => false));
That’s all! Now, if someone enters a number like 123.456.7890, it will get saved as (123) 456-7890.