2

My users always skip adding the "description" to the file field because they can. I tried implementing the code posted here:

How do I make Drupal File Description Field Required?

(and thank you) -- but I have a question. My users now do see a red asterisk suggesting that the description is required ... but if they do not enter a description, they may still click save and create the node ... with no description entered in the box. Any ideas on how to get this to really force the issue and require the description?

0

If you like you can add to the hook_field_widget_form_alter and then add a validation callback:

  $form['#validate'][] = 'my_validate_function';

Then create a my_validate_function($form, &$form_state) which contains a check on the values and calls a form_set_error($name, $message) if a value is wrong or missing.

/**
 * Implements hook_field_widget_form_alter().
 */
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['field_name'] == 'field_MYFIELD' && $context['instance']['widget']['type'] == 'file_generic') {
    $context['form']['#validate'][] = 'my_validate_function';
  }
}

/**
 * Require description to be set.
 */
function my_validate_function($form, &$form_state) {
  if ($form_state['values']['description'] == '') {
    form_set_error('description', 'Description must not be empty.');
  }
}

This may need to be changed above:

$form_state['values']['description']
  • this is probably good direction ... but I can't figure out how to append this info to the other code snippet / custom module. I tried googling drupal form validation for required fields, but could not work it out. – drupal curious Mar 15 '16 at 7:30

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.