Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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:

  1. if the user does not exist I take the email and password and create an account then create the content
  2. if the user exists and the password is correct , I create the content
  3. 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->type = 'announcement';
  $node_form->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(&$form, &$form_state, $form_id){
  if($form_id=='announcement_node_form')
  {
    $form['#after_build'][] = 'add_new_article_after_build';
    $form['account_password'] = array(
        '#title' => 'Parola',
        '#type' => 'password',
        '#required' => TRUE,
    );
    $form['#submit'][] = 'add_new_article_form_submit';

    return $form;
  }
}

and the form submit function

function add_new_article_form_submit($form, &$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."';")->fetchField())
  //create the new account
  {     
    $edit = array(
        'name' => $email,
        'pass' => $password,
        'mail' => $email,
        'init' => $email,
        'roles' => array('4' => 'standard user'),
        'status' => 0,
        'access' => REQUEST_TIME,
    );
    $loc_var=user_save(drupal_anonymous_user(), $edit);
    $GLOBALS['new_user']=$loc_var->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, &$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

share|improve this question
    
I want this form to be available only for anonymus users/unlogged users. If the user is loged i want to change the form, also I want the original form for the administrator. –  Cristi Jul 23 at 13:10

3 Answers 3

up vote 2 down vote accepted

Try adding the validate function as you have added the submit function:

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

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

    return $form;
  }
}
share|improve this answer
    
EPIC!!!! Briliant!!! Thank you very much!!! –  Cristi Jul 23 at 12:58

Cristi,

I am not properly getting the point why you used the HOOK_form_alter to alter the custom form add_new_article_simple_page. However, I think the simple process of using the Form API is as follows,

  1. Create a form as my_custom_form().
  2. Create validation function as my_custom_form_validate($form, &$form_state).
  3. Create a submit function my_custom_form_submit($form, &$form_state).

In your validation function you should only check for the 3rd condition of yours and then set form_set_error('field_name', t("Password doesn't match"));

In your submit function you just apply the other business logic for #1 & #2 to create content or users.

share|improve this answer
    
I thought of that at the begining but i have to import the form for a certain content type and then add another field so i can also create users during submision. I didnt manage to import it in hook_form . I am in my first month of drupal so still learning –  Cristi Jul 23 at 13:07
    
If you are using a pre defined node create form then you should go for hook_form_alter or it's other variants. It's always a good practice to use the method I mentioned while creating custom form but where you are not advisable to change the other module or core forms then use alter hooks. There are many hooks for node creation and you might like to check those out. api.drupal.org is the place. –  Aneek Mukhopadhyay Jul 23 at 16:08

You could use hook_validate, or, if you want to validate some data before you actually click submit, you could use the Drupal form api's #ajax.

/**    
 * Implementation of hook_form().
 */
function poll_form(&$node, $form_state) {
  $form['choice_wrapper']['poll_more'] = array(
    '#type' => 'submit',
    '#value' => t('More choices'),
    '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
    '#weight' => 1,
    '#submit' => array('poll_more_choices_submit'), // If no javascript action.
    '#ajax' => array(
      'callback' => 'poll_choice_js',
      'wrapper' => 'poll-choices',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
}    

/**
 * Menu callback for AJAX additions. Render the new poll choices.
 */
function poll_choice_js($form, $form_state) {
  return $form['choice_wrapper']['choice'];
} 

Here is the documentation for #ajax.

share|improve this answer
    
I thought about using ajax , but doesn't it make it easier for hackers and spambots to find a users password? –  Cristi Jul 23 at 13:08
    
@Cristi - Potentially, but Drupal offers some pre-defined functions to help: api.drupal.org/api/drupal/includes!password.inc/function/… –  CR47 Jul 23 at 13:15
    
interesting, thank you very much for the suggestion :) –  Cristi Jul 23 at 13:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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