0

From what i've learned today, there are two ways one can go about validating input.

One is the #element_validate field which is passed along the form hook, and the other is the validate hook.

Why use the validate hook if input can be validated with the #element_validate field? Is it a necessity to implement the validate hook in this case?

Or is it a method that should be implemented for complex validation while all simple validation(is number, is date, is url) can be done via the #element_validate option?

1

@Haider Ali with #element_validate, we can add our own custom validation functions to a field or we can pass the validation functions provided by Drupal, e.g., element_validate_integer_positive(), element_validate_number(), etc.

This #element_validate works only on the field we added it to.

When we want to add custom validation to multiple fields, we add it in form validate hook, so that we do not need to add separate function to each field and call multiple validate functions.

  • Yeah further reading into the API just told me the same thing, thank you however. :) However, the validate hook stops execution after first error is thrown, it doesn't seem possible to make a list of errors out of it. Which might be possible from the #element_validate field – Haider Ali Jan 15 '16 at 9:21
  • What kind of errors? – Dev Jan 15 '16 at 9:46

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.