0

I have an element with set #element_validate (in hook_element_info) and I use this element in a custom form. The problem is that when I try to add an additional #element_validate to the element in the form it overrides the original validate callback (the one set in hook_element_info).

So my question is, is there any way how to add an additional #element_validate in a custom form without having to use form_alter?

hook_element_info:

'ELEMENT_NAME' => array(
  '#input' => TRUE,
  '#tree' => TRUE,
  '#process' => array(..),
  '#element_validate' => array('original_validate'),
),

form:

$form['TST'] = array(
  '#type' => 'ELEMENT_NAME',
  ...
);
$form['TST']['#element_validate'][] = 'new_validate';

And then only new_validate is present when on 'process' callback of the element (as well as when the form is build). When I don't set the 'new_validate' the 'original_validate' is still present on 'process'.

Other elements (tried a few date ones) seem to behave the same way. E.g. #element_validate added in form declaration overrides the own element's one defined in hook_element_info.

1

You can create validate callback for the entire form and validate the element in that callback.

$form['#validate'][] = 'new_validate'; 
Also separate validate callbacks can be attached to 'submit' ('preview', 'cancel' etc) buttons within the form. And the element can be validated there as well.
$form['submit_button']['#validate'][] = 'new_validate'; 

| improve this answer | |

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.