Trongate Docs
switch to dark modeswitch to dark mode
Form Validation (The Pipe Method)

Form Validation (The Pipe Method)

Now, let's turn our attention to the business of form validation.  With Trongate, there are a variety of different ways to handle form validation.  Here, we're going to focus on The Pipe Method.  This is a technique that involves piping validation rules together and invoking an in-built form validation protocol.

If you're the kind of developer who prefers watching videos, here's a video for you, showing you everything you need to know about this topic:

Video Tutorial

In this video I'm going to demonstrate form validation using the pipe method.

If you're the kind of developer who prefers written docs, here's everything that gets covered in the video above:

Creating A Form Validation Method - First Steps

To get started with our form validation, we're going to create a method inside our 'books' module.  We're call this method submit().  To begin, our method is simply going to check for a posted 'submit' variable.  If one is found that happens to have a value of 'Submit' (notice the uppercase 'S'!), then we'll assume that somebody must have pressed the submit button on our form.

function submit() {
    $submit = post('submit');
    if ($submit == 'Submit') {
        //look out, it's a validation situation!

Did You Know?
The post() method is built into the Trongate framework.  It checks for a posted variable with name that matches the first argument that has been passed into the post() method.  If a variable has been posted with a name that matches the first argument then the value of that posted variable will be returned.  If no such variable has been posted then an empty string will be returned.

Calling The Validation Helper

Now, inside our IF statement, we're going to call upon Trongate's in-built form validation helper.  The validation helper is a PHP class that resides inside the 'engine' folder.  With the help of the validation helper, we can:

  • define some validation tests that we'd like to run
  • run our validation tests
  • report on what errors, if any, were found

Let's go through each of these items, one at a time.

Defining Form Validation Tests

The basic syntax for defining form validation tests is:

$this->validation_helper->set_rules(x, y, z);

With this syntax:

  • x represents a form field name
  • y represents a form field label
  • z represents some validation tests to be executed

The idea here is that each form field to be validated should have one 'set_rules' declaration.

Declaring Form Validation Rules 

Trongate comes with a helper file to assist you in creating form validation rules.  Full details of how this works can be found at:

The documentation for the form validation helper comes complete with a full Validation Rules Reference.  Scroll down to the bottom of the page to view this.

Below is some sample code that runs validation tests on three of our form fields.

$this->validation_helper->set_rules('title', 'book title', 'required|min_length[3]|max_length[75]');
$this->validation_helper->set_rules('description', 'book description', 'required');
$this->validation_helper->set_rules('author_id', 'author', 'required');

Understanding The Code Above

The code above defines some form validation tests to be carried out on three of our posted form fields.  As you can see, there are three lines of validation rules declarations, with one line being dedicated to each form field that is to be validated.  In our sample code:

  • the 'title' form field is required and must have a minimum length of three characters and a maximum length of seventy five characters
  • the 'description' form field is required
  • the 'author_id' form field is required

Top Tip
You do not have to perform validation tests on every single form field.

Running Form Validation Tests

With our form validation tests defined, we can run the tests by calling upon the validation helpers run() method.  This method will return true if the form passed all validation tests and false if there was at least one form validation error.  All of this can be achieved with just one line of code:

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

Our Complete Submit Method

Below is some sample code, showing you our complete submit() method.  As you can see, our code tests for a posted 'submit' button, we then define some validation rules and then we run our validation tests.

By using a simple IF statement on a $result variable, we can easily find out if our submitted form has passed the validation tests or failed.

function submit() {
    $submit = post('submit');

    if ($submit == 'Submit') {
        $this->validation_helper->set_rules('title', 'book title', 'required|min_length[3]|max_length[75]');
        $this->validation_helper->set_rules('description', 'book description', 'required');
        $this->validation_helper->set_rules('author_id', 'author', 'required');

        //run the validation tests
        $result = $this->validation_helper->run($validation_rules);

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

Just To Let You Know
The validation_errors() method is a function that has been built into the Trongate framework.  It displays a text summary of all any form validation errors that have been produced.


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