terminusstudio / phpvalidator
A PHP validator using respect validator. Built for Slim.
Installs: 34
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/terminusstudio/phpvalidator
Requires
- php: ^7.3 || ^8.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- respect/validation: 2.0.17
Requires (Dev)
- nyholm/psr7: ^1.3
- phpunit/phpunit: ^9.0
- slim/slim: ^4.0
- squizlabs/php_codesniffer: ^3.5
- twig/twig: ^3.1
README
A SlimPHP validator using respect validation package.
Install
Via Composer
$ composer require terminusstudio/phpvalidator
Usage
Initializing
Configuration
$config = [ 'useSession' => false ];
useSession- If set to true, validation results are saved in a session variable that can be accessed in the next request (using the ValidatorMiddleware). Defaults to false.
Using a container
Container::set('validator', function() { return new \TS\PHPValidator\Validator($config); });
Without container
$validator = new \TS\PHPValidator\Validator($config);
Example Usage
public function login($request, $response) { if ($request->getMethod() == 'POST') { $v = Container::get('validator')->validate($request, [ 'email' => v::noWhitespace()->notEmpty()->email(), 'password' => v::notEmpty()->length(8)->alnum('_'), ]);; if($v->isValid()) { //Do Processing } } return View::render($response, 'login.twig'); }
Middleware
The ValidatorMiddleware can be added to slim if you set the useSession to true in $config.
public function postLogin($request, $response) { $v = Container::get('validator')->validate($request, [ 'email' => v::noWhitespace()->notEmpty()->email(), 'password' => v::notEmpty()->length(8)->alnum('_'), ]);; if($v->isValid()) { //Do Processing } return $response->withHeader('Location', Router::getRouteParser()->urlFor('login.get'))->withStatus(302);; }
If there were any errors, the next request will have access to the errors and values. To enable the middleware just add ValidatorMiddleware class to the Slim app and pass the validator instance.
$app->add(new \TS\PHPValidator\ValidatorMiddleware(Container::get('validator')));
Twig Extension
This plugin also supports Twig functions. To enable just add ValidatorTwig when initializing twig. This requires slim/twig-view package.
Container::set('view', function() { $view = Twig::create(....); // Add the validator extension and pass the validator instance to it $view->addExtension( new \TS\PHPValidator\ValidatorTwig(Container::get('validator')) ); return $view; });
There are currently 5 functions supported by the extension,
has_errors()- Returns true if there are any errorshas_error($key)- Returns true if$keyis invalidget_errors()- Returns an array containing all errorsget_error($key, $toString = true)- Returns an array containing all errors for a specific$key. If$toStringis set to true, then it returns a string.get_value($key, $default = null)- Returns the value for a specific$key.
$key is the field name set in the request.
{% if has_error('email') %}
{{ get_error('email') }}
{% endif %}
<input type="email" name="email" value="{{ get_value('email') }}">
License
The MIT License (MIT). Please see License File for more information.