Support Forum
Support › MB User Profile › Backend validation for registration formResolved
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.
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 );
I have the same problem.
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 );
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
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.
It's just a shame that i can't customize the error message
I hope to see this amendment later.
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 );
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.
Hi, did this feature ever get implemented at all?
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 );