Symfony: Custom subform validation
<p>I have form witch uses custom non-mapped subform.</p>

<pre><code>public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder-&gt;add('subtype', 'my-subtype');
    }
}
</code></pre>

<p>The subform is consisting of multiple fields and I need to perform additional check on both of them together. The Callback constraint is perfect for the job. However I can not find a way how to add this constraint on the subform as a whole.</p>

<p>So far I have tried to set the Callback in setDefaultOptions() or set it with setAttribute() in buildForm() but the callback is not evaluated.</p>

<p>Currently I am just adding the Callback to one of the fields:</p>

<pre><code>public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder-&gt;add('field1', 'text')
        -&gt;add(
            'field2', 'text',
             array(
            'constraints' =&gt; array(
                new Callback(array(
                   'methods' =&gt; array(array($this, 'validateMyType'))
                ))
        )
    ));
}

public function validateMyType($data, ExecutionContextInterface $context) {
    // Validation failed...
    $context-&gt;addViolationAt('subtype', "mySubtypeViolation");
    return;
}
</code></pre>

<p>This however prevents me to add the violation on the whole subtype. What ever I use in addViolationAt() the violation is always added to field which hosts the Callback constraint.</p>