Created
September 10, 2014 12:03
-
-
Save stof/d21838b00d08858ae6bb to your computer and use it in GitHub Desktop.
Conditional validation group based on the state of the entity
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
<?php | |
namespace Incenteev\WebBundle\Validator; | |
use Symfony\Component\Validator\Constraint; | |
/** | |
* @Annotation | |
* @Target({"CLASS", "ANNOTATION"}) | |
*/ | |
class ConditionalValidationGroup extends Constraint | |
{ | |
public $groupProvider; | |
public function getRequiredOptions() | |
{ | |
return array('groupProvider'); | |
} | |
public function getTargets() | |
{ | |
return self::CLASS_CONSTRAINT; | |
} | |
} |
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
<?php | |
namespace Incenteev\WebBundle\Validator; | |
use Symfony\Component\PropertyAccess\PropertyAccess; | |
use Symfony\Component\Validator\Constraint; | |
use Symfony\Component\Validator\ConstraintValidator; | |
class ConditionalValidationGroupValidator extends ConstraintValidator | |
{ | |
private $propertyAccessor; | |
public function __construct() | |
{ | |
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); | |
} | |
/** | |
* @param object|array|null $value | |
* @param Constraint $constraint | |
*/ | |
public function validate($value, Constraint $constraint) | |
{ | |
if (null === $value) { | |
return; | |
} | |
/** @var ConditionalValidationGroup $constraint */ | |
$group = $this->propertyAccessor->getValue($value, $constraint->groupProvider); | |
if (is_string($group)) { | |
$this->context->validate($value, '', array($group)); | |
} | |
} | |
} |
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
<?php | |
namespace Incenteev\WebBundle\Entity; | |
use Incenteev\WebBundle\Validator as IncenteevAssert; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @IncenteevAssert\ConditionalValidationGroup(groupProvider="type") | |
*/ | |
class Prize | |
{ | |
const TYPE_CUSTOM = 'custom'; | |
const TYPE_POINTS = 'points'; | |
/** | |
* @var string | |
* | |
* @Assert\NotBlank(groups={"custom"}) | |
* @Assert\Length(max=255, groups={"custom"}) | |
*/ | |
private $name; | |
/** | |
* @var string | |
* | |
* @Assert\NotBlank() | |
* @Assert\Choice(callback="getTypeValues") | |
*/ | |
private $type = self::TYPE_CUSTOM; | |
/** | |
* @var float | |
* | |
* @Assert\NotBlank(groups={"points"}) | |
* @Assert\Range(min=1, groups={"points"}) | |
*/ | |
private $value; | |
public static function getTypeChoices() | |
{ | |
return array( | |
self::TYPE_CUSTOM => 'prize.type.custom', | |
self::TYPE_POINTS => 'prize.type.points', | |
); | |
} | |
public static function getTypeValues() | |
{ | |
return array_keys(static::getTypeChoices()); | |
} | |
// Getters and setters ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment