When generating a file upload form element using AJAX in a custom form, the element breaks when the upload button is pressed and it's ajax function is called. The form element completely disappears, and the values from the other form elements are removed from form_state. The following errors appear:
Notice: Undefined index: image in file_ajax_upload()
Notice: Undefined index: #suffix in file_ajax_upload()
The form element itself completely disappears from the dom.
The fix in the following do not work:
https://drupal.org/node/1067470
https://drupal.org/node/1067470
As they are specifically related to node forms, whereas this is just a standard form api form.
Attached a module to reproduce, although it is possible to also add a managed file element to any of the dynamic ajax forms in the examples module and see the same results.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | post_approval_test.zip | 2.12 KB | debaryadas |
| post_approval_test.zip | 1.81 KB | michaelmallett |
Comments
Comment #1
michaelmallett commentedReference for more information
https://drupal.org/node/2208855
Comment #2
debaryadas commentedThe $form_state['values']['approve'] will not be available when `Upload' button is clicked. But if image is successfully uploaded then
$form_state['values']['image'] and $form_state['values']['image_upload_button'] will be available. So the logic for the appearance of `Artwork`
image upload button should be changed. The `Artwork` image upload button will be available when the value of $form_state['values']['approve']
is 1 or $form_state['values']['image'] and $form_state['values']['image_upload_button'] are set. This will prevent those errors as indicated by you.
I have attached a revised version of your module. Check it and reply whether it works for you or not.
Comment #3
debaryadas commentedA small change in the previous logic.
With Regards
Debarya Das
Comment #4
debaryadas commentedComment #5
michaelmallett commentedIs that desired behaviour? The image upload widget should not be available until 'approve' is selected in and $form_state['approval'] is set. This 'approve' switch is to generate the 'approve' form, of which the image upload is part.
So taking it out of the switch but checking for various other form states to check for it is the way to go. That doesn't seem like intended behaviour, quite hacky no?
Thanks for your help
Comment #6
michaelmallett commentedCan you please clarify if this is desired behaviour?
Comment #7
debaryadas commentedThis is not a desired behaviour if you look from the point of view of your requirement. But if you look from Drupal's form handling behaivour then
it is desired behaviour due to the following reason.
In Drupal, the $form_state array contain informmation related to the form fields submitted. In this case when the radion button is clicked, the form is re-built
with the changes that you indicated in your callback. So when you click on the 'approve', the $form_state contain the selected value along with the other values
form elements and depending on that value you can add/remove new form elements. Now if you create a file upload field using 'managed_file', then it generates a browse button along with an
'Upload' button. Now Drupal does a trick. The 'Upload' button contain an attribute named 'limit_validating_errors'. The purpass of this attribute is to let know Drupal's
form API which field need to be validated. Since you are trying to upload a file/image, so this is only field that need to be validated when you click on 'Upload'
button. Drupal implements this checking at 'drupal_validate_form' in includes/form.inc. In this function, it is checked whether the 'triggering_element' contains
'#limit_validation_errors' or not. If it exists (an array containing the form elements need to be validated) then Drupal perform validation only for those fields
and the rest of fields ignored. It is the section where the $form_state is re-structured and in your case it removes form elements like $form_state['values']['approve']
form $form_state. It only keeps $form_state['values']['image'] and $form_state['values']['image_upload_button'] since this form element has been changed and
removing other form elements because that have not been changed and can introduce security threat.
You can use #states to show/remove the form elements instead of using switch statements.
Comment #8
michaelmallett commentedThankyou for the clarification, much appreciated
Comment #9
fnandogp commented#3 worked for me.
Thank you!