2

I want to add custom validation to all node edit forms. Specifically, will make any image field alt and title attributes required. But I haven't even gotten to that piece yet. So far, I have the following code, but for some reason my validation helper function is never called:

<?php

/*
 * Implements hook_form_alter()
 */
function image_a11y_form_alter($form, &$form_state, &$form_id) {

  // Do this for ALL node edit forms (not specific form ID)
  if (isset($form['#node_edit_form']) &&  $form['#node_edit_form'] == TRUE) {
    $form['#validate'][] = 'custom_validate';
  }
}

function custom_validate($form, &$form_state){

  // Custom validation here

}

I've followed several examples I found online and double checked the Drupal.org API doc. I don't know what I'm doing wrong.

Chris L
  • 41
  • 1
  • 4

1 Answers1

4

You are doing fine, just need to reference $form var on hook_form_alter()

hook_form_alter(&$form, &$form_state, $form_id)

And never forget to clear cache before you dive in to check. :)

Jeet
  • 1,587
  • 1
  • 9
  • 22