Skip to content

Instantly share code, notes, and snippets.

@ricbra
Created August 14, 2015 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricbra/99b8f6a8ffe259760fa3 to your computer and use it in GitHub Desktop.
Save ricbra/99b8f6a8ffe259760fa3 to your computer and use it in GitHub Desktop.
How to set violation with array
<?php
namespace SmaakEnSensatie\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class DeliveryValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if ($value) {
$data = $this->context->getRoot()->getData();
$required = array('deliveryStreet', 'deliveryNumber', 'deliveryZipcode', 'deliveryCity', 'deliveryCountry');
foreach ($required as $key) {
if (empty($data[$key])) {
$this->hackViolation($key, 'Dit veld is verplicht');
}
}
}
}
protected function hackViolation($path, $message)
{
$reflection = new \ReflectionObject($this->context);
$property = $reflection->getProperty('propertyPath');
$property->setAccessible(true);
$property->setValue($this->context, 'children['.$path.'].data');
$this->context->addViolation($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment