如何在drupal中提交表单提交

时间:2014-07-22 14:19:45

标签: php validation drupal-7 user-registration form-api

我创建了一个自定义模块,我创建了一个页面。在该页面中,我加载现有表单以创建新内容,并添加密码字段。在我加载的表单中,我有一个电子邮件字段。我想在提交表单之前检查是否存在具有用户名的用户,该用户名是在电子邮件字段中找到的值以及密码字段提供的密码。这里我有3个场景:

  1. 如果用户不存在,我会使用电子邮件和密码创建一个帐户,然后创建内容
  2. 如果用户存在且密码正确,我创建内容
  3. 如果用户存在但密码不正确,我将停止表单提交
  4. 我的问题是我不知道如何停止表单提交(我参考方案编号3)。任何建议都是最基本的

    以下是页面功能的回调:

    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;
    }
    

    用于插入密码字段的alter函数:

    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;
      }
    }
    

    和表单提交功能

    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.');
      }
    }
    

    更新

    我的不好,我忘了解释当我测试第三种情况时会发生什么。内容已创建,页面将跳转到该内容页面,这是出现错误消息的位置

    更新2 我遵循了D34dman的建议并编写了一个简单的验证函数,该函数应该始终给出错误。问题是内容仍然被提交和保存。它似乎甚至没有调用hook_form_validate ..这是函数:

    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));
    }
    

    谢谢你, 克里斯提

2 个答案:

答案 0 :(得分:1)

阻止表单提交不是一个好习惯。而是使用hook_form_validate,如下所示。

/**
 * Implements hook_form_validate().
 */
function add_new_article_form_validate(&$form, &$form_state) {
  // Validate email address.
  $mail = $form_state['values']['personal']['email_address'];
  $password=$form_state['values']['account_password'];
  if (!valid_email_address($mail)) {
    form_set_error('personal][email_address', t('The email address appears to be invalid.'));
  }
  else if (user_load_by_mail($mail)) {
    if($uid = user_authenticate($email,$password)) {
      $account = user_load($uid);
      // Check if the user would have permission to create content after login!!!
      // not asked by OP, but just a suggestion.
      if (!user_access($string, $account)) {
        form_set_error('personal][email_address', t('You donot have sufficient priviledge to create the content.'));
      }
    }
    else {
      form_set_error('personal][email_address', t('Email or password incorrect.'));
    }
  }
}

注意:请不要尝试在验证功能中创建用户。在表单提交功能中执行此操作。由于可能有其他验证器可能会失败,您可能无法确定您的表单将被提交多少次以进行验证。

答案 1 :(得分:0)

终于找到了!我忘了添加

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

到我的hook_form_alter函数。感谢您对hook_form_validate的帮助和建议:)