Backend validation for registration form
Support › MB User Profile › Backend validation for registration formResolved
- This topic has 10 replies, 4 voices, and was last updated 3 years, 1 month ago by
Long Nguyen.
-
AuthorPosts
-
August 17, 2018 at 8:32 PM #11040
Harold
ParticipantHi,
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.
August 18, 2018 at 3:34 AM #11049Harold
ParticipantThis 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 );
July 11, 2021 at 11:23 PM #29428eckharda
ParticipantI have the same problem.
July 12, 2021 at 10:50 AM #29435Long Nguyen
ModeratorCan you please share the code that validates your form? The variable
$is_valid
should be assigned tofalse
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 );
July 12, 2021 at 12:16 PM #29437eckharda
ParticipantThe 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 dataJuly 12, 2021 at 3:36 PM #29439Long Nguyen
ModeratorHi,
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.
July 12, 2021 at 4:46 PM #29447eckharda
ParticipantIt's just a shame that i can't customize the error message
I hope to see this amendment later.July 12, 2021 at 6:50 PM #29453eckharda
ParticipantIt'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 );
July 12, 2021 at 9:36 PM #29457Long Nguyen
ModeratorHi,
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.
March 31, 2022 at 10:49 PM #35388Nicholas Cox
ParticipantHi, did this feature ever get implemented at all?
April 1, 2022 at 10:49 AM #35392Long Nguyen
ModeratorHi 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 );
-
AuthorPosts
- You must be logged in to reply to this topic.