The Trongate PHP Framework
Documentation
Introduction
Quick Start
Basic Concepts
Understanding Routing
Controllers
Views
Assets
Modules Calling Modules
Parent & Child Modules
Database Operations
Modules within Modules
Templates & Themes
Helpers Explained
Form Handling
Working with Files
The Module Import Wizard
Authorization & Authentication
The API Explorer
Best Practices

Help Improve Our Docs

If you’ve found an error, spotted something missing, or feel a section could be clearer or better explained, we’d love to hear from you. Your feedback helps keep the documentation accurate and useful for everyone.

Please report issues or suggest improvements on GitHub. Community input is invaluable in making the docs stronger.

Not comfortable with GitHub? No problem — you can also get in touch with us directly via our contact form. We welcome all feedback.

Custom Form Validation Rules

In certain scenarios, you may need to implement form validation checks that go beyond the built-in rules provided by Trongate's Validation class. In such cases, custom form validation callbacks become invaluable.

General Concept

When using Trongate's Validation class to apply form validation checks, the set_rules method plays a crucial role in defining the validation rules for specific form fields. For instance, the following code ensures that both the 'first_name' and 'last_name' fields must:

  • Not be empty
  • Be at least 2 characters long
  • Be no more than 255 characters long

Adding Custom Validation Checks

To implement validation rules not covered by Trongate's default tests, you can create custom validation callbacks. For example, let's say you want to enforce the rule: "The last name cannot be 'Rambo'". To do this, follow these steps:

  1. Declare the custom validation check.
  2. Create a method to define the behavior of the custom validation check.

Declaring a Custom Validation Check

To declare a custom validation rule, prepend the rule name with 'callback_', followed by the name of the method that will handle the validation logic. For example, if you have a method called check_last_name() for this custom validation, you would append it to the set_rules declaration for 'last_name' field, like so:

This means the block of code for applying form validation rules would now look like this:

While it's not required, it's recommended to adhere to Trongate's Underscore Naming Convention whenever creating custom methods.

Creating a Custom Validation Method

Custom validation methods automatically receive the posted form field value as an argument and should return either a boolean true (indicating validation success) or an error message (as a string). The following demonstrates the basic structure of a typical custom validation callback method:

Custom form validation callback methods should only return one of the following two things:

  1. A string with a form validation error message, if the submitted value fails the validation test.
  2. A boolean of true, if the submitted value passes the validation test.

The behaviour of custom form validation callbacks may be described more fully by adding access modifiers, doc blocks, type hinting and return types, like so:

To implement the custom rule where the last name cannot be "Rambo", the method would look like this:

If the submitted last_name fails the custom validation check, the following error message will be returned:

We don't want guys like you in this town.

On the other hand, if the submitted last_name passes the custom validation, the method will return true, and no error message will be generated.

For enhanced code clarity and functionality, consider using doc blocks, access modifiers, type hints, and return types in your validation callback methods. Though optional, these practices improve code readability and maintainability. For example:

To learn more about docblocks, type hinting, and return types check out this YouTube tutorial.

By the way, there appears to be some ambiguity, in the PHP community, about how to write 'doc blocks'. Should it be two words, one word or something to do with camelCase? If you know the answer, please let us know!

In the next section, we will explore how display form validation errors.

×