Symfony email validation (not using Form Framework)
As the title suggest, I'm looking for a way to validate emails (or other string formats) using the Symfony Framework (without having to use the Form Framework).
I've done some searching around, but every example given is using the Form classes...
And I know that I can use external classes or libraries, but I'm just curious if there is something that I could use 开发者_如何学Pythonfrom within Symfony that'll work.
Something like this would be ideal:
$email = "email@to.validate";
$validator = new sfEmailValidator(); // just using this as an example - I know it doesn't work :p
$validator->check($email); // returns true or false if it validates
You should be able to do something like the following -- you don't need to use the form framework to use the validator framework:
$email = 'email@to.validate';
$v = new sfValidatorEmail();
try {
$email = $v->clean($email);
} catch (sfValidatorError $e) {
// email invalid
}
I'd reconsider using sfValidatorEmail, it's not quite accurate. Use filter_var instead (you can make your own validator based on filter_var if you want).
精彩评论