2

I'd like to clear the managed file field each time the contents are sent off for further processing on submit.

The callbacks work fine, but I have repeatedly come unstuck in my goal of having a "fresh" form everytime I need to submit a new image.

The form is defined thus:

function mymodule_form($form, &$form_state) {

$form['Input type'] = array(
    '#type'=>'fieldset',
    '#title'=>'Upload your image'
);

$form['Input type']['file'] = array(
    //file left in non-permanent state for testing purposes
    '#type' => 'managed_file',
    '#title'=> t('Upload image'),
    '#description' => t('Choose an image to upload. Format must be gif, png, jpg, and may not exceed 2MB in size. Press Submit to confirm your selection.'),
    '#upload_validators' => array(
        'file_validate_extensions' > array('gif png jpg jpeg'),
        'file_validate_size'=>array(512*512*512),
    ),
    '#upload_location'=>'public://mymodule_uploads',
);

$form['Input type']['submit'] = array(
    '#type'=>'submit',
    '#value' =>t('Submit'),
    '#submit'=>array('mymodule_clear_image_submit'),
    '#ajax' => array(
            'callback'=> 'mymodule_update_image_callback',
            'wrapper'=>'somediv'
        ),
);
return $form;
}
function mymodule_update_image_callback($form,&$form_state) {
$picture_settings = array( 
        'image' => file_create_url($form['Input type']['file']['#file']->uri)
        );
    drupal_add_js(array('mymodule' => $picture_settings),'setting');

    return;
}

function mymodule_clear_image_submit($form,&$form_state) {
$form_state['complete form']['Input type']['file']['#file'] = NULL;
$form_state['rebuild']=TRUE;
return;
}

Neither the submit nor the callback functions seems to be refreshing my form. Setting the file value to NULL has not had an effect either.

I don't know what else to try. Cheers!

4
  • Try removing 'file' from $form_state['input'] and $form_state['values']
    – Clive
    Commented Sep 25, 2014 at 10:27
  • That does seem to remove the file, but it doesn't update the form. I need the managed_file field to be empty after submitting.
    – Alex
    Commented Sep 25, 2014 at 11:22
  • Sorry I'm not getting you - unless the field has a #default_value, it will be empty next time the page is loaded
    – Clive
    Commented Sep 25, 2014 at 12:05
  • Sorry, what I am looking for is a way to refresh the form using ajax (ie that the field is emptied without having to reload the entire page)
    – Alex
    Commented Sep 25, 2014 at 13:23

1 Answer 1

1

I found a solution to this problem here: https://www.drupal.org/node/1578656

The idea is to add the following keys to the 'file_managed' field at the time you want to clear it:

$form[FORMKEY]['#file'] = FALSE;
$form[FORMKEY]['filename'] = array();
$form[FORMKEY]['#value']['fid'] = 0;

In your case it should be something like this in the form builder function:

if ( isset($form_state['storage']['reset-file-field'])
  && $form_state['storage']['reset-file-field'] === true
) {
  $form['Input type']['file']['#file'] = FALSE;
  $form['Input type']['file']['filename'] = array();
  $form['Input type']['file']['#value']['fid'] = 0;

  $form_state['storage']['reset-file-field'] = false;
}

And then in your submit handler:

$form_state['storage']['reset-file-field'] = true;

The if-block is necessary to prevent the upload field being reset when you press 'upload', thus making it useless.

3
  • 1
    It worked form me. Thanx
    – Clemorphy
    Commented Apr 22, 2021 at 14:47
  • @Clemorphy That is such a perfect typo :) And glad I could help here almost 5 years later.
    – Nicolaj
    Commented Apr 23, 2021 at 13:35
  • Haha I just discover my typo. My subconscious made me use this word ;-)
    – Clemorphy
    Commented Apr 24, 2021 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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