Last active
December 15, 2015 04:59
-
-
Save Mihailoff/5205406 to your computer and use it in GitHub Desktop.
Simple in-place validation in Symfony2
This file contains hidden or 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 | |
$data = array( | |
'agency' => array( | |
'name' => 'a', | |
'moderator' => 'b' | |
) | |
); | |
$rules = new Symfony\Component\Validator\Constraints\Collection(array( | |
'fields' => array( | |
'agency' => array( | |
new Symfony\Component\Validator\Constraints\Collection(array( | |
'fields' => array( | |
'name' => array( | |
new Symfony\Component\Validator\Constraints\NotBlank() | |
), | |
'moderator' => array( | |
new Symfony\Component\Validator\Constraints\NotBlank() | |
), | |
) | |
)) | |
) | |
) | |
)); | |
// @see http://api.symfony.com/2.1/Symfony/Component/Validator/ConstraintViolationList.html | |
$violationList = $container->get('validator')->validateValue($data, $rules); | |
if (count($violationList)) | |
echo $violationList; // gracefully casts to string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment