E5F6 [Validator] Removes exception when validating non-traversable value with Composite constraints by rchoquet · Pull Request #23040 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
*/
class All extends Composite
{
const WRONG_TYPE_ERROR = '824268b5-91c0-4730-983f-896fb0f971f0';

protected static $errorNames = array(
self::WRONG_TYPE_ERROR => 'WRONG_TYPE_ERROR',
);

public $constraints = array();
public $wrongTypeMessage = 'This value should be an array.';

public function getDefaultOption()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Validator/Constraints/AllValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public function validate($value, Constraint $constraint)
}

if (!is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
$this->context->buildViolation($constraint->wrongTypeMessage)
->setCode(All::WRONG_TYPE_ERROR)
->addViolation();

return;
}

$context = $this->context;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ class Collection extends Composite
{
const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
const WRONG_TYPE_ERROR = '212f9e68-bff4-404f-ab6d-9f29055139e2';

protected static $errorNames = array(
self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
self::WRONG_TYPE_ERROR => 'WRONG_TYPE_ERROR',
);

public $fields = array();
public $allowExtraFields = false;
public $allowMissingFields = false;
public $extraFieldsMessage = 'This field was not expected.';
public $missingFieldsMessage = 'This field is missing.';
public $wrongTypeMessage = 'This value should be an array.';

/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public function validate($value, Constraint $constraint)
}

if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
$this->context->buildViolation($constraint->wrongTypeMessage)
->setCode(Collection::WRONG_TYPE_ERROR)
->addViolation();

return;
}

// We need to keep the initialized context when CollectionValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ public function testNullIsValid()
$this->assertNoViolation();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testThrowsExceptionIfNotTraversable()
public function testAddViolationIfNotTraversable()
{
$this->validator->validate('foo.barbar', new All(new Range(array('min' => 4))));
$this->validator->validate('foo.barbar', new All(array(
'constraints' => array(new Range(array('min' => 4))),
'wrongTypeMessage' => 'myMessage',
)));

$this->buildViolation('myMessage')
->setCode(All::WRONG_TYPE_ERROR)
->assertRaised();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ public function testFieldsAsDefaultOption()
$this->assertNoViolation();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testThrowsExceptionIfNotTraversable()
public function testAddViolationIfNotTraversable()
{
$this->validator->validate('foobar', new Collection(array('fields' => array(
'foo' => new Range(array('min' => 4)),
))));
$this->validator->validate('foobar', new Collection(array(
'fields' => array(
'foo' => new Range(array('min' => 4)),
),
'wrongTypeMessage' => 'myMessage',
)));

$this->buildViolation('myMessage')
->setCode(Collection::WRONG_TYPE_ERROR)
->assertRaised();
}

public function testWalkSingleConstraint()
Expand Down
0