Created
September 26, 2011 14:46
-
-
Save weaverryan/1242396 to your computer and use it in GitHub Desktop.
Example of validating a scalar value in Symfony2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// .. other things | |
use Symfony\Component\Validator\Constraints\Email; | |
class DefaultController | |
{ | |
public function someAction() | |
{ | |
$emails = // ... some way to get an email array | |
$validator = $this->get('validator'); | |
$constraint = new Email(); | |
$goodEmails = array(); | |
$badEmails = array(); | |
foreach ($emails as $email) { | |
$errors = $validator->validateValue($email, $constraint); | |
if (count($errors) == 0) { | |
$goodEmails[] = $email; | |
} else { | |
$badEmails[] = $email; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment