• Resolved neoset

    (@neoset)


    Hello, if I wanted to validate two fields nickname and user_login and get an error if their values are the same from this example, how would I have to do it?
    https://docs.ultimatemember.com/article/94-apply-custom-validation-to-a-field
    I’ve tried several ways but can’t get it to work.
    Please, if you can put the example with the two versions to understand it better with the first and the second um_custom_field_validation_{$custom} if you can.

    Thank you very much for the help.

    • This topic was modified 2 years, 2 months ago by neoset.
Viewing 8 replies - 1 through 8 (of 8 total)
  • @neoset

    You can try this code snippet:

    add_action('um_submit_form_errors_hook_', 'um_custom_validate_username_nickname', 999, 1);
    
    function um_custom_validate_username_nickname( $args ) {
    	
    	if ( isset( $args['user_login'] ) && isset( $args['nickname'] ) && $args['user_login'] == $args['nickname'] ) {
    		UM()->form()->add_error( 'user_login', 'Your username and nickname can not be equal.' );
    	}
    }

    @neoset

    The second example with the Custom Validation set to username_nickname for the Nickname UM Forms Designer field:

    function um_custom_validate_username_nickname( $key, $array, $args ) {
    
    	if ( isset( $args[$key] ) && isset( $args['user_login'] ) && $args[$key] == $args['user_login'] ) {
    		UM()->form()->add_error( $key, __( 'Your username and nickname can not be equal.', 'ultimate-member' ) );
    	}
    }
    add_action( 'um_custom_field_validation_username_nickname', 'um_custom_validate_username_nickname', 30, 3 );
    Thread Starter neoset

    (@neoset)

    These codes validate very well in the registration form but in the profile form they do not work, would you know what could be the reason?
    When you edit in the profile form you can put the same value in both user_login and nickname and it doesn’t show the error.
    I think the error doesn’t show it because the user_login field isn’t shown in edit mode and you can’t compare it.
    It would be necessary to obtain that data from outside the form fields or in another way.
    Thank you very much for the help.

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

    @neoset

    Yes you are right with your comment about user_login.
    Another issue might be capitals and lowercase characters for bypassing the validation.

    This code snippet replaces example 2 and will also validate profile form updates and make the validation in lowercase characters.

    function um_custom_validate_username_nickname( $key, $array, $args ) {
    
        if ( isset( $args[$key] )) {
            if( isset( $args['user_login'] )) $user_login = $args['user_login'];
            else $user_login = um_user( 'user_login' );
            if( strtolower( $args[$key] ) == strtolower( $user_login )) {
                UM()->form()->add_error( $key, __( 'Your username and nickname can not be equal.', 'ultimate-member' ) );
            }
        }
    }
    add_action( 'um_custom_field_validation_username_nickname', 'um_custom_validate_username_nickname', 30, 3 );
    • This reply was modified 2 years, 2 months ago by missveronica.
    Thread Starter neoset

    (@neoset)

    In the end that code worked for me in the registry but not in the profile but well, I managed to solve it thanks to your examples, the function I was looking for from the beginning was um_user( 'user_login' ) since I had tried with $userum->user_login but value did not enter.
    In the end, the code looks like this:

    add_action('um_submit_form_errors_hook_', 'um_custom_validate_username_nickname', 999, 1);
    function um_custom_validate_username_nickname( $args ) {
    	$user_login = um_user( 'user_login' );
    	if ( isset( $user_login ) && isset( $args['nickname'] ) && $user_login == $args['nickname'] || isset( $args['user_login'] ) && isset( $args['nickname'] ) && $args['user_login'] == $args['nickname'] ) {
    		UM()->form()->add_error( 'nickname', 'Your username and nickname can not be equal.' );
    	}
    }

    This solution works in both places at the same time, both in the registry and in the profile, I know that the code can be reduced even more, but this way I make sure that strange things do not happen with the logical operators.

    Thank you very much for all your help, the resolution of the problem and the speed of response are impressive, thanks for the time you have dedicated.
    A huge greeting.

    @neoset

    In the end that code worked for me in the registry but not in the profile

    Did you add the Custom Validation also in the Profile Form?

    Thread Starter neoset

    (@neoset)

    If I had it in both.

    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Thanks for letting us know how it resolves the issue.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Validate two fields nickname and user_login’ is closed to new replies.