Skip to content

Commit

Permalink
[Validator][Form] Removed support for match-all group "*"
Browse files Browse the repository at this point in the history
The constraint "Valid" does not accept any options or groups anymore. As per
JSR303 1.0 final, section 3.5.1 "Object graph validation" (page 39),
properties  annotated with valid should be cascaded independent of the current
group (i.e. always). Thus the group "*" is not necessary anymore and was
removed from the "Valid" constraint in the Form validation.xml.
  • Loading branch information
Bernhard Schussek authored and fabpot committed Nov 17, 2010
1 parent a4e62a0 commit dbb8a94
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Constraint.php
Expand Up @@ -30,7 +30,7 @@ abstract class Constraint
{
const DEFAULT_GROUP = 'Default';

public $groups = self::DEFAULT_GROUP;
public $groups = array(self::DEFAULT_GROUP);

/**
* Initializes the constraint with options.
Expand Down
17 changes: 15 additions & 2 deletions Constraints/Valid.php
Expand Up @@ -11,8 +11,21 @@
* with this source code in the file LICENSE.
*/

use Symfony\Component\Validator\Exception\InvalidOptionsException;

class Valid extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should be instance of class {{ class }}';
public $class;
/**
* This constraint does not accept any options
*
* @param mixed $options Unsupported argument!
*
* @throws InvalidOptionsException When the parameter $options is not NULL
*/
public function __construct($options = null)
{
if (null !== $options) {
throw new InvalidOptionsException('The constraint Valid does not accept any options');
}
}
}
49 changes: 0 additions & 49 deletions Constraints/ValidValidator.php

This file was deleted.

20 changes: 20 additions & 0 deletions GraphWalker.php
Expand Up @@ -76,6 +76,26 @@ protected function walkMember(MemberMetadata $metadata, $value, $group, $propert
foreach ($metadata->findConstraints($group) as $constraint) {
$this->walkConstraint($constraint, $value, $group, $propertyPath);
}

if ($metadata->isCascaded()) {
$this->walkReference($value, $group, $propertyPath);
}
}

protected function walkReference($value, $group, $propertyPath)
{
if (null !== $value) {
if (is_array($value)) {
foreach ($value as $key => $element) {
$this->walkReference($element, $group, $propertyPath.'['.$key.']');
}
} else if (!is_object($value)) {
throw new UnexpectedTypeException($value, 'object or array');
} else {
$metadata = $this->metadataFactory->getClassMetadata(get_class($value));
$this->walkClass($metadata, $value, $group, $propertyPath);
}
}
}

public function walkConstraint(Constraint $constraint, $value, $group, $propertyPath)
Expand Down
7 changes: 6 additions & 1 deletion Mapping/ClassMetadata.php
Expand Up @@ -12,7 +12,8 @@
*/

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

class ClassMetadata extends ElementMetadata
{
Expand Down Expand Up @@ -77,6 +78,10 @@ public function getShortClassName()
*/
public function addConstraint(Constraint $constraint)
{
if ($constraint instanceof Valid) {
throw new ConstraintDefinitionException('The constraint Valid can only be put on properties or getters');
}

$constraint->addImplicitGroupName($this->getShortClassName());

parent::addConstraint($constraint);
Expand Down
8 changes: 1 addition & 7 deletions Mapping/ElementMetadata.php
Expand Up @@ -95,14 +95,8 @@ public function hasConstraints()
*/
public function findConstraints($group)
{
$globalConstraints = isset($this->constraintsByGroup['*'])
? $this->constraintsByGroup['*']
: array();

$groupConstraints = isset($this->constraintsByGroup[$group])
return isset($this->constraintsByGroup[$group])
? $this->constraintsByGroup[$group]
: array();

return array_merge((array) $globalConstraints, (array) $groupConstraints);
}
}
30 changes: 29 additions & 1 deletion Mapping/MemberMetadata.php
Expand Up @@ -11,13 +11,16 @@
* with this source code in the file LICENSE.
*/

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Exception\ValidatorException;

abstract class MemberMetadata extends ElementMetadata
{
public $class;
public $name;
public $property;
public $cascaded = false;
private $reflMember;

/**
Expand All @@ -34,6 +37,20 @@ public function __construct($class, $name, $property)
$this->property = $property;
}

/**
* {@inheritDoc}
*/
public function addConstraint(Constraint $constraint)
{
if ($constraint instanceof Valid) {
$this->cascaded = true;
} else {
parent::addConstraint($constraint);
}

return $this;
}

/**
* Returns the names of the properties that should be serialized
*
Expand All @@ -44,7 +61,8 @@ public function __sleep()
return array_merge(parent::__sleep(), array(
'class',
'name',
'property'
'property',
'cascaded', // TESTME
));
}

Expand Down Expand Up @@ -108,6 +126,16 @@ public function isPrivate()
return $this->getReflectionMember()->isPrivate();
}

/**
* Returns whether objects stored in this member should be validated
*
* @return boolean
*/
public function isCascaded()
{
return $this->cascaded;
}

/**
* Returns the value of this property in the given object
*
Expand Down

0 comments on commit dbb8a94

Please sign in to comment.