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 |
What about
VDW\IssueTrackerBundle\Entity\Author:
properties:
profileData:
- Collection:
fields:
alternate_email:
- Collection\Optional:
- Email: ~
There might be room for improvement here.
Then we get:
Fatal error: Class 'Symfony\Component\Validator\Constraints\Collection\OptionalValidator'
It's going wrong in Collection
line 50:
if (!$field instanceof Optional && !$field instanceof Required) {
$this->fields[$fieldName] = $field = new Required($field);
}
$field
is an instance of Optional or Required when using PHP or annotations. When using Yaml, $field
is an array:
array (size=1)
0 =>
object(Symfony\Component\Validator\Constraints\Collection\Optional)[4300]
public 'constraints' =>
array (size=1)
0 =>
object(Symfony\Component\Validator\Constraints\Email)[4301]
...
public 'groups' =>
array (size=1)
0 => string 'Default' (length=7)
Because it's not an instance of Optional or Required, it becomes:
object(Symfony\Component\Validator\Constraints\Collection\Required)[4302]
public 'constraints' =>
array (size=1)
0 =>
object(Symfony\Component\Validator\Constraints\Collection\Optional)[4300]
public 'constraints' =>
array (size=1)
...
public 'groups' =>
array (size=1)
...
public 'groups' =>
array (size=1)
0 => string 'Default' (length=7)
And that's when we are in trouble, because the ValidatorFactory
will search for OptionalValidator
.
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 the YamlFileLoader
is needed to make this work.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bschussek I'm sorry, but that gave another error:
And changing it to:
{ Collection\Optional: [Email] }
gives this error: (which is what we got too)