Adding PHP Validation to Forms in Convert Forms
When building forms with Convert Forms, you may encounter scenarios where standard validation isn’t sufficient. For example, you might need to validate numeric input fields, ensure radio buttons are selected, or combine multiple validation rules. This article will guide you through implementing custom PHP validation in Convert Forms.
This guide assumes you have a basic understanding of PHP and focuses solely on the implementation.
Understanding the PHP Scripts Section
The PHP Scripts section in Convert Forms allows developers to execute custom PHP code during form submission. This is particularly useful for performing advanced validation, modifying field values, or integrating with external systems.
- The PHP code runs before the form data is saved to the database.
- You can access submitted form data using the $post array.
- Any modifications to the $post array will reflect in the final submission entry.
With this flexibility, you can implement complex validation logic tailored to your specific requirements.
How to Add Custom PHP Validation
To add custom PHP validation, follow these steps:
Access the PHP Scripts Section
- Open your form in Convert Forms.
- Navigate to Behavior → PHP Scripts → Form Process.
- This is where you’ll insert your custom PHP code.
Write Your Validation Logic
- Use the $post array to access submitted field values.
- Implement conditional checks to validate the data.
- If validation fails, throw an Exception with an appropriate error message.
Save and Test
- Save your changes and test the form thoroughly to ensure the validation works as expected.
Example: Validating Numeric Input and Radio Buttons
Below is an example of PHP validation that ensures:
- A numeric input field meets a minimum value requirement.
- Specific radio buttons are selected before submission.
Step 1: Define Validation Rules
Here’s how the validation logic works:
- Amount Validation: Ensures the numeric input field (e.g.,
amount
) is greater than or equal to a specified minimum value (e.g., 500.00). - Radio Button Validation: Ensures that required radio buttons (e.g.,
amountSelection
,paymentOption
,invoiceTax
) are selected.
<?php
$errors = [];
// ++ Amount Validation
$min_amount = 500.00;
$amount_error = "Amount must be 500.00 or greater.<br>";
if (!empty($post["amount"])) {
$amount = floatval($post["amount"]);
if ($amount < $min_amount) {
$errors[] = $amount_error;
}
}
// ++ Amount Selection Validation (Radio Button)
$amountSelection_error = "Select an amount.<br>";
if (empty($post["amountSelection"])) {
$errors[] = $amountSelection_error;
}
// ++ Payment Method Validation (Radio Button)
$payment_error = "Select a payment method.<br>";
if (empty($post["paymentOption"])) {
$errors[] = $payment_error;
}
// ++ Tax Invoice Validation (Radio Button)
$invoiceTax_error = "Do you require a Tax Invoice?<br>";
if (empty($post["invoiceTax"])) {
$errors[] = $invoiceTax_error;
}
// Handle the errors
if (!empty($errors)) {
$errorMessage = "<b>Fix these errors before submitting the form</b><br>";
$errorMessage .= implode("", $errors);
throw new Exception($errorMessage);
}
Step 2: Key Points to Note
- Radio Button Required Setting: Ensure the required setting for radio buttons is turned off in the form builder. This allows the PHP validation to handle the requirement instead.
- Error Messaging: The
$errors
array collects all validation errors, which are then displayed as a single message usingimplode()
. - Throwing Exceptions: If any validation fails, an Exception is thrown, preventing the form from being submitted.
How It Works
Numeric Field Validation
The script checks if theamount
field contains a value and ensures it meets the minimum threshold. If the value is below the threshold, an error is added to the$errors
array.Radio Button Validation
Each radio button group is validated to ensure a selection has been made. If a required radio button is missing, an error is added to the$errors
array.Combined Error Handling
All errors are combined into a single message and displayed to the user. This approach ensures clarity and improves the user experience by highlighting all issues at once.
Why Use This Approach?
Using PHP validation in Convert Forms provides several advantages:
- Flexibility: You can implement complex validation rules that go beyond the built-in options.
- Custom Error Messages: Tailor error messages to match your brand’s tone and provide clear instructions to users.
- Centralised Logic: Keep all validation logic in one place, making it easier to maintain and update.
Final Notes
By leveraging the PHP Scripts section, you can create robust validation rules that ensure your form submissions meet your exact requirements. The provided example demonstrates how to validate numeric inputs and radio buttons while combining multiple checks into a single error message.