Skip to content

A validation library for the Slim Framework. It internally uses Respect/Validation.

Notifications You must be signed in to change notification settings

achimha/Slim-Validation

 
 

Repository files navigation

Slim Framework Validation

Latest version Build Status Coverage Status Quality Score Total Downloads

Build Status PSR2 Conformance

A validation library for the Slim Framework. It internally uses Respect/Validation.

Install

Via Composer

$ composer require davidepastore/slim-validation

Requires Slim 3.0.0 or newer.

Usage

In most cases you want to register DavidePastore\Slim\Validation for a single route, however, as it is middleware, you can also register it for all routes.

Register per route

use Respect\Validation\Validator as v;

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register provider
$container['apiValidation'] = function () {
  //Create the validators
  $usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
  $ageValidator = v::numeric()->positive()->between(1, 20);
  $validators = array(
    'username' => $usernameValidator,
    'age' => $ageValidator
  );

  return new \DavidePastore\Slim\Validation\Validation($validators);
};

$app->get('/api/myEndPoint',function ($req, $res, $args) {
    //Here you expect 'username' and 'age' parameters
    if($this->apiValidation->hasErrors()){
      //There are errors, read them
      $errors = $this->apiValidation->getErrors();

      /* $errors contain:
      array(
        'username' => array(
          '"davidepastore" must have a length between 1 and 10',
        ),
        'age' => array(
          '"89" must be lower than or equals 20',
        ),
      );
      */
    } else {
      //No errors
    }

})->add($container->get('apiValidation'));

$app->run();

Register for all routes

use Respect\Validation\Validator as v;

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register provider
$container['validation'] = function () {
  //Create the validators
  $usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
  $ageValidator = v::numeric()->positive()->between(1, 20);
  $validators = array(
    'username' => $usernameValidator,
    'age' => $ageValidator
  );

  return new \DavidePastore\Slim\Validation\Validation($validators);
};

// Register middleware for all routes
// If you are implementing per-route checks you must not add this
$app->add($container->get('validation'));

$app->get('/foo', function ($req, $res, $args) {
  //Here you expect 'username' and 'age' parameters
  if($this->validation->hasErrors()){
    //There are errors, read them
    $errors = $this->validation->getErrors();

    /* $errors contain:
    array(
      'username' => array(
        '"davidepastore" must have a length between 1 and 10',
      ),
      'age' => array(
        '"89" must be lower than or equals 20',
      ),
    );
    */
  } else {
    //No errors
  }
});

$app->post('/bar', function ($req, $res, $args) {
  //Here you expect 'username' and 'age' parameters
  if($this->validation->hasErrors()){
    //There are errors, read them
    $errors = $this->validation->getErrors();
  } else {
    //No errors
  }
});

$app->run();

Translate errors

You can provide a callable function to translate the errors.

use Respect\Validation\Validator as v;

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register provider
$container['validation'] = function () {
  //Create the validators
  $usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
  $ageValidator = v::numeric()->positive()->between(1, 20);
  $validators = array(
    'username' => $usernameValidator,
    'age' => $ageValidator
  );

  $translator = function($message){
    $messages = [
        'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}',
        '{{name}} must be a string' => '{{name}} deve essere una stringa',
        '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve avere una dimensione di caratteri compresa tra {{minValue}} e {{maxValue}}',
    ];
    return $messages[$message];
  };

  return new \DavidePastore\Slim\Validation\Validation($validators, $translator);
};

// Register middleware for all routes or only for one...

$app->run();

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

About

A validation library for the Slim Framework. It internally uses Respect/Validation.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%