ryodevz/validator

v1.8.0 2023-11-02 17:53 UTC

This package is auto-updated.

Last update: 2024-10-31 00:18:40 UTC


README

Latest Version

Laravel-like validation and a few others.

Installing validator

The recommended way to install validator is through Composer.

composer require ryodevz/validator

Validator example usage

<?php

use Ryodevz\Validator\Facades\Validator;

// Make validation
$validator = Validator::make([
    'name'  => $_POST['name'],
    'email' => $_POST['email'],
], [
    'name'  => 'required|min:3',
    'email' => 'required|email',
]);

// Validate
$validator->validate();

// Check if it passes validation
if(!$validator->fails()) {
    // code . . .
}

// All error messages with key
$validator->errors();

// All error messages without key
$validator->all();

// First error message
$validator->first();

Custom error message from config file

Create one config file in config/validator.php and fill it with

<?php

return [
    'array' => 'The :attribute must be an array.',
    'active_url' => 'The :attribute is not a valid URL.',
    'boolean' => 'The :attribute field must be true or false.',
    'confirmed' => 'The :attribute confirmation does not match.',
    'in' => 'The selected :attribute is invalid.',
    'ip' => 'The :attribute must be a valid IP address.',
    'ip4' => 'The :attribute must be a valid IPv4 address.',
    'ip6' => 'The :attribute must be a valid IPv6 address.',
    'integer' => 'The :attribute must be an integer.',
    'max' => [
        'integer' => 'The :attribute must not be greater than :max.',
        'string' => 'The :attribute must not be greater than :max characters.',
        'array' => 'The :attribute must not have more than :max items.',
    ],
    'min' => [
        'integer' => 'The :attribute must be at least :min.',
        'string' => 'The :attribute must be at least :min characters.',
        'array' => 'The :attribute must have at least :min items.',
    ],
    'not_in' => 'The selected :attribute is invalid.',
    'required' => 'The :attribute field is required.',
    'required_with' => 'The :attribute field is required when :required_with is present.',
    'required_without' => 'The :attribute field is required when :required_without is not present.',
    'same' => 'The :attribute and :same must match.',
    'string' => 'The :attribute must be a string.',
    'timezone' => 'The :attribute must be a valid timezone.',
    'url' => 'The :attribute must be a valid URL.'
];

Rules

Some rules available :