Last active
December 15, 2015 14:58
-
-
Save benglass/5277987 to your computer and use it in GitHub Desktop.
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
// YML: Does not work | |
VDW\IssueTrackerBundle\Entity\Author: | |
properties: | |
profileData: | |
- Collection: | |
fields: | |
alternate_email: | |
Collection\Optional: | |
// Annotations | |
/** | |
* @Assert\Collection( | |
* fields={ | |
* "primary_email" = @Required({@Assert\NotBlank, @Assert\Email}), | |
* "alternate_email" = @Optional({@Assert\Email}), | |
* } | |
* ) | |
*/ | |
$profileData = array(); | |
// PHP | |
public static function loadValidatorMetadata(ClassMetadata $metadata) | |
{ | |
$metadata->addPropertyConstraint('profileData', new Assert\Collection(array( | |
'fields' => array( | |
'primary_email' => new Required(array(new Assert\NotBlank(), new Assert\Email())), | |
'alternate_email' => new Optional(array(new Assert\Email())), | |
) | |
))); | |
} | |
// Controller Code | |
$author = new Author(); | |
$author->setProfileData('primary_email', 'foo'); | |
$author->setProfileData('alternate_email', 'bar'); | |
$validator = $this->get('validator'); | |
$errors = $validator->validate($author); | |
Error: | |
The options "Collection\Optional" do not exist in constraint Symfony\Component\Validator\Constraints\Collection\Required |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then we get:
It's going wrong in
Collection
line 50:$field
is an instance of Optional or Required when using PHP or annotations. When using Yaml,$field
is an array:Because it's not an instance of Optional or Required, it becomes:
And that's when we are in trouble, because the
ValidatorFactory
will search forOptionalValidator
.I have tried every Yaml syntax I know to make
$field
a class and not an array, but I can't find it. I think some change in theYamlFileLoader
is needed to make this work.