Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to throw custom error message when a user registers?


  • potatopotahto
    Participant

    @potatopotahto

    I wan to throw a custom error message when a user enters an incorrect phone number that isn’t in the proper syntax, and thus failing the registration as a whole, how exactly do I define this as well as call it? I’d assume I have to do this in the bp-custom.php class, but what function do I have to call to do so, or how do I create an error message in general?

Viewing 7 replies - 1 through 7 (of 7 total)

  • Venutius
    Moderator

    @venutius

    Take a look at plugins/buddypress/bp-members/screens/register.php this is loaded when the register form is submitted. There’s a hook in there do_action( 'bp_signup_validate' ); which you could use to implement your own validation.

    In there you can set `$bp->signup->errors for the error you want to display if the validation fails for that field.


    potatopotahto
    Participant

    @potatopotahto

    Hello, while I’ve gotten the site to fail a registration with an incorrect number using what you suggested above, it doesn’t display any error message of sorts. My line is: $bp->signup->errors['invalid_number'] = __( 'Please enter a valid phone number', 'buddypress' ); This is in my bp-custom.php class, in the function that gets added to the hook bp_signup_validate, when an incorrect phone number is determined. The site properly fails a registration with an incorrect number, while allowing registration with a correct one, but it doesn’t display any error pop up at all, even though I included an error message of sorts with the latter half of the line. Is this a code issue, or could this potentially be a theme issue? I’m using the Spacious theme if this helps


    potatopotahto
    Participant

    @potatopotahto

    do I have to use errors['invalid_number'] anywhere at all within any of the scripts?


    Venutius
    Moderator

    @venutius

    As far as I can see that should be all you need to do, The register.php screen runs a function to display the error that you have input in line 149.


    potatopotahto
    Participant

    @potatopotahto

    Where is it supposed to display the message?
    So do you have any recommendations or advice?


    potatopotahto
    Participant

    @potatopotahto

    invalid_number is an error that I created in the bp-custom.php class, not a built in one, do I have to do anything to have it show up as a popup or to move it to display over the text field?


    potatopotahto
    Participant

    @potatopotahto

    After some experimentation, it seems the line add_action( 'bp_' . $fieldname . '_errors', function() use ( $error_message ) {echo apply_filters( 'bp_members_signup_error_message', "<div class=\"error\">" . $error_message . "</div>" );} ); gets called with both the phone number error, or any other default one, such as a mismatch between passwords.

    As such, this seems to look like the function in question. Is there any reason it would run and display a message for default errors, but do nothing visually for my custom one?

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar