I have a form with two '#type' => 'managed_file'
fields.
Anonymous user selects a file on field 1, clicks the Upload. Ajax upload occurs and the browse widget is replaced by the filename uploaded and the Remove button, as expected.
user does same on field 2, which works same way. Form now displays with two uploaded files, as expected.
user clicks main (non ajax) submit button. Form complains that file field 1 is missing
Other observations
If I disable ajax, I notice that on clicking Upload on one managed file field, the other one loses its file!
If I don't use the managed file upload button at all, and supply one file then press submit, the file shows as if uploaded, shows validation error about file 2 (as expected). However, if I immediately press submit again I would expect the same state to be preserved, but instead I see now that both file fields are again blank.
Is this bug/limitation in Drupal (7) or am I doing something stupid?
Here's my code:
function mymod_formtest($form, &$form_state) {
$form['f1'] = [
'#type' => 'managed_file',
'#required' => TRUE,
'#upload_validators' => [
'file_validate_extensions' => ['doc docx odt pdf'],
],
'#upload_location' => 'private://test/',
'#title' => t('Upload 1'),
'#default_value' => isset($form_state['values']['f1']) ? $form_state['values']['f1'] : '',
];
$form['f2'] = [
'#type' => 'managed_file',
'#required' => TRUE,
'#upload_validators' => [
'file_validate_extensions' => ['doc docx odt pdf'],
],
'#upload_location' => 'private://test/',
'#default_value' => isset($form_state['values']['f2']) ? $form_state['values']['f2'] : '',
'#title' => t('Upload 2'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'go',
];
return $form;
}