BuddyDev

Search

Validating xprofile field

  • Participant
    Level: Initiated
    Posts: 1
    Olandir on #43032

    I can’t seem to find this anywhere and I’m not sure why as I would think this would be something people would want to do.

    I want to validate the “Name” field against a list of restricted names. Like I don’t want the word “admin” or “moderator” or other words to be allowed in the name field.

    I keep looking for the function to validate and how to just create a simple list to validate but I can’t find it anywhere.

  • Keymaster
    (BuddyDev Team)
    Posts: 22213
    Brajesh Singh on #43089

    Hi Olandir,
    Thank you for the question.
    Here is the code to do that

    
    add_action( 'bp_signup_validate', function () {
    
    	$disallowde_names = array( "admin", "me" );
    
    	if ( ! isset( $_POST['field_1'] ) ) {
    		return;
    	}
    
    	if ( in_array( $_POST['field_1'], $disallowde_names ) ) {
    		buddypress()->signup->errors['field_1'] = __( 'Sorry, this name is not allowed!' );
    	}
    
    } );
    

    Please update the disallowed list with the names.

    Please do understand that it does not prevent the users form changing the name after registration to one of the disallowed ones.

    PS:- My colleague @ravisharma proposed another alternative code which works for update as well as registration(Here is his code)

    
    add_filter( 'bp_xprofile_set_field_data_pre_save', function ( $save, $field_args ) {
        $field = $field_args['field'];
        $value = $field_args['value'];
    
        // Not name field.
        if ( $field->id != 1 ) {
            return $save;
        }
    
        $restricted_names = array( 'admin', 'moderator' );
    
        if ( in_array( $value, $restricted_names ) ) {
            $error_message = __( 'Name is restricted. Please try different name' );
    
            add_action(
                'bp_field_' . $field->id . '_errors',
                function () use ( $error_message ) {
                    echo esc_html( $error_message );
                }
            );
    
            return new WP_Error( $error_message );
        }
    
        return $save;
    },10, 3 );
    
    

    The problem is it does not show the error to the user.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 1
    Olandir on #43090

    Thank you so much

    Just out of curiosity, with the second code, where does the error show up if not to the use (as I see there is an error message).

    Also I assume I can’t use both, either one or the other.

  • Keymaster
    (BuddyDev Team)
    Posts: 22213
    Brajesh Singh on #43092

    You are welcome.
    You can use both of them if you want to.

    In case you used the 2nd code, the message on update profile page will be “There was a problem updating some of your profile information. Please try again.” That’s a bit misleading.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved