Skip to content

Data Validation

Hossein Pira edited this page Sep 11, 2023 · 9 revisions

Validation

A model used to validate input data to the application. This model is very modern and customizable as well as translatable.

Key Description
url Field for validating URLs
number Field for validating numeric values
min Field for validating minimum string length
max Field for validating maximum string length
required Field for validating required values
regex Field for validating values using regular expression
email Field for validating email addresses

Example

For example in a controller I call:

<?php

namespace Monster\App\Controllers;

use Monster\App\Models\Validation;

class HomeController
{
    public function index()
    {
        $data = [
            'name' => 'John Doe',
            'email' => '[email protected]',
            'age' => 19
        ];

        $rules = [
            'name' => 'required',
            'email' => 'required|email',
            'age' => 'number|min:1|max:3'
        ];

        $validator = new Validation($data, $rules);

        if ($validator->validate()) {
            echo 'Data is valid!';
        } else {
            $errors = $validator->getErrors();
            print_r($errors);
        }
    }
}
Clone this wiki locally