At the moment there is no way to pass results from one subform submit handlers' execution into another. E.g. we want to create a multiform, that would allow anonymous users to register and create a node under the newly registered user at once (as it is done in Inline Registration module). The problem is that when node form is built, it obviously doesn't contain new user uid, so when it is submitted it is assigned to anonymous user. Also we don't want to submit any subform if validation failed for any other. Thus, we need to validate all subforms before any of them is submitted. As a solution, all subforms' submits could be delayed until all of them are validated and fired only in case validation was successfull.
Comment | File | Size | Author |
---|---|---|---|
#12 | multiform-delayed_submit_and_ajax_support-1870120-12.patch | 74.8 KB | kpv |
Comments
Comment #1
kpv CreditAttribution: kpv commentedHere is a patch.
Changes list:
Submit handlers are executed after all subforms' were successfully validated. Relates to #1663212: Form processing ignores validation errors.
The hook allows to add handlers that would alter subforms' data before and after every single subform submission, or trigger specific actions depending on multiform submission stage. Also subforms' execution order can be changed through this hook.
Fixes #1785184: Multiform and AJAX for the case when standard path 'system/path' is used.
The hook allows to replace paths for custom ajax callbacks. See multiform_multiform_ajax_alter() for an example.
Although arguments can be passed to multiform_get_form() in old fasion, a new method is proposed.
Settings example
The patch should be consistent with 1.0 version implementations but anyway should be used with caution on production sites.
Comment #2
vlad.pavlovic CreditAttribution: vlad.pavlovic commentedThis patch works well the first time. So if the user clicks save, it shows the form again. Unfortunately, if you click on submit again after the failed validation submit, it returns a 500 Internal server error and the form disappears.
Comment #3
lslinnet CreditAttribution: lslinnet commentedThis is due to the form being located in a file that is not included doing the validation process, can be fixed by making sure that the specific file is included.
If we take the media and file_entity modules, we need the file_entity.pages.inc file to be included.
To archive that we need some code like below alternatively this code could be included in file_entity's media_edit form but not sure if it belongs there or it's actually multiform's responsibility to include the correct files.
the form_load_include adds the information about which files should be loaded doing validation and submit execution even if the first validation returns an error.
Hope that helps you or points you in the correct direction.
Comment #4
ezman CreditAttribution: ezman commentedHi, thanks a lot for this patch - it's helped me get 90% of the way to achieving the functionality I need.
I have my module generating a block which contains a multiform containing all nodes owned by the logged-in user.
I unset all of the fields except for two - the title and the entityreference field. Only the title field is a required field in the node's fields definition.
Entityreference view widget's default functionality is to submit via AJAX. I took your code for the /system/ajax path and repurposed it.
I then combined your AJAX callback with Entityreference View Widget's AJAX callback.
It almost worked straight away. It processes the AJAX POSTed form, and then returns HTML back to replace what was there.
A problem I had is that the IDs are wrong in the returned HTML, which results in it replacing the wrong field (the first one).
I worked around this by creating my own version of _multiform_get_form() which rewrites the IDs and the names of the form returned by Entityreference view widget's processed form.
The problem I'm having now is on submitting the multiform.
I'm getting errors because only one of the $multiform[#subforms] array actually has a form array attached to it on submit.
The first error I see is "Notice: Undefined index: form in multiform_delayed_submit_execute() (line 184 of /sites/all/modules/multiform/multiform.inc").
Investigating a little further, I've implemented hook_multiform_alter, which gets called on submit, to inspect the $multiform array, with:
In the output, I see that only the first form (the new node creation form) has a form and form_state attached to it. The others have their form_id, args, #tag, key, and #weight, but no form or form_state.
I've tried it with AJAX submit and without, and get the same result. I can't see where the other forms in the multiform array have gone to.
I suspect a validation problem, but I'm not sure how to go about resolving it.
I want my multiform's submit functionality to process and save what's changed for each of the nodes, and to not care whether the new node form is filled in or not.
It seems the current default functionality is to fail validation if any of the forms fail validation.
Do you have any ideas how I can achieve this?
Here's my module's relevant code in case there's something I'm doing that's obviously wrong, it also might help anyone who wants to use an entityreference view widget field in a multiform:
Comment #5
dustinsilva@gmail.com CreditAttribution: dustinsilva@gmail.com commentedBe advised I had to change
$hook == 'element_info'
to read$hook == 'element_info_alter'
in the following code.Comment #6
kpv CreditAttribution: kpv commentedHello, pls see https://drupal.org/sandbox/kpv/2234747 for another version. It was written from scratch (work is still in progress) and has some differences in multiform settings array. For reference see multifrom_example module.
Comment #7
kpv CreditAttribution: kpv commentedComment #8
timwoodThe patch from #1, with slight modification in #5 applies cleanly but throws the following error when used in conjunction with Media and Plupload modules.
If the Sandbox project by kpv is the answer to this it should probably be turned into a patch file so the maintainers can more easily review?
Comment #9
kpv CreditAttribution: kpv commentedAttached patch should be applied to 7.x-1.x branch.
Comment #10
kpv CreditAttribution: kpv commentedIf you are trying integration with other projects, pls consider changes in the way settings are passed into multiform_get_form().
Here is the code from example module.
Comment #11
kalman.hosszu CreditAttribution: kalman.hosszu commentedThanks for the work! It seems very good for the first look, good work. I have some minor suggestions bellow, please fix them and I will do a deeper review for the patch. Thanks for the work again!
Please remove unused spaces from the beginning of the empty lines.
Please use line breaks in code comments. Not only here, please fix it in the whole patch.
The first line of the code block comment should not longer than 80 characters.
What is the task? Please write the task after every @todo section.
Remove spaces form empty line.
Remove one space between "Implement"s and "hook" words.
Add this also to multiform package.
We need the old tests and we need to test the new functionality too.
Comment #12
kpv CreditAttribution: kpv commentedFixed 1-7 (from #11) and added tests from original 7.x-1.x branch (modified to correspond new api), yet without new functionality tests - let's discuss test cases.
Comment #13
kpv CreditAttribution: kpv commentedComment #14
kpv CreditAttribution: kpv commented@kalman.hosszu
Do you need help with patch at #12? I suggest to create 7.x-2.x-dev branch based on this patch, since it has different api. Also this would allow to use it in other projects.
Comment #15
andypostWould be great to see the new branch for the code
Comment #16
andypostProper title
Comment #18
kalman.hosszu CreditAttribution: kalman.hosszu commentedI created a new branch for this patch. I found some minor whitespace and "No newline at end of file" problems. I thinks we should create a new version after another review.
Comment #19
kpv CreditAttribution: kpv commentedIt seems that the patch wasn't applied correctly. Commit doesn't contain most of the patch content.
Comment #21
kalman.hosszu CreditAttribution: kalman.hosszu commentedFixed.
Here is the log of he patch:
Comment #22
kpv CreditAttribution: kpv commentedgreat! I will check those spaces.
Also, can we get a link to 2.x-dev on project page? Meanwhile I will prepare some docs.
Comment #23
kalman.hosszu CreditAttribution: kalman.hosszu commentedI have to change the version, because I get an error message when I try to create a snapshot from the new branch.
Comment #24
kalman.hosszu CreditAttribution: kalman.hosszu commentedFixed
Comment #25
joseph.olstadthis patch appears was applied to 7.x-2.x branch in commit 65cd39b4fb