gform_user_registration_validation

Description

This filter is used to validate submissions that have a User Registration feed attached to the submitted form. It passes several useful variables that make validating submissions dependent on a User Registration feed easier.

This filter is particularly useful if you are adding custom settings/meta to the User Registration feed using the gform_user_registration_add_option_section or gform_user_registration_add_option_group hooks and need to validate the submitted entry based on these settings/meta.

Usage

Applies to all forms.

add_filter( 'gform_user_registration_validation', 'your_function_name', 10, 3 );

Parameters

  • $form Form Object
    The form currently being processed.
  • $feed Feed Object
    The feed which is currently being processed.
  • $submitted integer
    The current page number (used to validate only the current page on multi-page forms).

Examples

Validate Multisite Submission

This example uses the gform_user_registration_validation filter to validate the User Registration Add-on to add support for multisite.

This example also uses a handy function available in the User Registration add-on which allows you to easily add validation errors to fields by ID: gf_user_registration()->add_validation_error().

add_filter( 'gform_user_registration_validation', 'validate_multisite_submission', 10, 3 );
function validate_multisite_submission( $form, $feed, $submitted_page ) {
    global $path;

    $meta = rgar( $feed, 'meta' );

    // make sure multisite create site option is set
    if ( rgempty( 'createSite', $meta ) ) {
        return $form;
    }

    // $_POST to Entry
    $entry = GFFormsModel::get_current_lead();

    $site_address_field = GFFormsModel::get_field( $form, $meta['siteAddress'] );
    $site_address       = gf_user_registration()->get_meta_value( 'siteAddress', $meta, $form, $entry );

    $site_title_field = GFFormsModel::get_field( $form, $meta['siteTitle'] );
    $site_title       = gf_user_registration()->get_meta_value( 'siteTitle', $meta, $form, $entry );

    // get validation result for multisite fields
    $validation_result = wpmu_validate_blog_signup( $site_address, $site_title, wp_get_current_user() );
    $error_msg         = false;

    // site address validation, only if on correct page
    if ( $site_address_field->pageNumber == $submitted_page ) {

        $error_msg = ( isset( $validation_result['errors']->errors['blogname'][0] ) ) ? $validation_result['errors']->errors['blogname'][0] : false;

        if ( $error_msg != false ) {
            $form = gf_user_registration()->add_validation_error( $meta['siteAddress'], $form, $error_msg );
        }
    }

    // site title validation, only if on correct page
    if ( $site_title_field->pageNumber == $submitted_page ) {

        $error_msg = ( isset( $validation_result['errors']->errors['blog_title'][0] ) ) ? $validation_result['errors']->errors['blog_title'][0] : false;

        if ( $error_msg != false ) {
            $form = gf_user_registration()->add_validation_error( $meta['siteTitle'], $form, $error_msg );
        }
    }

    return $form;
}

Ignore ‘This email address is already registered’ error returned by WordPress

If you want to allow users to register on your site using already registered emails, you will need to use the function below along with the gform_user_registration_check_email_pre_signup_activation filter.

add_filter("gform_user_registration_validation", "ignore_already_registered_error", 10, 3);
function ignore_already_registered_error($form, $config, $pagenum){

	// Make sure we only run this code on the specified form ID
	if($form['id'] != 1) {
		return $form;
	}

	// Get the ID of the email field from the User Registration config
	$email_id = $config['meta']['email'];

	// Loop through the current form fields
	foreach($form['fields'] as &$field) {

	// confirm that we are on the current field ID and that it has failed validation because the email already exists
	if($field->id == $email_id && $field->validation_message == 'This email address is already registered')
		$field->failed_validation = false;
	}

	return $form;

}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

$form = apply_filters( 'gform_user_registration_validation', $form, $feed, $submitted_page );

This filter is located in GF_User_Registration::validate() in class-gf-user-registration.php.