PHP code example of simsoft / validator

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

    

simsoft / validator example snippets




use Simsoft\Validator;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Sequentially;

$inputs = $_POST;

$validator = Validator::make($inputs, [
    'email' => new Sequentially([
        new NotBlank(message: 'Email is validator->passes()) {
    echo 'passed';
    $validated = $validator->validated();                       // get all validated data.
    $email = $validator->validated('email');                    // get email value only.
    $data = $validator->safe()->only(['email', 'password']);    // get only these attributes
    $data = $validator->safe()->except(['remember_me']);        // get all attributes except 'remember_me'.
    $validated = $validator->safe()->all();                     // get all validated data.

    foreach($validator->safe() as $key => $value) {
        ...
    }

} elseif ($valildator->fails()) {
    echo 'failed';

    echo $validator->errors()->first('email');  // Display the email first error message.
    $errors = $validator->errors()->all();      // Retrieve array of all error messages.

    // loop through all 'email' error messages.
    foreach($validator->errors()->get('email') as $message) {
        ...
    }

    // Loop through all error messages.
    foreach($validator->errors() as $key => $messages) {
        foreach($messages as $message) {
            ...
        }
    }
}

namespace App\Validators;

use Simsoft\Validator;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Sequentially;

class LoginValidator extends Validator
{
    /** @var array Expecting attributes and its default value */
    protected array $attributes ['email', 'password', 'remember_me' => false];

    /**
     * Define the validation rules.
     *
     * @return array<mixed> The validation rules.
     */
    protected function rules(): array
    {
        return [
            'email' => new Sequentially([
                new NotBlank(['message' => 'Email is 

use App\Validators\LoginValidator;

$inputs = $_POST;

$validator = LoginValidator::make($inputs);
if ($validator->passes()) {
    echo 'passed';
    $data = $validator->validated();
} else {
    echo 'failed';
    print_r($validator->errors()->all());
}

use Simsoft\Validator;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Sequentially;

$validator = Validator::make($inputs);

$validator->addRule('password', new Sequentially([
    new NotBlank(['message' => 'Password is 

use Simsoft\Validator;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\PasswordStrength;
use Symfony\Component\Validator\Constraints\Sequentially;

$inputs = $_POST;

$validator = Validator::make($inputs, [
    'email' => new Sequentially([
        new NotBlank(['message' => 'Email is maxMessage' => 'Maximum {{ limit }} characters exceeded',
            'groups' => ['login', 'register'],
        ]),
        new PasswordStrength([
            'minScore' => PasswordStrength::STRENGTH_VERY_STRONG,
            'groups' => ['register'],
        ])
    ],
]);

// Apply those constraints belong to 'login' group only.
if ($validator->validate('login')) {
    echo 'Pass';
} else {
    echo 'Failed';
}

use Symfony\Component\Validator\Constraints\GroupSequence;

$validator = LoginValidator::make($_POST);

// Apply constraints of group 'login', then constraints of group 'strict'.
if ($validator->validate(new GroupSequence(['login', 'strict']))) {
    echo 'Pass';
} else {
    echo 'Failed';
}

namespace App\Validators;

use Closure;
use Simsoft\Validator;
use Simsoft\Validator\Rule;

$inputs = $_POST;

$validator = Validator::make($inputs, [
    // ...
    'password' => Rule::make(function(mixed $value, Closure $fail) {
        $min = 8;
        $max = 20;

        $length = mb_strlen($value, 'UTF-8');

        if ($length == 0) {
            $fail('Password is }
    })
]);

namespace App\Constraints;

use Closure;
use Simsoft\Validator\Constraints\ValidationRule;

class Password extends ValidationRule
{
    public string $message = 'At least 8 alphanumeric characters which include at least 1 uppercase, 1 lowercase, 1 digit and 1 special characters only.';
    protected string $charset = 'UTF-8';
    protected string $format = '/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^\da-zA-Z])(.{8,20})$/';

    protected int $min;
    protected int $max;

    public function __construct(mixed $options = null, array $groups = null, mixed $payload = null)
    {
        $this->min = $options['min'] ?? 8;
        $this->max = $options['max'] ?? 20;
        $this->format = $options['format'] ?? $this->format;
        $this->message = $options['message'] ?? $this->message;

        parent::__construct($options, $groups, $payload);
    }

    public function validate(mixed $value, Closure $fail): void
    {
        $length = mb_strlen($value, $this->charset);

        if ($length == 0) {
            $fail('Password is 

use App\Constraints\Password;
use Simsoft\Validator;

$input = $_POST;

$validator = Validator::make($input, [
    // ...
    'password' => new Password([
        'message' => 'Invalid password',
        'min' => 5,
        'max' => 10,
        'format' => '/new regex pattern/',
        'groups' => ['login'],
    ]),
])

if ($validator->passes()) {
    echo 'Pass';
} else {
    echo 'Failed';
}

use Closure;
use Simsoft\Validator;
use Simsoft\Validator\Rule;

$inputs = $_POST;

$validator = Validator::make($inputs, [
    // ...
    'password' => [
        Rule::make(function(mixed $value, Closure $fail) {
            if (!preg_match('/^w+$/', $value, $matches)) {
                $fail('Invalid password');
            }
        })
    ]

    'password_confirm' => [
        Rule::