8000 handle Optional and Required constraints from XML or YAML sources correc... by xabbuh · Pull Request #7930 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

handle Optional and Required constraints from XML or YAML sources correc... #7930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public function __construct($options = null)
}

foreach ($this->fields as $fieldName => $field) {
// the XmlFileLoader and YamlFileLoader pass the field Optional
// and Required constraint as an array with exactly one element
if (is_array($field) && count($field) == 1) {
$this->fields[$fieldName] = $field = $field[0];
}

if (!$field instanceof Optional && !$field instanceof Required) {
$this->fields[$fieldName] = $field = new Required($field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Collection\Required;
use Symfony\Component\Validator\Constraints\Collection\Optional;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Valid;

/**
Expand Down Expand Up @@ -70,4 +71,42 @@ public function testRejectValidConstraintWithinRequired()
'foo' => new Required(new Valid()),
));
}

public function testAcceptOptionalConstraintAsOneElementArray()
{
$collection1 = new Collection(array(
"fields" => array(
"alternate_email" => array(
new Optional(new Email()),
),
),
));

$collection2 = new Collection(array(
"fields" => array(
"alternate_email" => new Optional(new Email()),
),
));

$this->assertEquals($collection1, $collection2);
}

public function testAcceptRequiredConstraintAsOneElementArray()
{
$collection1 = new Collection(array(
"fields" => array(
"alternate_email" => array(
new Required(new Email()),
),
),
));

$collection2 = new Collection(array(
"fields" => array(
"alternate_email" => new Required(new Email()),
),
));

$this->assertEquals($collection1, $collection2);
}
}
0