하나의 페이지를 만드는 맞춤 모듈을 만들었습니다. 이 페이지에서 새로운 내용을 만들기 위해 기존 양식을로드하고 암호 필드를 추가합니다. 로드 된 양식에는 이메일 필드가 있습니다. 양식을 제출하기 전에 이메일 필드에있는 사용자 이름과 비밀번호 필드에서 제공 한 비밀번호가있는 사용자가 있는지 확인해야합니다. 여기에 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;
}
바꾼다 기능은 암호 필드 삽입 : 제출
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.');
}
}
업데이트
제 잘못, 제 3의 시나리오를 테스트 할 때 어떤 일이 발생하는지 설명하는 것을 잊어 버렸습니다. 내용이 생성 페이지가 콘텐츠 페이지로 이동하고 오류 메시지가 표시되는 위치 즉
내가 D34dman 제안을 따라 항상 오류를 제공해야합니다 간단한 검증 기능을 썼다 2 UPDATE. 문제는 콘텐츠가 여전히 제출되고 저장된다는 것입니다. .. 여기에서도 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