2

I have my own validation on user form login like this :

if ($form_id == 'user_login') {
        $form['#validate'][] = 'wemusic_user_login_validate';
        $form['#submit'] = array();
        array_unshift($form['#submit'], 'wemusic_user_login_submit');
    }



function wemusic_user_login_validate($form, &$form_state){
    $username = $form['name']['#value'];
    $user = user_load_by_name($username);
    if ($user){
        if($user->status == "1"){
            $form_state = array();
            $form_state['uid'] = $user->uid;
            user_login_submit(array(), $form_state);
        }else{

            drupal_goto('registration/confirm', array('query'=>array(
                'user'=>$username,
            )));
        }

    }else{
        drupal_goto('user/register');
    }
} 

with this code CAPTCHA failed , how can I call CAPTCHA validation in my custom function ?

  • Hello Bro, nice to see you here again,can you tell me exactly what do you want to do? USING DRUPAL_GOTO on validation is A BIG MISTAKE, it seems you just want if user is valid and active ( status =1 ) login it, else user is valid but not Active ( status !=1 ) redirect it to registration/confirm with 'user'=>$username, and if not valid user redirect it to registration form. TELL ME EXACTLY WHAT DO YOU WANT TO DO TILL I HELP YOU DADASH ;) – Yusef Dec 22 '16 at 22:16
  • hello Bro, thanks ;) so what's you'r suggest instead of drupal_goto.when i use my custom validation user can login without captcha, this is my problem @zhilevan – user23811 Dec 22 '16 at 23:08
  • DO NOT do any action except validation on validator functions use validation in order to inform user about a mistake or unvalid data on submitted forms, and if is the validation failed user stay on that form with some error to complete/correct data and resubmit that. but in your case you redirect user to another page,Tell me what you want happened when user is valid but not active ? you just want do this mechanism on user_login form ? – Yusef Dec 23 '16 at 6:14
  • got it thanks, i want to check if users is valid but users does not have a confirm code redirect them to registration confirm code. too in safhe vase user sms mifrestim – user23811 Dec 23 '16 at 6:45
0

This a improved version of your code

  if ($form_id == 'user_login') {
        $form['#validate'][] = 'wemusic_user_login_validate';
        $form['#submit'] = array();
        array_unshift($form['#submit'], 'wemusic_user_login_submit');
    }

function wemusic_user_login_validate($form, &$form_state){
    $username = $form['name']['#value'];
    $user = user_load_by_name($username);
    if ($user && $user->status != "1"){
            drupal_set_message("Baba Taid kon bad boro to",'error');
            drupal_goto('registration/confirm', array('query'=>array(
                'user'=>$username,
            )));
    }
   else {
        drupal_set_message("Sabenam nakarde mikhay beri to akhe amo ?!!!",'error');
        drupal_goto('user/register');
    }
} 

When you are in user_login and user is going to login don't empty $form_state with $form_state = array(); Let Drupal Handle it's way to log on user you just check what you want before it.

This will work for you , but I think is better when the situation is not satisfied in validator set error and set a destination variable on $form_state ( maybe $form_state['redirect'] or your custom $form_state['my_redirect'] ) then in form_alter check if your error exists redirect to that destination with Drupal_goto.

Any way mentioned code ( up code) satisfy what you want.

  • thnanks for your answer, it helped me a lot . – user23811 Dec 23 '16 at 14:04
  • 1
    @Soheil you're welcome Dear Soheil – Yusef Dec 23 '16 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy