PHP code example of rohit-sagar / validatorplus

1. Go to this page and download the library: Download rohit-sagar/validatorplus 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/ */

    

rohit-sagar / validatorplus example snippets


use ValidatorPlus\Validator;

$data = [
    'name' => '',
    'email' => 'invalid-email'
];

$validator = new Validator();

$validator->field('name')
    ->label('Full Name')   // Optional alias
    ->

[
    'name' => ['The Full Name field is id email address.']
]

use ValidatorPlus\Contracts\RuleInterface;

class StartsWithA implements RuleInterface
{
    protected string $label = '';

    public function passes(string $field, $value): bool {
        return is_string($value) && str_starts_with($value, 'A');
    }

    public function message(string $field): string {
        return "The {$this->label} must start with the letter A.";
    }

    public function setLabel(string $label): void {
        $this->label = $label;
    }
}

$validator->field('nickname')
    ->label('Nickname')
    ->addRule(new StartsWithA());

$validator->field('email')->label('Email Address')->

src/
├── Validator.php           // Main entry point
├── FieldValidator.php      // Handles rule chains for a field
├── Contracts/
│   └── RuleInterface.php   // Interface for all rules
├── Rules/
│   ├── RequiredRule.php
│   ├── MinRule.php
│   ├── MaxRule.php
│   └── EmailRule.php
tests/
composer.json