The task is simple: submit button must be enabled only if file is attached to file input.
The form code:
function custom_module_form($form, &$form_state) {
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['xml_file'] = array(
'#field_prefix' => '<span class="description">'.t('Only XML files are allowed').'</span>',
'#type' => 'managed_file',
'#title' => '',
'#default_value' => '',
'#upload_location' => CUSTOM_MODULE_UPLOAD_DIR,
'#upload_validators' => array('file_validate_extensions' => array('xml')),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
The form alter code:
function custom_module_FORM_ID_form_alter(&$form, &$form_state, $form_id) {
if (! isset($form_state['clicked_button'])
|| $form_state['clicked_button']['#name'] == 'xml_file_remove_button') {
$form['submit']['#disabled'] = TRUE;
}
}
It works, but when I removed uploaded file by Remove button, refreshed form has enabled submit button, even if I am sure that above code was executed. What am I doing wrong?
Use case:
- Go to page with a form, submit button is disabled - ok
- Uploaded sample xml file:
- Click the Remove button, form is refreshed but submit button is enabled instead of disabled