0

I have a pretty specific case where I want to fill in the node title myself if the user fills a checkbox above the title field.

For this reason, I can't use autonodetitle (because the title is only automatic if the checkbox is selected).

I have added my own validation callback using array_unshift() in a hook_form_alter to make sure its the first one but when its called the form already has an error (title field is required)

Is there any way I hook into the validation process earlier or even just remove the error for this field?

Thanks!

  • where did you add the array_unshift(), in a hook_form_alter()? Also, feel free to share what is the error. – amateur barista Jan 25 '12 at 16:26
  • yeah in a hook_form_alter(), the error is that title field can't be empty. – Alex Weber Jan 25 '12 at 17:32
1

You can easily hook into the validation process by implementing hook_form_alter(&$form, &$form_state, $form_id) or it's cousin hook_form_FORM_ID_alter(). There you can inspect the $form variable, find out where the array of validation functions is, and perform array_unshift() on it.

You could also use the form alter hooks to modify the #required property of your title field, therefore preventing further validation errors down the line.

Don't forget that in order for your array_unshift() operation to be successful, the form needs to be passed by reference to your hook, or else you would only be modifying a local copy of the form variable. It's very easy to forget adding the ampersand & in front of $form in the hook implementation. i.e. ...& $form...

| improve this answer | |
  • I already did that and no go! :( – Alex Weber Jan 25 '12 at 17:31
  • If you put error_log(__FUNCTION__) in each of the functions mentioned in the validator array, what do you get? Is your validator truly the first? If it is, then your problem is beyond hook_form_alter() and it might lie in some core code. If it not the first, then you might be missing a step from my solution. – amateur barista Jan 25 '12 at 17:51
  • If you can confirm 100% that your validator is the first, and by the time you break into your validator you have an error in the form, then most likely form_error() is being called outside a validator (read: no-no, naughty code). If this is indeed the case, then just break into form_error() and see who's calling it. – amateur barista Jan 25 '12 at 17:58
  • Also, have you checked out hook_form_BASE_FORM_ID_alter? I've never used it, but could it be that the form you are targeting has a base form, which also specifies a "base form validator"? – amateur barista Jan 25 '12 at 18:03
  • Good catch, I'll try that out! – Alex Weber Jan 26 '12 at 1:57

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.