custom field validation hook

Hi,

I created a custom field for the membership 2 registration form. Its an autocomplete text field which pulls up a list of current users to select who you want to work with.

I'm adding the field to the form using the register_form hook and recording the input using the ms_controller_frontend_register_user_complete hook. This part is working fine.

I want to validate the field by making sure the user they want to work with exists and the field isn't empty. I've tried using the hook - ms_controller_frontend_register_user_before but it doesn't seem to work.

This is the code I'm trying to get work -

function validate_autocomplete() {
        //input from autocomplete text field
	$user_id  = $_POST['user_id'];
	if ($user_id == ' '){
		//if no input return an error and don't let the form submit
	}
}
add_action('ms_controller_frontend_register_user_before', 'validate_autocomplete');

Thanks for your help.

  • Adam Czajczyk

    Hello David,

    I hope you're well today and thank you for your question!

    When you are saving custom fields values in a custom location, the validation should also be custom and the simplest way to introduce it is to include it in the code that actually saves the data.

    However, I think the hook you identified should work but let me please consult one of my colleagues about it. I've already asked him the question on this and hopefully he'll be able to lend me a hand on this. Please keep an eye on this thread and I'll update it as soon as I get a replay.

    Best regards,
    Adam

  • Ash

    Hello David

    Please try the following code:

    add_filter( 'ms_model_membership_create_new_user_validation_errors', function( $validation_errors, $member ) {
            // Validate here
            if( trim( $_POST['user_extra'] ) == '' )
            {
                    $validation_errors->add(
                            'user_extra',
                            'Extra field is mandatory!'
                    );
            }
    
            return $validation_errors;
    }, 99, 2 );

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.