I have a symfony 1.4 form with 2 embedded forms.
The parent form has a drop down which determines which of the embedded forms you fill in (hidden/shown on the frontend using JavaScript).
The problem is, when I submit the form, the validation and save is being run on both of the embedded forms, which I obviously don’t want.
What is the best way to alter the parent form so that it only validates and saves the relevant embedded form based on the selection made in the parent form?
Thanks.
Note: Please see jeremy’s answer as mine is based on his.
Thank you for your answer jeremy. Your code had a few issues, so I thought I’d post my implemented solution explaining what I did differently.
1. Override doBind()
The override of doBind() had an issue where an uncaught sfValidatorError would be thrown if the parent value didn’t return clean from the validator. I wrapped it in a try/catch to suppress this.
I also altered it to work with multiple embedded forms, not just the two I specified.
2. NEW STEP Override updateObjectEmbeddedForms()
Because I’ve disabled validation on some or all of my embedded forms, we now have some uncleaned data in the $values array. I don’t want this data being passed to my model objects within the embedded forms, so I’ve overridden
updateObjectEmbeddedForms()to remove any data related to an embedded form that isn’t validated.3. Override saveEmbeddedForms()
And finally, I didn’t like I had to copy and paste the entire base
saveEmbeddedForms()method and then alter it, so I refactored it to remove the embedded forms I don’t want to save before passing them to the parent.Thanks again for the answer jeremy, it got me to this which works for my use case.