fyre/validation

A validation library.

v3.0.5 2024-07-21 03:16 UTC

This package is auto-updated.

Last update: 2024-09-21 03:36:53 UTC


README

FyreValidation is a free, open-source validation library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/validation

In PHP:

use Fyre\Validation\Rule;
use Fyre\Validation\Validator;

Validators

$validator = new Validator();

Add

Add a validation rule.

  • $field is a string representing the field name.
  • $rule is a Closure or a Rule representing the validation rule.
  • $options is an array containing options for the validation rule.
    • on is a string representing the type of validation the rule applies to, and will default to null.
    • message is a string representing the error message for the rule, and will default to null.
    • name is a string representing the name of the validation rule, and will default to null.
    • skipEmpty is a boolean indicating whether the rule should be skipped if the value is empty, and will default to true.
$validator->add($field, $rule, $options);

Clear

Clear all rules from the Validator.

$validator->clear();

Get Field Rules

Get the rules for a field.

  • $field is a string representing the field name.
$rules = $validator->getFieldRules($field);

Remove

Remove a validation rule.

  • $field is a string representing the field name.
  • $name is a string representing the rule name.
$removed = $validator->remove($field, $name);

If the $name argument is omitted, all rules will be removed instead.

$validator->remove($field);

Validate

Perform validation and return any errors.

  • $data is an array containing the data to validate.
  • $type is a string representing the type of validation, and will default to null.
$errors = $validator->validate($data, $type);

Rules

Alpha

Create an "alpha" Rule.

Rule::alpha();

Alpha Numeric

Create an "alpha-numeric" Rule.

Rule::alphaNumeric();

Ascii

Create an "ASCII" Rule.

Rule::ascii();

Between

Create a "between" Rule.

  • $min is a number representing the minimum value (inclusive).
  • $max is a number representing the maximum value (inclusive).
Rule::between($min, $max);

Boolean

Create a "boolean" Rule.

Rule::boolean();

Decimal

Create a "decimal" Rule.

Rule::decimal();

Date

Create a "date" Rule.

Rule::date();

DateTime

Create a "date/time" Rule.

Rule::dateTime();

Differs

Create a "differs" Rule.

  • $field is a string representing the other field to compare against.
Rule::differs($field);

Email

Create an "email" Rule.

Rule::email();

Empty

Create an "empty" Rule.

Rule::empty();

Equals

Create an "equals" Rule.

  • $value is the value to compare against.
Rule::equals($value);

Exact Length

Create an "exact length" Rule.

  • $length is a number representing the length.
Rule::exactLength($length);

Greater Than

Create a "greater than" Rule.

  • $min is the minimum value.
Rule::greaterThan($min);

Greater Than Or Equals

Create a "greater than or equals" Rule.

  • $min is the minimum value.
Rule::greaterThanOrEquals($min);

In

Create an "in" Rule.

  • $values is an array containing the values to compare against.
Rule::in($values);

Integer

Create an "integer" Rule.

Rule::integer();

Ip

Create an "IP" Rule.

Rule::ip();

Ipv4

Create an "IPv4" Rule.

Rule::ipv4();

Ipv6

Create an "IPv6" Rule.

Rule::ipv6();

Less Than

Create a "less than" Rule.

  • $max is the maximum value.
Rule::lessThan($max);

Less Than Or Equals

Create a "less than or equals" Rule.

  • $max is the maximum value.
Rule::lessThanOrEquals($max);

Matches

Create a "matches" Rule.

  • $field is a string representing the other field to compare against.
Rule::matches($field);

Max Length

Create a "maximum length" Rule.

  • $length is a number representing the maximum length.
Rule::maxLength($length);

Min Length

Create a "minimum length" Rule.

  • $length is a number representing the minimum length.
Rule::minLength($length);

Natural Number

Create a "natural number" Rule.

Rule::naturalNumber();

Not Empty

Create an "not empty" Rule.

Rule::notEmpty();

Regex

Create a "regular expression" Rule.

  • $regex is a string representing the regular expression.
Rule::regex($regex);

Required

Create a "required" Rule.

Rule::required();

Require Presence

Create a "require presence" Rule.

Rule::requirePresence();

Time

Create a "time" Rule.

Rule::time();

Url

Create a "URL" Rule.

Rule::url();

Error Messages

Custom error messages can be used by supplying the message property of the $options array to the Validator add method.

$validator->add('field', Rule::required(), [
    'message' => 'The field is required.'
]);

Alternatively, for custom validation callbacks, a string can be returned and that will be used as the error messages.

$validator->add('field', function($value) {
    if ($value) {
        return true;
    }

    return 'The field is required.';
});

If a custom error message is not supplied, the rule name will be used to retrieve a Lang value. The field placeholder can be used for the field name, and any arguments supplied to the rule will be available as numeric placeholders.

// language/en/Validation.php

return [
    'required' => 'The {field} is required.'
];

If no error message is available, the error message will simply be set to "invalid".