1

I'm sure there's some simple reason that my custom validate function isn't executing.

Here's my code:

function MYMODULE_form_message_ui_instance_message_manage_alter(&$form, &$form_state, $form_id) {
  array_unshift($form['#validate'], 'MYMODULE_validate_relationship');
  watchdog("form", "<pre>" . print_r($form, true) . "</pre>");
}

function MYMODULE_validate_relationship($form, &$form_state) {
  watchdog("validate form state", "<pre>" . print_r($form_state, true) . "</pre>");
  watchdog("validate form", "<pre>" . print_r($form, true) . "</pre>");
}

On form submission, the first watchdog - "form" is being exected, however the second and third are not. Why is this? I can't find any information on why a custom validate hook wouldn't execute.

EDIT per Pierre's request:

I am writing a custom validation function that I am trying to execute before the other validation functions. I am doing this using array_unshift. This seems to be working, as my #validate array looks like this:

[#validate] => Array
    (
        [0] => MYMODULE_validate_relationship
        [1] => message_ui_instance_message_manage_validate
        [2] => message_private_form_message_private_instance_message_manage_validate
    )

However the code in MYMODULE_validate_relationship is never executing.

As far as I can tell, $form and &$form should be irrelevant in the validate function, because once you're at the validation stage, nothing should actually be modifying the form (I think?). And most examples I was seeing did not including passing the $form by reference. And I've tried both ways, and in neither way is the validate hook firing.

5
  • Consider EDITING your question to better explain what you're trying to achieve with the code shown in youir question. Especially about using &$form in the first function, and $form in the second function, and also what you believe / think how the array_unshift line updates the $form. In doing so, somebody may notice some error in your explanation .... Jul 6, 2015 at 6:26
  • cleared cache? What about trying something like $form['#validate'][] = 'MYMODULE_validate_relationship'; ? Jul 6, 2015 at 9:05
  • Yeah, I've tried both of those. The reason I decided to put my validation function first was because $form['#validate'][] wasn't working. The form in question is for Message Private module, which is built on the Message stack.
    – Jack Ryan
    Jul 6, 2015 at 9:09
  • if you don't use function array_unshift ( $form [ '#validate' ], 'MYMODULE_validate_relationship' ); do your function validate excute?
    – MrD
    Jul 6, 2015 at 10:47
  • No, I started using array_unshift specifically because it wasn't working the normal way.
    – Jack Ryan
    Jul 6, 2015 at 10:48

1 Answer 1

2

The answer is that the submit button was being generated by the actions array item.

Simply changing my form value to this:

$form['actions']['submit']['#validate'][] = 'MYMODULE_validate_relationship';

Correctly allowed it to be executed.

https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#actions

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .