What would be the best way to temporarily disable validators on form. Consider following
MyController.php
$builder = $this->createFormBuilder()
->add('parentfield1')
->add('parentfield2')
->add('children', 'collection', array('type' => new ChildType(), 'allow_add' => true));
$form = $builder->getForm();
if ($request->request->get('addb')) {
$formReq = $request->request->get('form');
$formReq['children'][] = array(
'child_id' => '1',
'childfield1' => '',
'childfield2' => ''
);
$request->request->set('form', $formReq);
// I would like to disable validators here somehow
$form->bindRequest($request);
} elseif ($request->request->get('sendb')) {
$form->bindRequest($request);
// persist form to database
}
So in my form I have two different buttons: sendb - that posts the form, validates it and persists to database addb - that just posts the form and adds new fields for adding child items without calling validators
Currently I can do this with validation groups:
$validationGroups = array();
if($request->request->get('addb')) {
// I just use group not defined in entity for any validators
$validationGroups[] = 'novalidation';
}
$builder = createFormBuilder(new ParentEntity(), array('validation_groups' => $validationGroups));
This works but this also means code duplication since symfony2 only allows to pass validators into form builder constructor and I have to check request->get('addb') twice.
I do know that adding child form fields can also be done with javascript (collection prototype option) but I want to get it working without javascript.
You can set validation groups dynamically based on submitted data in setDefaultOptions
method. See here.