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:

  1. Go to page with a form, submit button is disabled - ok
  2. Uploaded sample xml file:

enter image description here

  1. Click the Remove button, form is refreshed but submit button is enabled instead of disabled
  • "You can't do it, because" would be a perfectly valid answer. – Mołot Sep 2 '13 at 10:27
  • @Mołot If a user adds the answer to the question, there is no need to flag the question for moderation attention, since you can edit any post. Moderators are proxies for something users can do. – apaderno Sep 2 '13 at 11:17

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.