gravityflow_validation_approval

Allow the validation result to be overridden using the gravityflow_validation_approval filter.

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 a specific approval step of form from being rejected without customizing the workflow note.

add_filter( 'gravityflow_validation_approval', 'validate_agreement_default_note', 10, 2 );
function validate_agreement_default_note( $validation_result, $step ) {
	if ( $validation_result['form']['id'] == '1' &&  $step->_step_type == 'approval' ) :
		$current_step_ID = rgpost( 'step_id' );
		if( $current_step_ID == 14 ):
			$note  = rgpost( 'gravityflow_note' );
			$new_status = rgpost( 'gravityflow_approval_new_status_step_' . $current_step_ID );

			if( $new_status == 'rejected' && $note == "I acknowledge and accept the terms and conditions listed above"):
				$validation_result['is_valid'] = false;
				$validation_result['form']['failed_validation'] = true;
				$validation_result['form']['workflow_note']     = array(
					'failed_validation'  => true,
					'validation_message' => esc_html__( 'Please change the confirmation above to rationale for rejection' )
				);
			endif;
		endif;
	endif;
	return $validation_result;
}

Example 2 - Prevent the entry creator from completing an approval step

add_filter( 'gravityflow_validation_approval', '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 should be placed in the functions.php file of your active theme.