-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Embedded forms using same property path - error bubbling problems #5578
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
ping @bschussek - any ideas? |
Hey, Thank you for taking the time to report this issue. Adding two fields with the same property path smells fishy to me. Not sure if that could work, I did not try it so far. However, I suggest you use the "virtual" option which was created for solving your use case: <?php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array(
'label' => '* Email address:'
))
->add('account_personal', 'my_personalinfo_form', array(
'virtual' => true,
))
->add('account_contact', 'my_contactinfo_form', array(
'virtual' => true,
))
;
} Within "my_personalinfo_form" and "my_contactinfo_form", set the property paths of the fields explicitly. I will leave this ticket open for further investigation. |
I added a corresponding feature request. |
This PR was merged into the 2.1 branch. Commits ------- 2568432 [Form] Hardened code of ViolationMapper against errors Discussion ---------- [Form] Hardened code of ViolationMapper against errors Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - License of the code: MIT Documentation PR: - This ticket improves the code of ViolationMapper to reduce the risk of bugs and in order to make further bug fixing easier. It was implemented while trying to solve #5578 and is semantically equivalent to the previous version.
Thanks for the reply @bschussek! I still had errors following your suggestion, the first was along the lines of:
Then I changed the
Couldn't get past this, but then came up with a different tact of creating a new FormType to contain the 2 embedded forms using virtual fields:
and then replaced these fields in the main
This seems like the 'proper' way of doing this, it keeps the property_paths unique within the form and no property_path changes required in the sub-forms either. |
Embedding "account_personal" and "account_contact" directly within the main form definitly should be possible. Could you post your complete code? |
Hi @bschussek Sorry for the slow reply. So following your suggestions my code looked like this: Main registration form:
Personal info form:
Contact info form:
The error I get with this configuration (I just set this up again to check) is thrown when processing the form (ie, it renders fine):
I suspect the
Again, thrown on form submit. As I say, the other solution I mentioned works well for me anyway as it means I can still use the Hope this helps, let me know if I can provide any more information. |
This is weird. I'm reopening this ticket until this is solved. |
I just tested your code and, apart from a few mistakes, it works for me. You should not define any custom property paths on PersonalInfoType and ContactInfoType. The code will work with the default property paths. Also, don't forget to set the "data_class" option on RegistrationType (if you did not already). |
This doesn't work for me. I think because the
Does that make sense? |
I correctly reproduced your problem now, I previously made a mistake. I think this may be fixable by solving #5720. |
This is fixed in the referenced PR. |
This PR was merged into the master branch. Discussion ---------- [2.3] [Form] Renamed option "virtual" to "inherit_data" and improved handling of such forms Bug fix: yes Feature addition: yes Backwards compatibility break: yes Symfony2 tests pass: yes Fixes the following tickets: #5899, #5720, #5578 Todo: - License of the code: MIT Documentation PR: symfony/symfony-docs#2107 This PR renames the option "virtual" to "inherit_data" for more clarity (the old option is deprecated and usable until 2.3). It also fixes the behavior of forms having that option set. Forms with that option set will now correctly return their parents' data from `getData()`, `getNormData()` and `getViewData()`. Furthermore, `getPropertyPath()` was fixed for forms that inherit their parent data. Commits ------- 1290b80 [Form] Fixed the deprecation notes for the "virtual" option ac2ca44 [Form] Moved parent data inheritance from data mappers to Form 8ea5e1a [Form] Renamed option "virtual" to "inherit_data"
I have a problem that error messages for an embedded form are bubbling to the parent form when used with another embedded form pointing to the same property_path of a related object.
My situation is like this, I have Users and Account entities I have split up the Account into different forms, then for the registration form I am pulling these bits together like so:
The problem is that error messages for
account_personal
are bubbling to the top of the form. For example "Please enter your first name" if first name is left blank in the personal info form. The 'personal' and 'contact' forms work fine in their own forms on their own pages.The error messages for account_contact are fine and appear next to the correct fields.
HOWEVER, if I swap the two
->add
bits about above (so haveaccount_contact
first) then the problem reverses; the error messages foraccount_personal
now appear correctly next to their corresponding fields, but now the errors foraccount_contact
get bubbled to the top!Does this sound like a bug or am I doing something wrong?
Thanks!
---- Other files for reference:
Personal info form:
Contact info form:
and the Account entity for completion:
The text was updated successfully, but these errors were encountered: