3

I'm unsure which one you should use for a form_validation.

Seeing that _form_validate() starts with a _ makes it a private function following the Drupal coding standards if I'm not mistaken.

So my guess is that a form validation should be done with hook_validate().

But the question remains: what's the difference between the two and where should they be used?


EDIT: I misunderstood the hook_validate() which is not the _validate "hook" used in the form API. I'll leave the question unchanged so other users can learn from my mistake.

7

You're totally correct, Drupal's coding standards denote anything with a _ prefix is seen as a pseudo-'private' function. As such the _form_validate() function was never meant to be called directly. Unless you really need to use it, and you're absolutely positive there's no other way to accomplish what you want, I wouldn't advise using it.

In fact only one function in the whole core system calls _form_validate(): drupal_validate_form(). It would stand to reason that you should be using this function instead (being that it's not marked as a private function).

These are the functions that Drupal uses internally to validate a managed form, so if you're using the Form API correctly you should never need to call them. Adding validate handlers to a form to validate individual elements is the correct way to do things.

Just to clear up a wee bit of terminology as well, hook_validate() has nothing to do with the form API as such; it's used by node-providing modules to validate nodes before saving. The validation handlers that you add to forms (with custom forms, or through hook_form_alter(), don't have a particular 'Drupal name' that I'm aware of.

Also, you don't actually even need to use the standard named _validate function if you don't want to...you can add a function with any name as a form validate handler:

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

Drupal just looks for FORMNAME_validate() by default, and runs it if it exists.

6
  • I see now, have to implement the form validation in the definition of the form fields. – FLY May 9 '12 at 9:17
  • 1
    Yep :) The Form API Quickstart Guide has some good information on the subject in general if you're interested, in particular the Validating Forms section – Clive May 9 '12 at 9:19
  • Thanks! that's exactly what I was looking for! so the _validate() in the form API is not the hook_validate() but some hidden hook in the form API :) – FLY May 9 '12 at 9:24
  • 3
    No, it's just another one of Drupal's naming conventions; you don't even need to use it...you can add a function with any name as a form validate handler ($form['#validate'][] = 'my_strangely_named_function';, etc.). Drupal just looks for FORMNAME_validate() be default, and runs it if it exists – Clive May 9 '12 at 9:28
  • @Clive You probbly want to add that last comment to the question body :) – Berdir May 9 '12 at 12:53
3

hook_validate performs node validation before a node is created or updated. This hook is invoked only on the module that defines the node's content type.

_form_validate checks if all required fields are filled and if max lengths of text field is not exceeded. If it finds any errors of such kind it fires form_error or form_set_error functions.

You need to use hook_validate when you define a new node's content type in your module and want to do some customized validation for this content type. This hook won't work for non-node forms.

Generally you won't need to call _form_validate, because it is being called automatically during form submission process.

The best practice to validate an arbitrary form is to create a custom validation function.

For example you have a function that defines your form:

function YOURFORM_form(){
    // form definition
}

The custom validation function for this form would be

function YOURFORM_validate(){
    // form validation
}
1
  • I never said there was :) – Alex Petrov May 9 '12 at 9:10
2

I suppose it's a custom form that you'd like to validate?

If your form function is called mymodule_form(), then you should add a function called mymodule_form_validate($form, &form_state). That function will be called automatically, and you'll be able to validate the submitted values using $form_state['values'].

If there are errors, you should call form_set_error(), which will prevent the submission and notify the user.

hook_validate() is only used to validate node submissions, if your module provides a content type, and can't be used to validate any other forms.

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.