-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Impossible to affect form options during buildForm in FormExtensions #8513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I forgot to give a usage example, here an "other: please specify" type field is dependant on it's choice field public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('refer', 'choice', array(
'label' => 'How did you hear about us?',
'constraints' => array(
new NotBlank(),
),
'choices' => array(
'internet' => 'Internets',
'other' => 'Other'
),
))
->add('referOther', 'text', array(
'label' => 'Please specify',
'depends' => array(
'[refer]' => 'other'
),
'constraints' => array(
new NotBlank(),
),
));
} |
I think you are posing two questions in your issue that I'll answer separately:
Since 1. is not easily solvable and 2. is covered by #5807, I'll close this as a duplicate. |
I forgot to answer a third question. You want to change options of a field after that field was constructed. This is impossible by design. Instead, you can overwrite the field with a new one and different options: $config = $form->get('field')->getConfig();
$type = $config->getType()->getName();
$options = $config->getOptions();
$form->add('field', $type, array_replace($options, array(
'constraints' => // changed constraints...
)); |
Thanks for responding! As far as point 1 goes, my plan was to serialise constraints and their associated options to data attributes, somewhat like With the exception of validation groups, it would be possible to write a generic js library that could pick up all those data attributes and re-create the validation schema client side. As far as point 2 goes, I was considering (re-)using the constraints / validation system to allow for complex, value based dependencies, and at least dependencies on certain values would be needed for our use case - see the TODO in Thanks for the pointer on overwriting fields - maybe I can use that in the interim. Once again, thanks for the feedback
|
Hi,
For background, I am attempting to write a
FormTypeExtension
that serialises form field constraints into data attributes on the input elements so that it is possible to write a generalised javascript library capable of validating most typical symfony forms client side.One place that breaks down is when validation rules depend on submitted data - I've seen and have been using this to successfully validate on the server side, but without the ability to define field interdependencies declaratively, it becomes impossible to bake these rules into data attributes: there is no way to serialise the decision that happens in that closure.
With that in mind, I've been attempting to build another
FormTypeExtension
that introduces the notion of dependencies, see comments inline:And the listener:
As you can see, it seems impossible to unset / interact with the constraints associated with the field, even though it's possible to simply outright delete the field.
Could anyone shed any light on if what I'm trying to do is possible / desired / insane / stupid and help point me in the direction of a solution?
Thanks,
Luke Cawood
The text was updated successfully, but these errors were encountered: