Custom Fields
Re-Envisioned
Support MB User Profile Backend validation for registration form

  • Creator
    Topic
  • #11040
    Harold
    Participant

    Hi,

    I need to do some backend validation on the custom fields of the registration form. For example, i'd like to check if the invitation code during registration is correct, and if not prevent registration of a new user and echo "Incorrect invitation code" on the registration form. I tried different things with the "rwmb_profile_validate" hook, but it didn't work.

    https://ghostbin.com/paste/5oh7p

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #11049
    Harold
    Participant

    This is the code so far. How do I echo a warning if the invitation code from the registration form is incorrect?

    
    // Validation for metabox registration form
    add_filter( 'rwmb_profile_validate', function( $is_valid, $config ) {
        if ( 'invitation_verification' === $config['id'] ) {
            if ( '123' !== $_POST['invitation_code'] ) {
                // do NOT register new user and echo "Incorrect invitation code" on the registration form
            }
        }
        return $is_valid;
    }, 10, 2 );
    
    #29428
    Neesam
    Participant

    I have the same problem.

    #29435
    Long Nguyen
    Moderator

    Hi @fdk3r6bgmail-com,

    Can you please share the code that validates your form? The variable $is_valid should be assigned to false if the condition does not match.

    add_filter( 'rwmb_profile_validate', function( $is_valid, $config ) {
        if ( 'invitation_verification' === $config['id'] ) {
            if ( '123' !== $_POST['invitation_code'] ) {
                $is_valid = false;
            }
        }
        return $is_valid;
    }, 10, 2 );
    #29437
    Neesam
    Participant

    The method I use is wp_die('error text');
    rwmb_profile_validate can validate
    But it cannot return custom error messages
    I wish there was a perfect solution too
    Because I don't want to use ajax to validate unique data

    #29439
    Long Nguyen
    Moderator

    Hi,

    This filter only helps you to validate the form based on the condition, does not support showing the custom error message. You can use the plugin Loco Translate to translate the default message Invalid form submit.

    #29447
    Neesam
    Participant

    It's just a shame that i can't customize the error message
    I hope to see this amendment later.

    #29453
    Neesam
    Participant

    It's a bad way to go, but it works for me.

    open \meta-box-aio\vendor\meta-box\mb-user-profile\src\Forms\Base.php

    
    if (!$is_valid) {
                $this->error->set(__('Invalid form submit.', 'mb-user-profile'));
                return null;
    }
    

    modify to

    
    if (!$is_valid[0]){
                $this->error->set($is_valid[1]);
                return null;
    }
    
    
    
    add_filter( 'rwmb_profile_validate', function( $is_valid, $config ) {
        if ( 'invitation_verification' === $config['id'] ) {
            if ( '123' !== $_POST['invitation_code'] ) {
                $is_valid = [false,'Error Text'];
            }
        }
        return $is_valid;
    }, 10, 2 );
    
    #29457
    Long Nguyen
    Moderator

    Hi,

    Thanks for the feedback.

    I'm going to inform the developer team to consider adding custom error text to the to-do list for the future development of the plugin.

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