2017 © Pedro Peláez
 

library validation

A simple little validation library

image

psecio/validation

A simple little validation library

  • Wednesday, April 25, 2018
  • by enygma
  • Repository
  • 1 Watchers
  • 5 Stars
  • 298 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Psecio/Validation

This library seeks to be a simple and reusable validation library for input based on rules., (*1)

Example

 'bar'
];
$rules = [
    'foo' => 'required|alpha'
];

$result = $v->execute($rules, $data);
var_export($result);

?>

Messages

You can also define custom messages for the failures using the third $messages parameter on the execute method. The key names on the array match to the value name + check type. For example:, (*2)

 'bar'
];
$rules = [
    'foo' => 'required|alpha'
];
$messages = [
    'foo' => ['alpha' => 'This is a custom message']
];

$result = $v->execute($rules, $data, $messages);
var_export($result);
```

In the example above, we define a custom message for the `alpha` check on the `foo` value. If that check were to fail, the error message output would be the new custom message instead of the default.

### Getting Errors

There are two method available to get the errors when the result of the `execute` method is `false`:

- The `errors` method that will return a nested key/value set of failure messages (top level is the value name with each check failure message under it)
- The `errorArray` method that will return a flattended set of messages useful for output to a page without requiring too much looping

### Checks

Here is a listing of the check types that Validation supports:

#### alpha
Checks for *only* alpha characters

#### alphanum
Checks for *only* alpha-numeric characters

#### numeric
Checks to ensure the value provided is numeric (integer, float, etc)

#### integer
Checks for integer-only values. Can also include minimum and maximum values:

```php
// Minimum of 1, max of 10
$rules = ['mynumber' => 'integer[1,10]'];
```

#### boolean
Checks for boolean values (`true`, `false`, `0`, `1` and strings `'0'`, `'1'`)

#### array
Checks to ensure the value provided is an array

#### length
Checks the value to ensure the (string) length matches requirements. You must provide a minimum value, but a maximum can also be defined

```php
// Using just the minimum, checking for a length of at least 3
$rules = ['mystring' => 'length[3]']

// Using both minimum and maximum, check for a length between 3 and 10
$rules = ['mystring' => 'length[3,10]']
```

#### date
Checks to be sure the value given is a *date* (as parsed by [strtotime](http://php.net/strtotime))

#### before
Checks to see if the value (a parseable date) is before the date provided (as parsed by [strtotime](http://php.net/strtotime))

```php
// Check to see if the date provided is before yesterday
$rules = [
    'myinputdate' => 'before[yesterday]'
];
```

#### after
Checks to see if the value (a parseable date) is after the date provided (as parsed by [strtotime](http://php.net/strtotime))

```php
// Check to see if the date is in the last three days
$rules = [
    'myinputdate' => 'after[-3 days]'
];
```

### in
Checks to ensure the value provided is in a set of values

```php
// Check to see if the value is one of "foo", "bar" or "baz"
$rules = [
    'myvalue' => 'in[foo,bar,baz]'
]
```

#### json
Checks to be sure the value is a valid JSON formatted string (using [json_decode](http://php.net/json_decode))

#### required
Checks to be sure the value exists

#### ip
Checks to ensure the value provided is a valid IPv4 or IPv6 formatted address

#### email
Check to ensure the value provided is a validly formatted email address

#### regex
Check to ensure the value matches a certain regular expression at least once

```php
$rules = [
    'mystring' => 'regex[/[0-9a-z]+/]'
]
```

#### equals
This check can be used to see if the values of two fields match exactly.

```
$data = [
    'foo' => 'test1',
    'bar' => 'test1'
];
$rules = [
    'bar' => 'equals[foo]'
];
```

#### callback

This check can be used to call custom logic via a static class method. For example, if your class is:

```php

Then your rule would look something like this:, (*3)

$rules = [
    'mystring' => 'callback[Foo::check]'
];

And the check method should return a boolean result., (*4)

The Versions

25/04 2018

dev-master

9999999-dev

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

21/09 2017

0.5.4

0.5.4.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

21/09 2017

0.5.3

0.5.3.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

25/03 2017

0.5.2

0.5.2.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

02/02 2017

0.5.1

0.5.1.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

20/12 2016

0.5

0.5.0.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

10/11 2016

0.4

0.4.0.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

31/10 2016

0.3

0.3.0.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

31/10 2016

0.2

0.2.0.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security

27/10 2016

0.1

0.1.0.0

A simple little validation library

  Sources   Download

MIT

The Development Requires

validation security