Trongate Website Homepage

Setting Validation Rules

On the previous page, we defined the form submission location as follows:

$data['form_location'] = BASE_URL . 'students/submit/' . $update_id;

This configuration ensures that form submissions are directed to the 'Students' module, specifically to a method named submit.

Add the following method to your 'Students.php' controller file:

public function submit(): void {
    $this->module('trongate_security');
    $this->trongate_security->_make_sure_allowed();

    $submit = post('submit', true);

    if ($submit === 'Submit') {
        $this->validation->set_rules('username', 'Username', 'required|min_length[2]|max_length[255]');
        $this->validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[255]');
        $this->validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[255]');
        $this->validation->set_rules('email_address', 'Email Address', 'required|valid_email');

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

        if ($result) {
            // Form submission successful
            echo 'Form submission successful!'; // This will be improved later!
        } else {
            // Form submission error
            $this->create();
        }
    }
}

How to Set Form Validation Rules

Trongate offers a robust Validation class that facilitates setting rules, validating form submissions, and displaying error messages. This mechanism ensures that user input conforms to your application's requirements before processing.

Validation rules can be defined in your controller using the set_rules() method. In the submit() method provided above, the validation rules are applied as follows:


$this->validation->set_rules('username', 'Username', 'required|min_length[2]|max_length[255]');
$this->validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[255]');
$this->validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[255]');
$this->validation->set_rules('email_address', 'Email Address', 'required|valid_email');

Here are the validation rules used:

Rule Description
required Ensures that the field is not empty.
min_length[x] Specifies that the field must contain at least x characters.
max_length[x] Limits the field to a maximum of x characters.
valid_email Verifies that the field contains a valid email address.

Multiple validation rules can be combined using the pipe character (|). For example:

'required|min_length[2]|max_length[255]'

Setting Validation Rules Using Array-Based Validation

While the pipe method for setting validation rules is efficient, it may lead to lengthy and complex lines of code, particularly for forms with numerous fields and intricate validation requirements. To address this, Trongate provides an alternative approach: Array-Based Validation. This method offers a more structured and readable way to configure validation rules.

How It Works

Instead of using pipes to chain validation rules, the Array-Based Validation approach allows you to define rules as an associative array. Below is a comparison of the two methods:

Pipe Method (Traditional)

$this->validation->set_rules('username', 'Username', 'required|min_length[2]|max_length[255]');
$this->validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[255]');
$this->validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[255]');
$this->validation->set_rules('email_address', 'Email Address', 'required|valid_email');

Array-Based Validation


// Define validation rules for username
$username_rules = array(
    'required' => true,
    'min_length' => 2,
    'max_length' => 255
);
$validation_rules['username'] = $username_rules;

// Define validation rules for first name
$first_name_rules = array(
    'required' => true,
    'min_length' => 2,
    'max_length' => 255
);
$validation_rules['first_name'] = $first_name_rules;

// Define validation rules for last name
$last_name_rules = array(
    'required' => true,
    'min_length' => 2,
    'max_length' => 255
);
$validation_rules['last_name'] = $last_name_rules;

// Define validation rules for email address
$email_address_rules = array(
    'required' => true,
    'valid_email' => true
);
$validation_rules['email_address'] = $email_address_rules;

// Execute validation
$result = $this->validation->run($validation_rules);

The array-based validation method is a robust alternative for setting validation rules, especially advantageous for forms with numerous fields or complex validation needs. Select the method that best fits your project requirements and coding style preferences.

Key Points Regarding Setting Validation Rules

All of the validation rules that have been used in our example are built into the framework. In other words, the inner workings of the various validation rules have been written into the Trongate framework.

In the next section, we will explore how to create your own custom validation rules.