2017 © Pedro Peláez
 

library slim-validation

A validator for the Slim PHP Micro-Framework

image

awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 23 Versions
  • 27 % Grown

The README.md

Slim Validation

SensioLabsInsight Scrutinizer Code Quality Build Status Latest Stable Version Total Downloads License, (*1)

A validator for PHP, using Respect Validation (Requires PHP 7+), (*2)

This project was originally designed to be used with the Micro-Framework "Slim", but can now be used with any psr/http-message compliant framework, or any other PHP project if you don't need request parameters validation., (*3)

Installation

``` bash $ composer require awurth/slim-validation, (*4)


### Configuration To initialize the validator, create a new instance of `Awurth\SlimValidation\Validator` ``` php Validator::__construct([ bool $showValidationRules = true [, array $defaultMessages = [] ]])
$showValidationRules
  • If set to true, errors will be stored in an associative array with the validation rules names as the key ``` php $errors = [ 'username' => [ 'length' => 'The username must have a length between 8 and 16', 'alnum' => 'The username must contain only letters (a-z) and digits (0-9)' ] ];
* If set to `false`, errors will be stored in an array of strings
``` php
$errors = [
    'username' => [
        'The username must have a length between 8 and 16',
        'The username must contain only letters (a-z) and digits (0-9)'
    ]
];
$defaultMessages

An array of messages to overwrite the default Respect Validation messages ``` php $defaultMessages = [ 'length' => 'This field must have a length between {{minValue}} and {{maxValue}} characters', 'notBlank' => 'This field is required' ];, (*5)


### Add the validator as a service You can add the validator to the app container to access it easily through your application ``` php $container['validator'] = function () { return new Awurth\SlimValidation\Validator(); };

Usage

``` php use Respect\Validation\Validator as V;, (*6)

// The validate method returns the validator instance $validator = $container->validator->validate($request, [ 'get_or_post_parameter_name' => V::length(6, 25)->alnum('_')->noWhitespace(), // ... ]);, (*7)

if ($validator->isValid()) { // Do something... } else { $errors = $validator->getErrors(); }, (*8)


### Validation methods #### Request parameters validation ``` php $_POST = [ 'username' => 'awurth', 'password' => 'my_password' ];

