What is the difference between hook_field_attach_validate() and hook_field_validate()?

by NRaf   Last Updated April 06, 2015 19:03 PM

I'm a bit confused as for the difference between these two hooks, and which one I should use for custom validation of attached fields in my code.

Is there a better way to define a custom validation for an attached field (I am attaching the field programatically if that helps)?

EDIT: Looking more into it, I don't really get all that much information in hook_field_attach_validate(). All I'm pretty much getting is the entity_type, entity object and an errors array.

Whereas with hook_field_validate(), I get the field I'm actually validating against, which, I'm assuming is where I need to put the bulk of my custom field validation logic. The only issue I'm having now is that I'm unable to find a function that will invoke this method for me. Should I manually invoke this function or is there an API function I can use in this instance?

Tags : hooks


Answers 2


hook_field_validate() Should be used to validate the form for your modules fields.

hook_field_attach_validate() works on field_attach_validate() which is more for back end validation.

This function does not perform field widget validation on form submissions. It is intended to be called during API save operations. Use field_attach_form_validate() to validate form submissions.

Are the fields ones which your modules is creating, or are they fields from a third party module?

The #element_validate property may be what you need to validate another modules fields.

Jeremy French
Jeremy French
August 02, 2011 16:07 PM

In case anyone else like me finds this old question, here is a bit more information based on my own experiments and searching.

  • There is now a D7 module Field Validation.
  • It looks as if hook_field_validate() is only called for the module that defines the field, so that's no good for a standard field.
  • If you use #element_validate I think it would only affect the standard edit form - so for example not edits via View Bulk Operations. Maybe this doesn't matter to many people.
  • You can use hook_field_attach_validate(), as it is called from form validation in field_attach_form_validate(). This hook is what the Field Validation module uses. Everything you need is inside the entity parameter.

Sample code below validates a phone number for the UK.

function XXX_field_attach_validate($entity_type, $entity, &$errors) {
  if (!empty($entity->field_telephone_number)) {
    foreach ($entity->field_telephone_number as $langcode => $items) {
      foreach ($items as $delta => $item) {
        if (!preg_match('/^0(\s*\d){9,10}$/', $item['value'])) {
          $errors ['field_telephone_number'][$langcode][$delta][] = array(
            'error' => 'XXX_telephone_number',
            'message' => t('Telephone number must be 10 or 11 digits starting with 0.'),
          );
        }
      }
    }
  }
}
AdamS
AdamS
April 06, 2015 16:43 PM

Related Questions


How to find hooks being called by a Drupal page?

Updated April 03, 2015 21:20 PM

Modify text of error message for required field

Updated April 22, 2015 20:03 PM

Where are hooks stored/cached?

Updated April 13, 2015 20:03 PM