0
 function mymodule_form_alter(&$form, &$form_state) {

            if ($form['#form_id'] == 'article_node_form') {         
              $form['#validate'][] = '_mymodule_test_validation';
                }
    }

    function mymodule_test_validation($form, &$form_state) {
        dsm("hello");
    }

Why is not printed the word "hello"?? What is my error in this code.

1

Please see right way of doing form alter below

mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form_id = $form['form_id']['#value'];
  if ($form_id == "article_node_form") {
    $form['#validate'][] = 'mymodule_test_validation';
  }
}

function mymodule_test_validation($form, &$form_state) {
  if(($form_state['values']['test_field']) == ''){
    form_set_error('test_field', t('You must select a name for this group of  settings.'));
  }
}

FYI, hook_form_alter(), Form API reference #validate and form_set_error()

  • In my case if I put $form_id == "article_ndoe_form" appears the next error: Notice: Undefined variable: form_id Thanks for your help. – cabita Sep 13 '12 at 10:24
  • I'm going to update in your answer the code worked for me. Thanks for your help. – cabita Sep 13 '12 at 10:39

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.