When you build a web application, users can submit information through forms, URLs, and other requests. But how can you make sure that the information is correct before using it?

This is where data validation becomes important.

Inphinit provides a Validator class that helps you check user input with simple, reusable rules. Instead of writing many if statements, you can define what each field must contain.

In this article, we will explore the basics of validation in Inphinit through short PHP examples.

Note: Inphinit's Validator is an experimental feature. Its implementation may change in future versions.

1. Validating POST data

Let's start with a common example: a registration form.

First, import the Validator class and create a validator for POST data:

use Inphinit\Experimental\Validation\Validator; $validator = Validator::post(); 

Next, define the fields you want to validate:

$validator->field('name', $name, 'required|min:3|max:30'); $validator->field('email', $email, 'required|email'); 

Each call to field() receives three arguments:

  • The name of the field.
  • A variable that will receive its value.
  • The validation rules.

In this example, the name is mandatory and must contain between 3 and 30 characters. The email field is also mandatory and must contain a valid email address.

Now, check whether the submitted data is valid:

if ($validator->valid()) { echo 'All data is valid!'; } else { foreach ($validator->failures() as $failure) { echo '<p>', htmlspecialchars($failure, ENT_QUOTES, 'UTF-8'), '</p>'; } } 

The valid() method returns true when all registered fields pass validation. Otherwise, failures() provides the error messages.

2. Understanding validation rules

Rules tell Inphinit what kind of data a field should accept. You can combine multiple rules using the pipe (|) character.

Here are some common examples:

Rule What it checks
required The field must be provided.
email The value must be a valid email address.
url The value must be a valid HTTP or HTTPS URL.
digit The value must contain an integer.
decimal The value must contain a decimal number.
bool The value must represent a boolean.
min:5 The value must have at least 5 characters, or meet a numeric minimum when used with digit or decimal.
max:20 The value must have at most 20 characters, or meet a numeric maximum when used with digit or decimal.
in: The value must match one of the options in a list.
regex: The value must match a regular expression.
equal: The value must match another field.

Let's look at a few practical examples.

Checking an age:

$validator->field('age', $age, 'required|digit|min:18|max:120'); 

This requires an integer between 18 and 120.

Accepting only specific categories:

$validator->field( 'category', $category, 'required|in:electronics,books,clothing' ); 

The category must be one of the three listed options.

Checking a password confirmation:

$validator->field('password', $password, 'required|min:8'); $validator->field( 'password_confirmation', $confirmation, 'required|equal:password' ); 

The equal rule checks whether the confirmation matches the password field.

3. Validating GET parameters

You can also validate values received through a GET request.

For example, imagine a page that filters products by search term and category:

$validator = Validator::get(); $validator->field('query', $query, 'min:5'); $validator->field( 'type', $type, 'in:electronics,motors,fashion,sports' ); if ($validator->valid()) { echo 'Search parameters are valid.'; } 

The main difference is the method used to create the validator: Validator::get() instead of Validator::post().

The validation rules work in the same way.

4. Working with boolean values

Sometimes, a form sends values that represent true or false.

Inphinit provides the bool rule for this situation:

$validator->field('active', $active, 'required|bool'); 

The rule recognizes values such as:

  • yes, true, and 1 as true.
  • no, false, and 0 as false.

Other values are converted to null.

This is useful when working with checkboxes, status fields, or settings that can be enabled or disabled.

5. Validating arrays and nested data

Not all data comes directly from an HTML form. Sometimes, you need to validate an existing PHP array.

You can create a Validator instance by passing the array to its constructor:

$data = [ 'user' => [ 'email' => 'user@example.com' ] ]; $validator = new Validator($data); $validator->field( 'user.email', $email, 'required|email' ); if ($validator->valid()) { echo 'The email is valid.'; } 

Notice the dot notation in user.email. It allows you to access nested array values without having to manually navigate through each level.

This approach is also useful when processing structured data, such as JSON requests.

6. Customizing error messages

By default, Inphinit provides error messages in English. You can customize them to match your application's language or style.

For example:

$validator->setMessage( 'required', 'Please fill in this field.' ); 

You can configure messages for different rules. Inphinit also supports integration with translation systems such as Gettext.

This makes it easier to build applications that support multiple languages.

7. Additional validation rules

Besides the standard rules, Inphinit includes some extended rules:

  • cpf: validates a Brazilian individual taxpayer registration number.
  • cnpj: validates a Brazilian company taxpayer registration number.
  • cpfcnpj: accepts either a CPF or a CNPJ.
  • ip: validates an IP address.
  • password: provides additional password validation.

These rules can save time when your application needs to validate specific types of information.

Conclusion

Data validation is an essential part of building reliable web applications. It helps prevent incorrect information from entering your application and reduces the amount of repetitive code you need to write.

With Inphinit's Validator class, you can validate POST and GET parameters, work with nested arrays, combine different rules, and customize error messages.

Start with simple rules such as required, email, and min, then explore more advanced options as your application grows.

Reference