I've created a module which uses hook_form_alter(). When a form is loaded (node/add) I hide a field, and the hook works fine. The problem appears when I do a preview of the form. When the form appears again I see the hidden field (the hook doesn't work). Why?

I hide the field through the line:


2 Answers 2


First, you should use #access = FALSE which will disallow access to a element. #type hidden is not safe, it means that it is printed as a hidden input field which can be easily changed by the user, for example with Firebug.

When you do that, you can also directly set #access on the field element, like $form['field_versio']['#access'] and it will disallow access to all subelements (if there are multiple).

Not sure why it doesn't work in Preview, try adding a drupal_set_message('test') call to see if your hook is called.

  • Thanks a lot for your advice, Berdir. I have already changed the way I hide the field 'versio'. And definetely, hook_form_alter doesn't run when I click on preview button. drupal_set_message('test') appears in the first load of the form but it doesn't appear in the preview page of the form. Why? I haven't found another hook for previews.
    – user709
    Apr 13, 2011 at 7:35

hook_node_validate() is the answer.

I have built a personal function X that includes a drupal_add_js function that I need to link to my form.

Depending on the form (content-type) I call my function X from hook_form_alter() (node/add first load) or from hook_node_validate() (node/add after previewing, validating,...).

function my_javascripts($form_id) {
  $path = drupal_get_path('module', 'MYMODULE');
  switch ($form_id) {
    case "CONTENTTYPE-node-form":

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {

function MYMODULE_node_validate($node, $form, &$form_state) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.