Why submit button is losing disabled attribute after deletion of uploaded file? 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, &amp;$form_state) {

    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['xml_file'] = array(
        '#field_prefix'      =&gt; '&lt;span class="description"&gt;'.t('Only XML files are allowed').'&lt;/span&gt;',
        '#type'              =&gt; 'managed_file',
        '#title'             =&gt; '',
        '#default_value'     =&gt; '',
        '#upload_location'   =&gt; CUSTOM_MODULE_UPLOAD_DIR,
        '#upload_validators' =&gt; array('file_validate_extensions' =&gt; array('xml')),
        '#required'          =&gt; TRUE,
    );

    $form['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Submit'),
    );

    return $form;
}


The form alter code:

function custom_module_FORM_ID_form_alter(&amp;$form, &amp;$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
You can't do it, because on remove event, only HTML for file input field is reloaded. There is a need of using custom AJAX callback for this (such as ajaxSuccess). I don't know nothing about Drupal Ajax callbacks for this type of event too.