Trongate Docs
switch to dark modeswitch to dark mode
Displaying Form Validation Errors

Displaying Form Validation Errors

Let's deal with how to gracefully display form validation errors.  For those of you who prefer videos, here's a video showing you everything you need to know:

Video Tutorial

In this video I'm going to demonstrate how to gracefully display form validation errors.

How It Works

Whenever a form is submitted that fails our pre-defined validation tests, the error messages for each of the validation errors can be display on the page by calling:


Calling validation errors, with the validation_errors() method, results in each error being output to the screen inside a paragraph containing some red text.

Custom Error Tags

The validation_errors() method, which is built into the Trongate framework, can accept two optional arguments.

  • Opening HTML Tag
  • Closing HTML Tag

As an option, you can use this feature to change the appearance of form validation error messages.  For example, here's an example of validation_errors() being invoked where each error is going to be output inside a paragraph that contains a class of error.

validation_errors('<p class="error">', '</p>');

With a little CSS, it's easy to modify the appearance of our validation errors.  For example,

.error {
    background-color: tomato;
    color: white;
    padding: 7px;
    border-radius: 5px;
    font-weight: bold;

Re-Presenting The Form

Whenever form validation errors occur, it's a good practice to display the form once more, so that the end-user has an opportunity to try again.  This can be easily achieved with a simple IF statement.  For example,

$result = $this->validation_helper->run();

if ($result == true) {
    echo 'Well done!';
} else {

As you can see, in the code above we call the create() method whenever there are validation errors.  Doing this means that if there are validation errors then the end-user will immediately be taken back to the page that displays the form.

From inside our 'create' view file, we can then activate the displaying of validation errors by adding the validation_errors() method.  A good place to add this is under the page title.  For example,

<h1>Create New Record</h1>
<?=validation_errors() ?>

Remember, the validation_errors() method will only display form validation errors when at least one form validation test has not been passed.  In instances where there are no validation errors, the validation_errors() method will have no effect on the appearance or functionality of any web pages.


If you have a question or a comment relating to anything you've see here, please goto the Help Bar.