Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would create a custom validation constraint which applies constraints on each key-value pair (or key only if you want to) in array. Similar to <code>All</code> constraint, but validation is performed on key-value pair, not value only.</p> <pre><code>namespace GLS\DemoBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; class AssocAll extends Constraint { public $constraints = array(); public function __construct($options = null) { parent::__construct($options); if (! is_array($this-&gt;constraints)) { $this-&gt;constraints = array($this-&gt;constraints); } foreach ($this-&gt;constraints as $constraint) { if (!$constraint instanceof Constraint) { throw new ConstraintDefinitionException('The value ' . $constraint . ' is not an instance of Constraint in constraint ' . __CLASS__); } } } public function getDefaultOption() { return 'constraints'; } public function getRequiredOptions() { return array('constraints'); } } </code></pre> <p>Constraint validator, which passes an array with key-value pair to each constraint:</p> <pre><code>namespace GLS\DemooBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; class AssocAllValidator extends ConstraintValidator { public function validate($value, Constraint $constraint) { if (null === $value) { return; } if (!is_array($value) &amp;&amp; !$value instanceof \Traversable) { throw new UnexpectedTypeException($value, 'array or Traversable'); } $walker = $this-&gt;context-&gt;getGraphWalker(); $group = $this-&gt;context-&gt;getGroup(); $propertyPath = $this-&gt;context-&gt;getPropertyPath(); foreach ($value as $key =&gt; $element) { foreach ($constraint-&gt;constraints as $constr) { $walker-&gt;walkConstraint($constr, array($key, $element), $group, $propertyPath.'['.$key.']'); } } } } </code></pre> <p>I guess, only <code>Callback</code> constraint makes sense to be applied on each key-value pair, where you put your validation logic.</p> <pre><code>use GLS\DemoBundle\Validator\Constraints\AssocAll; $validator = Validation::createValidator(); $constraint = new Constraints\Collection(array( 'emails' =&gt; new AssocAll(array( new Constraints\Callback(array( 'methods' =&gt; array(function($item, ExecutionContext $context) { $key = $item[0]; $value = $item[1]; //your validation logic goes here //... } ))), )), 'user' =&gt; new Constraints\Regex('/^[a-z]+$/i'), 'amount' =&gt; new Constraints\Range(['min' =&gt; 5, 'max' =&gt; 10]), )); $violations = $validator-&gt;validateValue($input, $constraint); var_dump($violations); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload