0

I'm using the Form API to create a custom form. It has a managed_file field and some other fields, some of which are required. If any of the required fields are not filled in, the file upload for the managed_file field doesn't work, and produces an error informing the user to fill out the required field first before the image upload can process.

I'm assuming this is because the form is being rebuilt when the form uploads, but is there a way to stop it from checking for required fields? Surely there is a way, as node edit forms have this functionality working.

Here is my form code (simplified):

function create_event_form($form, &$form_state) {

  $form['event_image'] = array(
    '#type' => 'managed_file',
    '#upload_validators' => array(
      'file_validate_extensions' => array('jpg jpeg gif png')
    ),
    '#upload_location' => 'public://event_images/',
    '#required' => FALSE,
  );

  $form['event_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Event Name'),
    '#required' => TRUE,
  );

  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Create Event'),
  );

  return $form;
}

If event_name field is empty when attempting to upload a file, the upload will not process.

0

This was happening due to the Clientside Validation module being enabled.

For anyone having this issue, I had to override the form settings in Clientside Validation for this particular form, setting "Validate fields on submit" to "No".

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.