How to intetrupt a form submission in drupal I have made a custom module where I create one page. In that page I load the existing form for creating a new content and also add a password field. In my loaded form I have an email field. I want before submitting the form to check if there exists a user with the username the value found in the email field and the password offered by the password field. Here I have 3 scenarios:


if the user does not exist I take the email and password and create an account then create the content
if the user exists and the password is correct , I create the content
if the user exists but the password is incorrect I stop the form submission 


My problem is I don't know how to stop the form submission ( I am refering to scenario number 3). Any suggestions are most aprerciated

Here are the callback for the page function:

function add_new_article_simple_page() {
  module_load_include('inc', 'node', 'node.pages');
  $node_form = new stdClass;
  $node_form-&gt;type = 'announcement';
  $node_form-&gt;language = LANGUAGE_NONE;
  $form = drupal_get_form('announcement_node_form', $node_form);
  return $form;
}


the alter function to insert the password field :

function add_new_article_form_alter(&amp;$form, &amp;$form_state, $form_id){
  if($form_id=='announcement_node_form')
  {
    $form['#after_build'][] = 'add_new_article_after_build';
    $form['account_password'] = array(
        '#title' =&gt; 'Parola',
        '#type' =&gt; 'password',
        '#required' =&gt; TRUE,
    );
    $form['#submit'][] = 'add_new_article_form_submit';

    return $form;
  }
}


and the form submit function

function add_new_article_form_submit($form, &amp;$form_state){
  $email=$form_state['values']['field_email']['und'][0]['value'];
  $password=$form_state['values']['account_password'];
  //check if the email even exists
  if(!db_query("SELECT COUNT(*) FROM {users} WHERE name = '".$email."';")-&gt;fetchField())
  //create the new account
  {     
    $edit = array(
        'name' =&gt; $email,
        'pass' =&gt; $password,
        'mail' =&gt; $email,
        'init' =&gt; $email,
        'roles' =&gt; array('4' =&gt; 'standard user'),
        'status' =&gt; 0,
        'access' =&gt; REQUEST_TIME,
    );
    $loc_var=user_save(drupal_anonymous_user(), $edit);
    $GLOBALS['new_user']=$loc_var-&gt;uid;     
  }
  else
  {
    //check if username + password are valid combination
    if($uid = user_authenticate($email,$password))
        //log in user after account creation
    else
        //this is where I want to interrupt the submission of the form
        form_set_error('account_password', 'Parola nu este buna pentru acest email.');
  }
}


When i test the 3rd scenario: The content is created , the page jumps to that content page and that is where the error message appears 

I have followed a suggestion to try hook_form_alter, so I wrote a simple validation function which should always give an error. The problem is that the content is still submited and saved. It doesn't seem to even call hook_form_validate .. Here is the function :

function add_new_article_form_validate($form, &amp;$form_state) 
{
    $email=$form_state['values']['field_email']['und'][0]['value'];
    $password=$form_state['values']['account_password'];
    form_set_error('account_password',t('The form is being validated.'.$email.' and '.$password));
}


Thank you,
Cristi
Try adding the validate function as you have added the submit function:

function add_new_article_form_alter(&amp;$form, &amp;$form_state, $form_id){
  if($form_id=='announcement_node_form')
  {
    $form['#after_build'][] = 'add_new_article_after_build';
    $form['account_password'] = array(
        '#title' =&gt; 'Parola',
        '#type' =&gt; 'password',
        '#required' =&gt; TRUE,
    );

    $form["#validate"][] = 'add_new_article_form_validate';
    $form['#submit'][] = 'add_new_article_form_submit';

    return $form;
  }
}