gravityflow_validation_user_input

The 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

$validation_result array
The validation result and form currently being processed

$step object
The current user input step

$new_status string
The new status for the current step

Examples

Example 1 - 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;
}

Example 2 - 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;
}

Example 3 - 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 sh_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;
}

Placement

This code should be placed in the functions.php file of your active theme.