Displaying Validation Errors
When validation errors are produced by Trongate's Validation class, it is standard practice to re-present forms to users. This approach allows users to correct their input without losing the information they have already entered.
The typical technique for re-presenting forms to end users is to invoke the method responsible for displaying the previous page (i.e., the page that renders the HTML form). For example:
$result = $this->validation->run();
if ($result === true) {
echo 'Form submission successful!'; // This will be improved later!
} else {
// Form submission error (re-present the form to the user)
$this->create();
}
By immediately re-invoking the create()
method, as shown above, the entire page that renders the HTML form will be rendered again.
When the form is re-presented to the user, an array of form validation errors will be made available.
There are two techniques that can be used to display validation errors:
- Traditional
- Inline
1. Traditional Validation Errors Display
This approach involves displaying all validation errors at once, typically at the top of the form.
How to Implement:
- Use the run() method to execute form validation tests.
- If the validation tests produce a
$result
that is not equal to true, re-invoke the method responsible for rendering the form page. For example: - In your view file, use the validation_errors() function to display all validation errors. The code below demonstrates how to render validation errors using PHP short tags:
$result = $this->validation->run();
if ($result === true) {
echo 'Form submission successful!'; // This will be improved later
} else {
// Form submission error (re-present the form to the user)
$this->create();
}
<?= validation_errors() ?>
The validation_errors() method will display all of the form validation errors together. The line of code responsible for rendering the validation errors is usually positioned inside a view file and above the corresponding form.
By default, the validation_errors() function renders error messages as red text within <p>
tags. However, Trongate offers flexibility in customizing the appearance of these error messages:
- Custom HTML wrapping: You can pass optional arguments to validation_errors() to control the HTML wrapping of error messages. For example:
<?= validation_errors('<div class="custom-error">', '</div>') ?>
- Global configuration: For site-wide customization, you can define the PHP constants
ERROR_OPEN
andERROR_CLOSE
in yourconfig.php
file. When set, these constants will be used as the default HTML wrapping for all validation errors. For example:define('ERROR_OPEN', '<div class="site-wide-error">'); define('ERROR_CLOSE', '</div>');
These options allow you to easily integrate validation error displays with your site's design and CSS, ensuring a consistent look and feel across your application.
2. Inline Validation Errors Display
Trongate offers an alternative methodology for displaying form validation errors inline. With inline validation errors applied, individual form validation errors are displayed next to their respective form fields, providing more context for the end user.
How to Implement:
- Add the class 'highlight-errors' to your form tag.
- For each form field, use the validation_errors() function with the field name as an argument.
For example:
echo form_open($form_location, array('class' => 'highlight-errors'));
echo form_label('Username');
echo validation_errors('username');
echo form_input('username', $username, array("placeholder" => "Enter Username"));
echo form_label('First Name');
echo validation_errors('first_name');
echo form_input('first_name', $first_name, array("placeholder" => "Enter First Name"));
echo form_label('Last Name');
echo validation_errors('last_name');
echo form_input('last_name', $last_name, array("placeholder" => "Enter Last Name"));
echo form_label('Email Address');
echo validation_errors('email_address');
echo form_input('email_address', $email_address, array("placeholder" => "Enter Email Address"));
echo form_submit('submit', 'Submit');
echo anchor($cancel_url, 'Cancel', array('class' => 'button alt'));
echo form_close();
This method will display errors inline, next to their respective fields.
In situations where inline form validation errors appear, an alert will appear at the top of the page that:
- Makes the user aware of the fact that something went wrong.
- Informs the user that more details are 'highlighted below'.
This alert is contained within a CSS class named .validation-error-alert
. If you do not wish the alert to appear, simply apply a CSS rule onto the page that forces the element to remain hidden. For example:
.validation-error-alert {
display: none;
}
By effectively utilizing these validation error display techniques, you can create user-friendly forms that provide clear feedback, improving the overall user experience of your Trongate application.