Lima documentation

The Docs

Learn all you need to know about Lima MVC.

Validation

Contents

Creating a validator

Lima includes a small input validator for request data. To use it, extend Lima\Requests\Validator, define your rules, and pass the input into the constructor.

use Lima\Requests\Validator;

class ContactValidator extends Validator
{
    protected $rules = [
        'name' => 'text|required,min:2,max:80',
        'email' => 'email|required',
        'message' => 'text|required,min:10',
    ];
}

Then use the validator from your controller.

$validator = new ContactValidator($_POST);
$input = $validator->getInput();
$errors = $validator->getErrors();

if (!empty($errors)) {
    $this->view('contact', [
        'errors' => $errors,
    ]);
    return;
}

getInput() returns the validated and sanitized input. getErrors() returns an array of validation errors keyed by input name.

Rule format

Rules use the following format:

type|option,option:value

The type comes first. Additional options are added after the pipe and separated with commas.

protected $rules = [
    'username' => 'text|required,min:3,max:30',
];

Supported types

Type Description
text, string Validates a string and escapes HTML characters
int Validates numeric input and returns an integer
float Validates a float value
array Validates an array
email Validates and sanitizes an email address

Supported options

Option Description
required The input must not be empty
min Minimum length for text, minimum value for numbers, or item count for arrays
max Maximum length for text, maximum value for numbers, or item count for arrays
match The input must match another input value

Matching fields

The match option is useful for confirmation fields.

protected $rules = [
    'password' => 'text|required,min:8',
    'confirm_password' => 'text|required,match:password',
];

If the values don't match, an error will be added to the confirmation field.

Returning validation errors

Errors are plain strings, which makes them easy to pass into a template.

if (!empty($errors['email'])) {
    echo '<p>' . $errors['email'] . '</p>';
}

For larger applications, you may want to wrap validation errors in your own form helper so every form renders messages consistently.

A complete controller example

class Contact extends \Lima\Core\Controller
{
    public function submit(): void
    {
        $validator = new ContactValidator($_POST);
        $input = $validator->getInput();
        $errors = $validator->getErrors();

        if (!empty($errors)) {
            $this->view('contact/index', [
                'errors' => $errors,
                'input' => $_POST,
            ]);
            return;
        }

        // Use $input here to send an email, create a model, or call a service.
        $this->view('contact/success');
    }
}

What to read next

Validation usually sits between your Controllers and Views and Models and Database, so those are the best places to go next.