我已經創建了一個自定義模塊,用於創建一個頁面。在該頁面中,我加載用於創建新內容的現有表單並添加密碼字段。在我的加載形式中,我有一個電子郵件字段。我希望在提交表單之前檢查是否存在一個用戶,該用戶的用戶名是在電子郵件字段中找到的值以及密碼字段提供的密碼。在這裏,我有3個場景:如何在drupal中插入表單提交
- 如果用戶不存在,我拿的電子郵件和密碼,並創建一個帳戶,然後創建內容
- 該用戶是否存在,密碼是否正確,我創建的內容
- 如果用戶存在,但密碼不正確,我停止表單提交
我的問題是我不知道如何停止的形式提交(我指的情景3號)。任何建議都最aprerciated
這裏有翻頁功能回調:
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.');
}
}
UPDATE
我不好,我忘了解釋當我測試第三種情況時會發生什麼。在創建內容,頁面跳轉到該頁面內容,這是出現的錯誤消息,其中
更新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));
}
謝謝 克里斯提
從技術上講,您無法「停止」表單提交。在PHP實際執行代碼的時候,表單已經提交給服務器並完全處理成$ _POST/$ _ GET/etc ... –
Ooooook,所以我應該嘗試在hook_form_validate中設置它? – Cristi