Lets say for example I have a module "nospam".  It's purpose is to check the content a user would submit in the site wide contact form (https://api.backdropcms.org/api/backdrop/core!modules!contact!contact.pa...).

For testing purposes, I have this function in my module:

function nospam_contact_site_form_validate($form, &$form_state) {
            form_set_error('message', t("Spam"));
}

When I try to submit the form, I do not see the error above and the submission proceeds normally.

Accepted answer

@BWPanda is correct.

If you want to validate a form created by other modules, you need to create a hook_form_alter() or hook_form_FORM_ID_alter() function and put something like what is in that validate link above in it.

Something like this untested snippet:

function nospam_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'nospam_contact_site_form_validate';
}

That might not be entirely correct, but it should get you on the path.

 

Most helpful answers

Yep, using hook_form_FORM_ID_alter() is generally the preferred method for altering a single form. But just for clarity, if you wanted/needed to use the regular hook_form_alter() function to affect one form only, you'd simply do this:

function nospam_form_alter(&$form, &$form_state, $form_id){
    if ($form_id == 'contact_site_form') {
        $form['#validate'][] = 'nospam_form_validate';
    }
}

Comments

You'll need to add your custom validation function to the form via hook_form_alter(). See https://api.backdropcms.org/form_api#validate for details.

EDIT: I believe naming a function with '_validate' on the end of the form name only works when your module has created that form itself. When you're editing an existing form, you'll need to alter the form to add your validation function manually as above.

oadaeh's picture

@BWPanda is correct.

If you want to validate a form created by other modules, you need to create a hook_form_alter() or hook_form_FORM_ID_alter() function and put something like what is in that validate link above in it.

Something like this untested snippet:

function nospam_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'nospam_contact_site_form_validate';
}

That might not be entirely correct, but it should get you on the path.

 

Great, it works!

I thought I was doing something wrong when I tried that approach before.  However thanks to your replies (confirming the correct way), I was able to find out that I needed to flush the caches for the changes to the module's code to get picked up.

Using:

function nospam_form_alter(&$form, &$form_state, $form_id){
        $form['#validate'][] = 'nospam_form_validate';
}

Works for all forms, and:

function nospam_form_contact_site_form_alter(&$form, &$form_state, $form_id){
        $form['#validate'][] = 'nospam_form_validate';
}

does indeed alter the site-wide contact form, triggering this validation function:

function nospam_form_validate($form, &$form_state) {
         form_set_error('message', t("Spam"));
}

 

 

Yep, using hook_form_FORM_ID_alter() is generally the preferred method for altering a single form. But just for clarity, if you wanted/needed to use the regular hook_form_alter() function to affect one form only, you'd simply do this:

function nospam_form_alter(&$form, &$form_state, $form_id){
    if ($form_id == 'contact_site_form') {
        $form['#validate'][] = 'nospam_form_validate';
    }
}