• Resolved katso

    (@katso)


    Hello,
    I’m trying to add a custom validation to a text field to both Login and Registration forms, but I can’t get it to fire 🙁

    I’m using a child theme, and my code is in the functions.php in that theme, here’s the function:

    
    function um_custom_validate_mobile_number( $key, $array, $args ) {
      //die("I waz here"); - this never fires either!
      UM()->form()->add_error( $key, __( 'Secret word not valid', 'ultimate-member' ) );
    }
    add_action( 'um_custom_field_validation_secret_word_login', 'um_custom_validate_mobile_number', 30, 3 ); 
    

    In the form itself, the meta key, and custom action are both: secret_word_login
    and the Validate is set to Custom Validation. The new field is also set to Required, but I can’t get the form to fail to log in – whether I put the value in the Secret Word field or not…

    Am I missing something? Many thanks for your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • @katso

    You can use this code snippet for your secret word field
    both for login and registrations.

    I have used the same field for login and registration
    but you can have different fields and different secret words.

    function um_custom_validate_secret_word_registration( $key, $array, $args ) {
    	
        if ( isset( $args[$key] ) && $args[$key] != '1234' ) {
    		UM()->form()->add_error( $key, __( 'Please enter the secret word.', 'ultimate-member' ) );
    	}
    }
    add_action( 'um_custom_field_validation_secret_word_login', 'um_custom_validate_secret_word_registration', 30, 3 );
    
    function um_user_login_secret_word( $args ) {
    	
        if ( isset( $args['submitted']['secret_word_login'] ) && $args['submitted']['secret_word_login'] != '1234' ) {
    		um_reset_user();
    		exit( wp_redirect( add_query_arg( 'err', 'bad_secret_word', UM()->permalinks()->get_current_url() ) ) );
    	}
    }
    add_action( 'um_submit_form_errors_hook_logincheck', 'um_user_login_secret_word', 30, 1 );
    
    function um_login_error_secret_word( $err, $my_error ) {
    
        if( $my_error == 'bad_secret_word' ) {
            return 'Please enter the secret word.';
        }
        return $err;
    }
    add_filter( 'um_custom_error_message_handler', 'um_login_error_secret_word', 10, 2 );
    
    Thread Starter katso

    (@katso)

    @missveronicatv Thank you very much for the reply!

    With some small adjustments I got both registration and login working, mostly:

    
    function um_custom_validate_secret_word_registration( $key, $array, $args ) {
    	
        if ( isset( $args[$key] ) && $args[$key] != '1234' ) {
    		UM()->form()->add_error( $key, __( 'Please enter the current secret word, found on the IAA dashboard.', 'ultimate-member' ) );
    	}
    }
    add_action( 'um_custom_field_validation_secret_word_registration', 'um_custom_validate_secret_word_registration', 30, 3 );
    
    function um_custom_validate_secret_word_login( $key, $array, $args ) {
    	
        if ( isset( $args[$key] ) && $args[$key] != '1234' ) {
    		UM()->form()->add_error( $key, __( 'Please enter the current secret word, found on the IAA dashboard.', 'ultimate-member' ) );
    	}
    }
    add_action( 'um_custom_field_validation_secret_word_login', 'um_custom_validate_secret_word_login', 30, 3 );

    `

    However when logging in with the wrong code, I see the error code in the url, but there’s no error next to the Secret Word input, and at the top of the inputs it says “An error has been encountered”. Should something be done with um_login_error_secret_word?

    Thank you very much for your help!!

    • This reply was modified 2 years, 6 months ago by katso.
    • This reply was modified 2 years, 6 months ago by katso.
    • This reply was modified 2 years, 6 months ago by katso.

    @katso

    You got three functions from me the first for doing a custom validation during registration.

    The second for secret word validation in login and is not a custom validation function like first function. I’m using another hook for this validation.

    The third function will deliver the error message on top of the login form by reading the err code given by the second function in the URL.

    Replace your second function with my second and third function.
    The only change you should do to my code is the message in the return statement.

    • This reply was modified 2 years, 6 months ago by missveronica.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @katso,

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. 🙂

    Regards,

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom validation not firing :(’ is closed to new replies.