PHP code example of terminusstudio / phpvalidator

1. Go to this page and download the library: Download terminusstudio/phpvalidator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

terminusstudio / phpvalidator example snippets

 php
$config = [
            'useSession' => false 
          ];
 php
Container::set('validator', function() {
    return new \TS\PHPValidator\Validator($config);
});
 php
$validator = new \TS\PHPValidator\Validator($config);
 php
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');
}
 php
  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);;
    }
 php
$app->add(new \TS\PHPValidator\ValidatorMiddleware(Container::get('validator')));
 php
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;
});
 php
{% if has_error('email') %}
    {{  get_error('email') }}
{% endif %}
<input type="email" name="email" value="{{ get_value('email') }}">