Backend validation for registration form

Support MB User Profile Backend validation for registration formResolved

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #11040
    HaroldHarold
    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

    #11049
    HaroldHarold
    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
    eckhardaeckharda
    Participant

    I have the same problem.

    #29435
    Long NguyenLong 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
    eckhardaeckharda
    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 NguyenLong 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
    eckhardaeckharda
    Participant

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

    #29453
    eckhardaeckharda
    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 NguyenLong 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.

    #35388
    Nicholas CoxNicholas Cox
    Participant

    Hi, did this feature ever get implemented at all?

    #35392
    Long NguyenLong Nguyen
    Moderator

    Hi Nicholas,

    Yes, it is implemented. Works like the frontend form validate https://docs.metabox.io/extensions/mb-frontend-submission/#validation

    add_filter( 'rwmb_profile_validate', function( $validate, $config ) {
        if ( 'your-meta-box-id' !== $config['id'] ) {
            return $validate;
        }
        if ( empty( $_POST['image_upload_field'] ) ) {
            $validate = 'Please select at least one image to upload'; // Return a custom error message
        }
        return $validate;
    }, 10, 2 );
    
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.