PHP code example of codercms / form-request

1. Go to this page and download the library: Download codercms/form-request 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/ */

    

codercms / form-request example snippets




declare(strict_types=1);

use Codercms\FormRequest\RequestNormalizer;
use Codercms\FormRequest\ValueNormalizer;
use Codercms\FormRequest\ValidationException;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

class TestFormRequest extends \Codercms\FormRequest\FormRequest
{
    protected function setupRules(): Assert\Collection
    {
        return new Assert\Collection(
            [
                'is_active' => new Assert\Optional(
                    new Assert\Type('bool')
                ),
            ]
        );
    }
}

$formRequest = new TestFormRequest(
    new RequestNormalizer(new ValueNormalizer()), 
    Validation::createValidator()
);

$data = [/* your incoming data here */];

try {
    $normalizedData = $formRequest->handle($data);
} catch (ValidationException $e) {
    $errors = $e->getViolationList();
    // handle errors here
}