kocal/validator

This package is abandoned and no longer maintained. The author suggests using the kocal/validator package instead.

Validator bassed on Laravel Validator, with support of translation.

v0.1.0 2017-04-15 23:00 UTC

This package is auto-updated.

Last update: 2024-05-17 18:06:10 UTC


README

Build Status

A PHP values validator that makes you able to use the great Laravel Validator, but outside a Laravel project.

Installation

$ composer require kocal/validator

Usage

All Laravel validation rules except exists and unique are supported.

<?php
use Kocal\Validator\Validator;

$rules = ['field' => 'required|min:5'];
$data = ['field' => 'Validation'];

$validator = new Validator($rules);
$validator->validate($data);
$validator->passes(); // true
$validator->fails(); // false
$validator->errors()->toArray(); // returns array of error messages

Advanced usage

Translations

Available validation translation languages: see src/lang directory. The default language is fr.

<?php
use Kocal\Validator\Validator;

$validator = new Validator([], 'es');

Custom validation rule

<?php
use Kocal\Validator\Validator;

$validator = new Validator(['field' => 'is_foo']);

$validator->extend('is_foo', function ($attribute, $value, $parameters, $validator) {
    return $value == 'foo';
}, "Le champ :attribute n'est pas égal à 'foo'.");

$validator->validate(['field' => 'not_foo']);