function file_managed_file_validate

An #element_validate callback for the managed_file element.

1 string reference to 'file_managed_file_validate'
file_element_info in modules/file/file.module
Implements hook_element_info().

File

modules/file/file.module, line 620

Code

function file_managed_file_validate(&$element, &$form_state) {
    // If referencing an existing file, only allow if there are existing
    // references. This prevents unmanaged files from being deleted if this
    // item were to be deleted.
    $clicked_button = end($form_state['triggering_element']['#parents']);
    if ($clicked_button != 'remove_button' && !empty($element['fid']['#value'])) {
        if ($file = file_load($element['fid']['#value'])) {
            if ($file->status == FILE_STATUS_PERMANENT) {
                $references = file_usage_list($file);
                if (empty($references)) {
                    form_error($element, t('The file used in the !name field may not be referenced.', array(
                        '!name' => $element['#title'],
                    )));
                }
            }
        }
        else {
            form_error($element, t('The file referenced by the !name field does not exist.', array(
                '!name' => $element['#title'],
            )));
        }
    }
    // Check required property based on the FID.
    if ($element['#required'] && empty($element['fid']['#value']) && !in_array($clicked_button, array(
        'upload_button',
        'remove_button',
    ))) {
        form_error($element['upload'], t('!name field is required.', array(
            '!name' => $element['#title'],
        )));
    }
    // Consolidate the array value of this field to a single FID.
    if (!$element['#extended']) {
        form_set_value($element, $element['fid']['#value'], $form_state);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.