Open leevigraham opened 7 months ago
Yeah, if you don't use JS to drop the value field before form submit - then form events is the correct way to work around it. Thanks for sharing your solution with others!
If you have ideas on how to improve the package - please, feel free to open a PR
@bocharsky-bw experimenting with this some more and I think I found something that may be useful.
In my form I have added an additional "update" button that sets the following options:
'validate' => false,
- Adds a formnovalidate
attribute which disables HTML5 validation if the form is submitted using this button'validation_groups' => false
- Disables all validation on the formIf the 'update' button is clicked the form is submitted and all front end and back end validation is disabled.
Combine this with an updated on change listener which passes the update button through as the requester element:
and you have no errors on change:
https://github.com/SymfonyCasts/dynamic-forms/assets/25124/8f5cab70-c819-455e-974f-2980b02f4a09
Submitting the form with the submit button still triggers HTML5 and backend validation.
Hey @leevigraham ,
Oh wow, this is an interesting workaround! Thanks for such a detailed description with the nice demo!
Hm, having 2 buttons is a bummer, but seems that do the trick, and probably the additional button might be hidden with CSS. Though I wonder if the behavior may be different in different browsers :/
So this workaround solves your issue? Do you think we can improve our docs by mentioning this?
I'm still not sure we're not missing something simple here... Btw, I found another possible solution - Symfony Forms have allow_extra_fields
feature, see https://symfony.com/doc/current/reference/forms/types/form.html#allow-extra-fields . So using it in your form like:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'allow_extra_fields' => true,
]);
}
Should also do the trick probably because will allow the form to accept additional fields not defined in the form type. However, it might pose security risks if not handled properly.
@bocharsky-bw allow_extra_fields
definitely works… I'm looking for a solution that disables all validation and updates the form. During my experiments I've found that I can only disable validation for the current form and child forms (not bubble up validation to the root form). I'll keep exploring.
I think allowing extra fields is risky. Adding $field->add(HiddenType::class, ['mapped' => false]);
instead seems to do the trick: it will register the field in the form, and because it's not mapped, it won't get into the resulting form data.
When submitting a form with a dependent field if the dependent field is not shown then a "This form should not contain extra fields." error is shown.
Video:
https://github.com/SymfonyCasts/dynamic-forms/assets/25124/fbec34dc-b3ec-4c64-ad35-81696c763d4d
I'm not using any javascript to remove the
value
field before submission so the data is being submitted triggering the error.I've also implemented this with
PRE_SUBMIT
on the parent form which checks the view data, adds / removes thevalue
field and unsets the data which works without error.