PHP code example of digitalrevolution / symfony-console-validation

1. Go to this page and download the library: Download digitalrevolution/symfony-console-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/ */

    

digitalrevolution / symfony-console-validation example snippets


use DigitalRevolution\SymfonyConsoleValidation\AbstractValidatedInput;
use DigitalRevolution\SymfonyConsoleValidation\ValidationRules;

class ExampleInput extends AbstractValidatedInput
{
    public static function getValidationRules(): ValidationRules
    {
        return new ValidationRules([
            'arguments' => [
                'email'   => '== null ? null : (int)$value;
    }
}

class ExampleCommand extends Command
{
    public function __construct(private InputValidator $inputValidator, ?string $name = null)
    {
        parent::__construct($name);
    }
    
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // third argument will throw exception if input is invalid. Set to `false` if you want to handle the validation yourself.
        $validatedInput = $this->inputValidator->validate($input, ExampleInput::class, true);
        
        ...
    }    
}

class ExampleCommand extends Command
{
    ...
    
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $validatedInput = $this->inputValidator->validate($input, ExampleInput::class, false);
        if ($validatedInput->isValid() === false) {
            $violations = $validatedInput->getViolations();
            ...
        }
        ...
    }   
}