gravityflow_validation_user_input

Description

The filter gravityflow_validation_user_input is used to validate whether the user input step form is valid. You might use it when you require an equivalent to gform_validation. Note that gform_field_validation does fire for user input steps, and is generally the better filter to hook your custom logic to.

Parameters

ParameterTypeDetails
$validation_resultArray The validation result and form currently being processed
$stepStepThe current user input step
$new_statusStringThe new status for the current step

Examples

Prevent users with specific meta data from completing a user input

add_filter( 'gravityflow_validation_user_input', 'sh_custom_user_input_validation', 10,3 );

function sh_custom_user_input_validation( $validation_result, $step, $new_status ) {

    $current_user = wp_get_current_user();
    if ( 0 !== $current_user->ID ) {
        $level = get_user_meta( $current_user->ID, 'employee_level', true );
        if ( isset( $level ) && (int) $level < 5 ) {
            return new WP_Error( 'validation_result', esc_html__( 'Only Level 5+ employees can complete this step', 'gravityflow' ), $validation_result );
        }
    }

    return $validation_result;
}

Prevent all validation from failing on a particular user input step

add_filter( 'gravityflow_validation_user_input', 'sh_custom_user_input_validation', 10,3 );

function sh_custom_user_input_validation( $validation_result, $step, $new_status ) {
    if ( $step->get_id() == '223' ) {
        $validation_result['is_valid'] = false;
    }
    return $validation_result;
}

Validate for a field to be required only on a specific step

Setting a field to be required via the form editor will validate that it is not blank on every step. This example shows how you could leave the required field setting off and still validate that it is required on a specific step. It could easily be customized in the opposite way to allow a field set as required on all steps be excluded on a single step as well.

add_filter( 'gravityflow_validation_user_input', 'jo_custom_user_input_validation', 10, 3 );
function jo_custom_user_input_validation( $validation_result, $step, $new_status ) {
	if ( $step->get_id() == '229' ) {

		if ( empty( rgpost( 'input_1' ) ) ) {
			// set the form validation to false
			$validation_result['is_valid'] = false;

			//finding Field with ID of 1 and marking it as failed validation
			$form = $validation_result['form'];
			foreach( $form['fields'] as &$field ) {

				//NOTE: replace 1 with the field you would like to validate
				if ( $field->id == '1' ) {
					$field->failed_validation = true;
					$field->validation_message = 'Fill in the required field for this step';
					break;
				}
			}

			return new WP_Error( <br>				'validation_result', 
				esc_html__( 'Required field(s) for this step are incomplete.', 'gravityflow' ), 
				$validation_result
			);
		}
	}
	return $validation_result;
}

Prevent the entry creator from completing a user input step

add_filter( 'gravityflow_validation_user_input', 'validate_entry_creator_conflict', 10, 2 );
function validate_entry_creator_conflict( $validation_result, $step ) {

	// Only check for a specific step ID
	if ( $step->get_id() != 443 ) {
		return $validation_result;
	}
	//Only apply for logged in  user
	$current_user = wp_get_current_user();
	if ( 0 == $current_user->ID ) {
		return $validation_result;
	}

	$entry = $step->get_entry();
	$created_by_user_id = $entry['created_by'];
	$error = new WP_Error( 'validation_result', esc_html__( 'You cannot action a step for an entry that you submit.', 'gravityflow' ), $validation_result );
	// if current user is the entry creator
	if ( $created_by_user_id == $current_user->ID ) {
		$assignees = $step->get_assignees();

		$matched = false;
		// loop through all assignees
		foreach ( $assignees as $assignee ) {
			switch ( $assignee->get_type() ) {
				case 'user_id':
					// remove when user id match
					return $error;
					break;
				case 'role':
					// remove when user role match
					$assignee_role = $assignee->get_id();
					if ( in_array( $assignee_role, $current_user->roles ) ) {
						return $error;
					}
					break;
				case 'email':
					// remove when user email match
					$assignee_email = $assignee->get_id();
					if ( $assignee_email == $current_user->user_email ) {
						return $error;
					}
					break;
			}
		}
	}
	return $validation_result;
}

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?