4

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?

2 Answers 2

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.

1
  • I'm creating custom fields based on the default field types: i.e. creating a city field using field_create_field() with a 'text' field type. In this case, would I use #element_validate?
    – NRaf
    Aug 2, 2011 at 21:02
0

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.'),
          );
        }
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.