Sometimes you might need to submit a form without enforcing all the validation rules, perhaps for testing purposes or under specific conditions. In this tutorial, we’ll walk through a simple way to bypass Fluent Form validations for specific forms. By following these steps, you’ll learn how to set it up and customise it to fit your needs.
What We’re Doing
We’ll write some code that:
- Checks if a specific form ID is targeted.
- If the form ID is in the target list, it will remove all validation rules from our form fields.
1. Setting Up the Bypass
First, we need to create a function that hooks into Fluent Form’s rendering process. This function will check if the form ID is in our target list and decide whether to bypass the validations.
/** * Bypass validation rules if URL contains validation=false. * * @param object $form The form object to process. * @return object The modified form object. * * @author Taylor Drayson * @link https://snippetclub.com */ function tct_bypass_validation_rules($form) { // Array of target form IDs where validation rules can be bypassed $target_forms = [1, 2]; if (!in_array($form->id, $target_forms)) { return $form; } // Remove validation rules from form fields $form->fields['fields'] = remove_validation_rules($form->fields['fields']); return $form; } add_filter('fluentform/rendering_form', 'tct_bypass_validation_rules', 10, 1);
Copy
What This Code Does:
- Target Forms: We specify which forms should be affected by setting their IDs in the
$target_forms
array. - Remove Validation Rules: If the form ID is in the target list, we call a function to remove the validation rules from the form fields.
2. Handling Form Submission
Next, we need to handle the form submission to ensure the validations are bypassed correctly.
/** * Bypass validation rules during form submission based on the form ID. * * @param array $originalValidations The original validation rules. * @param object $form The form object. * @param array $formData The submitted form data. * @return array The modified validation rules. * * @author Taylor Drayson * @link https://snippetclub.com */ function tct_bypass_validation_rules_submit($originalValidations, $form, $formData){ $target_forms = [1, 2]; // Add your target form IDs here // If the form ID is not in the target forms array, return the original validations if (!in_array($form->id, $target_forms)) { return $originalValidations; } // Return an array with dummy validation rules to bypass actual validation return array( array('text' => 'noop'), array('text.required' => '') ); } add_filter('fluentform/validations', 'tct_bypass_validation_rules_submit', 10, 3);
Copy
What This Code Does:
- Target Forms: Again, we specify which forms should be affected.
- Dummy Validation Rules: If the form ID matches, we return a set of dummy validation rules to bypass actual validation.
3. Removing Validation Rules
Finally, we need a helper function to remove validation rules from the fields recursively.
/** * Recursively remove validation rules from form fields. * * @param array $fields The fields array to process. * @return array The modified fields array without validation rules. * * @author Taylor Drayson * @link https://snippetclub.com */ function remove_validation_rules($fields) { foreach ($fields as &$field) { // Remove validation rules if they exist in the field settings if (isset($field['settings']['validation_rules'])) { unset($field['settings']['validation_rules']); } // Check for nested fields and call the function recursively if (isset($field['fields']) && is_array($field['fields'])) { $field['fields'] = remove_validation_rules($field['fields']); } // Check for columns with nested fields and call the function recursively if (isset($field['columns']) && is_array($field['columns'])) { foreach ($field['columns'] as &$column) { if (isset($column['fields']) && is_array($column['fields'])) { $column['fields'] = remove_validation_rules($column['fields']); } } } } return $fields; }
Copy
What This Code Does:
- Remove Validation: This function goes through each field and removes its validation rules.
- Recursive Calls: It handles nested fields and columns by calling itself recursively.
How You Can Adapt This
Want to customise which forms get this bypass? It’s super easy! Just change the $target_forms
array to include the IDs of the forms you want to target.
$target_forms = [1, 2, 3]; // Add the IDs of the forms you want to target
Copy
Conclusion
And there you have it! A straightforward way to bypass FluentForm validations. This method is flexible, allowing you to target specific forms. I hope this tutorial has been clear and helpful. Happy coding!