``` php /** * @var Psr\Http\Message\ServerRequestInterface $request */, (*9)

$validator->request($request, [ 'username' => V::notBlank(), 'password' => V::length(8) ]);, (*10)


#### Object properties validation ``` php class ObjectToValidate { private $privateProperty; protected $protectedProperty; public $pulicProperty; // ... }

``` php /** * @var object $object */, (*11)

$validator->object($object, [ 'privateProperty' => V::notBlank(), 'protectedProperty' => V::notBlank(), 'publicProperty' => V::notBlank() ]);, (*12)


If a property does not exist, the tested value will be `null` #### Array validation ``` php $arrayToValidate = [ 'key_1' => 'value_1', 'key_2' => 'value_2' ];

``` php /** * @var array $arrayToValidate */, (*13)

$validator->array($arrayToValidate, [ 'key_1' => V::notBlank(), 'key_2' => V::notBlank() ]);, (*14)


#### Single value validation ``` php $validator->value('12345', V::numeric(), 'secret_code');

The validate() method

``` php /** * @var Psr\Http\Message\ServerRequestInterface $request */, (*15)

$validator->validate($request, [ 'param' => V::notBlank() ]);, (*16)

/** * @var object $object */, (*17)

$validator->validate($object, [ 'property' => V::notBlank() ]);, (*18)

/** * @var array $array */, (*19)

$validator->array($array, [ 'key' => V::notBlank() ]);, (*20)

$secretCode = '12345'; $validator->validate($secretCode, [ 'rules' => V::numeric(), 'key' => 'secret_code' ]);, (*21)


### Error groups ``` php $user = [ 'username' => 'awurth', 'password' => 'my_password' ]; $address = [ 'street' => '...', 'city' => '...', 'country' => '...' ]; $validator->validate($user, [ // ... ], 'user'); $validator->validate($address, [ // ... ], 'address');

``` php $validator->getErrors();, (*22)

// Will return: [ 'user' => [ 'username' => [ // Errors... ] ], 'address' => [ 'street' => [ // Errors... ] ] ], (*23)


### Custom messages Slim Validation allows you to set custom messages for validation errors. There are 4 types of custom messages #### Default rules messages The ones defined in the `Validator` constructor. #### Global rules messages Messages that overwrite **Respect Validation** and **default rules messages** when calling the `validate` method. ``` php $container->validator->validate($request, [ 'get_or_post_parameter_name' => V::length(6, 25)->alnum('_')->noWhitespace(), // ... ], null, [ 'length' => 'Custom message', 'alnum' => 'Custom message', // ... ]);

Individual rules messages

Messages for a single request parameter. Overwrites all above messages., (*24)

``` php $container->validator->validate($request, [ 'get_or_post_parameter_name' => [ 'rules' => V::length(6, 25)->alnum('_')->noWhitespace(), 'messages' => [ 'length' => 'Custom message', 'alnum' => 'Custom message', // ... ] ], // ... ]);, (*25)


#### Single parameter messages Defines a single error message for a request parameter, ignoring the validation rules. Overwrites all messages. ``` php $container->validator->validate($request, [ 'get_or_post_parameter_name' => [ 'rules' => V::length(6, 25)->alnum('_')->noWhitespace(), 'message' => 'This field must have a length between 6 and 25 characters and contain only letters and digits' ], // ... ]);

Twig extension

This package comes with a Twig extension to display error messages and submitted values in your Twig templates. You can skip this step if you don't want to use it., (*26)

To use the extension, you must install twig first ``` bash $ composer require slim/twig-view, (*27)


### Configuration ``` php $container['view'] = function ($container) { // Twig configuration $view = new Slim\Views\Twig(...); // ... // Add the validator extension $view->addExtension( new Awurth\SlimValidation\ValidatorExtension($container['validator']) ); return $view; };

Functions

``` twig {# Use has_errors() function to know if a form contains errors #} {{ has_errors() }}, (*28)

{# Use has_error() function to know if a request parameter is invalid #} {{ has_error('param') }}, (*29)

{# Use error() function to get the first error of a parameter #} {{ error('param') }}, (*30)

{# Use errors() function to get all errors #} {{ errors() }}, (*31)

{# Use errors() function with the name of a parameter to get all errors of a parameter #} {{ errors('param') }}, (*32)

{# Use val() function to get the value of a parameter #} {{ val('param') }}, (*33)


## Example ##### AuthController.php ``` php public function register(Request $request, Response $response) { if ($request->isPost()) { $this->validator->validate($request, [ 'username' => V::length(6, 25)->alnum('_')->noWhitespace(), 'email' => V::notBlank()->email(), 'password' => [ 'rules' => v::length(6, 25), 'messages' => [ 'length' => 'This field must have a length between {{minValue}} and {{maxValue}} characters' ] ], 'confirm_password' => [ 'rules' => v::equals($request->getParam('password')), 'messages' => [ 'equals' => 'The password confirmation must be equal to the password' ] ] ]); if ($this->validator->isValid()) { // Register user in database return $response->withRedirect('url'); } } return $this->view->render($response, 'register.twig'); }
register.twig

``` twig , (*34)

{% if has_error('username') %}{{ error('username') }}{% endif %}
<input type="text" name="email" value="{{ val('email') }}">
{% if has_error('email') %}<span>{{ error('email') }}</span>{% endif %}

<input type="text" name="password">
{% if has_error('password') %}<span>{{ error('password') }}</span>{% endif %}

<input type="text" name="confirm_password">
{% if has_error('confirm_password') %}<span>{{ error('confirm_password') }}</span>{% endif %}

, (*35)

```

The Versions

21/06 2018

dev-master

9999999-dev https://github.com/awurth/SlimValidation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validator validation slim respect

15/11 2017

3.0.2

3.0.2.0 https://github.com/awurth/SlimValidation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validator validation slim respect

28/10 2017

3.0.1

3.0.1.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

21/09 2017

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

21/09 2017

3.0.0

3.0.0.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

17/08 2017

2.1.3

2.1.3.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

15/08 2017

2.1.2

2.1.2.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

15/08 2017

2.1.1

2.1.1.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

13/08 2017

2.1.0

2.1.0.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

11/08 2017

2.0.3

2.0.3.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

08/08 2017

2.0.2

2.0.2.0 https://github.com/awurth/slim-validation

A validator for the Slim PHP Micro-Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

03/04 2017

2.0.0

2.0.0.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

26/02 2017

1.4.1

1.4.1.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

26/02 2017

1.4.0

1.4.0.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

26/02 2017

1.3.1

1.3.1.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

21/02 2017

1.3.0

1.3.0.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

21/01 2017

1.2.3

1.2.3.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

21/01 2017

1.2.2

1.2.2.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

18/01 2017

1.2.1

1.2.1.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

09/01 2017

1.2.0

1.2.0.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

23/12 2016

1.1.1

1.1.1.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

14/12 2016

1.1.0

1.1.0.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim

09/12 2016

1.0.0

1.0.0.0 https://github.com/awurth/slim-validation

A validator for Slim micro-framework

  Sources   Download

MIT

The Requires

 

The Development Requires

validation slim