2017 © Pedro Peláez
 

library form-validator

Validation of form data using defined rulesets.

image

forestry/form-validator

Validation of form data using defined rulesets.

  • Tuesday, October 6, 2015
  • by daniel.melzer
  • Repository
  • 1 Watchers
  • 0 Stars
  • 63 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Forestry Form validator

Latest Version Software License Build Status Codacy Badge Total Downloads, (*1)

Library to validate form data based on rules., (*2)

Install

Via Composer, (*3)

$ composer require forestry/form-validator

Usage

Create a validator

$validator = new Forestry\FormValidator\Validator();

Define a ruleset for a form

$rules = [
    'name' => 'alpha|required',
    'age' => 'num|required',
];

Validate the form data

$validator->validate($_POST, $rules);

if ($validator->hasErrors()) {
    $errors = $validator->getErrors();
}

Show error message

<label for="name">Name</label>
<input type="text" name="name" id="name">
<?= (!empty(Forestry\FormValidator\Validator::error('name')) ? Forestry\FormValidator\Validator::error('name') : '') ?>

You can also wrap error messages with custom markup:, (*4)

Forestry\FormValidator\Validator::error('confirm_password', '<span class="text-danger">{message}</span>');

Rules

This package comes with the following built-in rules:, (*5)

Rule Parameter Description
alpha none Allows only text, space, . and -
alphanum none Allows text and numerical values
boolean none Checks if the value can be interpreted as a boolean true (true, 1, yes or on).
date format Checks if the value is a valid date, time or date-time string. You can provide a date format to check against.
email none Checks if the value is a valid email address.
float none Checks if the value is a valid float value.
integer none Checks if the value is a valid integer value.
ip none Checks if the value is a valid IP address.
max number Checks if the value exceeds number characters.
min number Checks if the value has at least number characters.
natural none Checks if the value is a natural number.
number none Checks if the value is a number.
phone none Checks if the value is a valid phone number format.
required none Checks if the value is set.
same fieldname Compares if the value matches with the one in fieldname.
url none Checks if the value is a valid URL.

same rule example

With this rule, you can define that two fields must have the same value, e.g. password fields:, (*6)

$rules['password'] = 'required|min:8';
$rules['confirm_password'] = 'same:password';

Custom messages

Instead of using the default error messages, you can also pass a custom one with each rule:, (*7)

$rules['tos'] = "required--Please accept the terms of service";

Custom rules

You can define your own rules:, (*8)

First, create a class which implements the Forestry\FormValidator\SimpleRuleInterface or Forestry\FormValidator\ParameterRuleInterface. The later on is used for rules whose have a parameter., (*9)

use Forestry\FormValidator\SimpleRuleInterface;

MyRule implements SimpleRuleInterface
{
    public function validate($value)
    {
        // Your code.
    }

    public function getMessage($value)
    {
        // Your code.
    }
}

Next, register your rule with a name and the full qualified class name., (*10)

$validator->registerRule('myrule', '\MyRule');

If you want to change an already registered rule, use the updateRule() method., (*11)

$validator->updateRule('url', '\MyRule');

Testing

bash $ phpunit, (*12)

Contributing

Please see CONTRIBUTING for details., (*13)

Credits

License

The MIT License (MIT). Please see License File for more information., (*14)

The Versions

06/10 2015

dev-master

9999999-dev https://github.com/ForestryCodes/form-validator

Validation of form data using defined rulesets.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

form validation forestry

06/10 2015

dev-develop

dev-develop https://github.com/ForestryCodes/form-validator

Validation of form data using defined rulesets.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

form validation forestry

06/10 2015

0.1.1

0.1.1.0 https://github.com/ForestryCodes/form-validator

Validation of form data using defined rulesets.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

form validation forestry

17/04 2015

0.1.0

0.1.0.0 https://github.com/ForestryCodes/form-validator

Validation of form data using defined rulesets.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

form validation forestry