You are here

function _filefield_filefield_validators in FileField 6.2

Implementation of hook_filefield_validators(): Upload restrictions for file size, file extension and supported file widgets. Implemented as private function instead of as a real hook, because we want to make an exception so that these requirements appear first in any list.

1 call to _filefield_filefield_validators()
_filefield_validator_info in ./filefield.widget.inc
Retrieve an array of file validators and their associated requirement messages (placing filefield's own validators first in the result array).

File

./filefield.widget.inc, line 293
FileField: Defines a CCK file field type.

Code

function _filefield_filefield_validators($field, $widget, $existing_files) {
  $validators = array();

  // Thanks to the php.ini restrictions, there is always a maximum file size.
  // Therefore we can rely on at least one restriction always being in force.
  $max_filesize = _filefield_maximum_filesize($field, $widget, $existing_files);
  $filesize_message = $max_filesize > 0 ? t('Maximum file size: !size.', array(
    '!size' => format_size($max_filesize),
  )) : t('The allowed maximum file size total has been exceeded.');
  $validators['file_validate_size'] = array(
    'validator arguments' => array(
      $max_filesize,
    ),
    'requirement message' => $filesize_message,
    'upload possible' => $max_filesize > 0,
  );
  if (!empty($widget['file_extensions'])) {
    $validators['file_validate_extensions'] = array(
      'validator arguments' => array(
        $widget['file_extensions'],
      ),
      'requirement message' => t('Allowed extensions: %ext.', array(
        '%ext' => $widget['file_extensions'],
      )),
    );
  }
  $supported_file_widgets = _filefield_supported_file_widgets($widget);
  if (!empty($supported_file_widgets)) {
    $validators['filefield_validate_file_widget_support'] = array(
      'validator arguments' => array(
        $field,
        $widget,
        $supported_file_widgets,
      ),
      'requirement message' => t('Uploads are restricted to the following categories: !widgets.', array(
        '!widgets' => implode(', ', $supported_file_widgets),
      )),
    );
  }
  return $validators;
}