PHP code example of tobento / app-validation

1. Go to this page and download the library: Download tobento/app-validation 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/ */

    

tobento / app-validation example snippets


use Tobento\App\AppFactory;

// Create the app
$app = (new AppFactory())->createApp();

// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();

use Tobento\App\AppFactory;
use Tobento\Service\Validation\ValidatorInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);
$app->booting();

$validator = $app->get(ValidatorInterface::class);

$validation = $validator->validating(
    value: 'foo',
    rules: 'alpha|minLen:2',
);

// var_dump($validation->isValid());
// bool(true)

// Run the app
$app->run();

use Tobento\Service\Validation\ValidatorInterface;

class SomeService
{
    public function __construct(
        protected ValidatorInterface $validator,
    ) {}
}

use Tobento\App\AppFactory;
use Tobento\Service\Validation\RulesInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// using the app on method:
$app->on(RulesInterface::class, function(RulesInterface $rules) {
    $rules->add(name: 'same', rule: new Same());
});

// Run the app
$app->run();

use Tobento\App\AppFactory;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();

use Tobento\App\AppFactory;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// HTTP boots:
// Required for request validation:
$app->boot(\Tobento\App\Http\Boot\Routing::class);
$app->boot(\Tobento\App\Http\Boot\RequesterResponser::class);

// Required for flashing input and messages:
$app->boot(\Tobento\App\Http\Boot\Session::class);

// VIEW boots:
// Optional boots for view support:
$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);
$app->boot(\Tobento\App\View\Boot\Messages::class);

// VALIDATION boots:
// Default error handler for handling ValidationException.
// You may create your own handler or add one with higher priority.
$app->boot(\Tobento\App\Validation\Boot\HttpValidationErrorHandler::class);

$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();

use Tobento\App\Validation\Http\ValidationRequest;
use Tobento\Service\Validation\ValidationInterface;
use Tobento\Service\Requester\RequesterInterface;
use Tobento\Service\Responser\ResponserInterface;
use Tobento\Service\Routing\RouterInterface;
use Psr\Http\Message\ResponseInterface;

class ProductController
{
    /**
     * Store a new product.
     */
    public function store(ValidationRequest $request): ResponseInterface
    {
        $validation = $request->validate(
            rules: [
                'title' => 'y change the behaviour by a custom validation error handler though.
        );

        // The product is valid, store product e.g.

        var_dump($validation instanceof ValidationInterface);
        // bool(true)

        // The following interfaces are available:
        var_dump($request->requester() instanceof RequesterInterface);
        // bool(true)

        var_dump($request->responser() instanceof ResponserInterface);
        // bool(true)

        var_dump($request->router() instanceof RouterInterface);
        // bool(true)

        // you may use the responser and router for redirection:
        return $request->responser()->redirect($request->router()->url('products.index'));
    }
}

use Tobento\App\Validation\Http\ValidationRequest;
use Psr\Http\Message\ResponseInterface;

class ProductController
{
    public function store(ValidationRequest $request): ResponseInterface
    {
        $validation = $request->validate(
            rules: [
                'title' => ' // ...
    }
}

use Tobento\App\AppFactory;

// Create the app
$app = (new AppFactory())->createApp();

// Adding boots
$app->boot(\Tobento\App\Validation\Boot\HttpValidationErrorHandler::class);

$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();
en