PHP code example of danieltm / validation-io

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

    

danieltm / validation-io example snippets


use Daniel\Validator\Valid\Email;
use Daniel\Validator\Valid\NotEmpty;

final class ModelValidation
{
    #[NotEmpty]
    private string $nome;

    #[Email]
    private string $email;
}

use Daniel\Origins\Controller;
use Daniel\Origins\Get;
use Daniel\Origins\Request;
use Daniel\Validator\Props\Valid;

#[Controller]
final class TesteController
{
    #[Get("/")]
    #[Valid(ModelValidation::class)]
    function index(Request $request) {
        // A validação será processada automaticamente
    }
}

   use Daniel\Validator\Props\AbstractValidation;
   use Attribute;

   #[Attribute(Attribute::TARGET_PROPERTY)]
   #[ValidationProvider(new MinLengthValidator())]
   class MinLength extends AbstractValidation
   {
       public function __construct(private int $length, string $message = "Valor muito curto.") {
           parent::__construct($message);
       }
   }
   

   use Daniel\Validator\BaseValidator;

   class MinLengthValidator implements BaseValidator
   {
       private int $length;

       public function __construct(int $length)
       {
           $this->length = $length;
       }

       public function isValid($value): bool
       {
           return strlen($value) >= $this->length;
       }
   }