PHP code example of programmatordev / fluent-validator
1. Go to this page and download the library: Download programmatordev/fluent-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/ */
programmatordev / fluent-validator example snippets
use ProgrammatorDev\FluentValidator\Validator;
// example: validate the user's age to ensure it's between 18 and 60
$errors = Validator::notBlank()
->greaterThanOrEqual(18)
->lessThan(60)
->validate($age);
if ($errors->count() > 0) {
// handle errors
}
use ProgrammatorDev\FluentValidator\Exception\ValidationFailedException;
use ProgrammatorDev\FluentValidator\Validator;
try {
Validator::notBlank()->assert($name);
Validator::notBlank()->email()->assert($email);
}
catch (ValidationFailedException $exception) {
// exception message will always be the first error thrown
$message = $exception->getMessage();
// value that failed validation
$value = $exception->getInvalidValue();
// get access to all errors
// returns a ConstraintViolationList object like in the validate method
$errors = $exception->getViolations();
// ...
}
use Symfony\Component\Validator\Constraints\GroupSequence;
isValid(mixed $value, string|GroupSequence|array|null $groups = null): bool
use ProgrammatorDev\FluentValidator\Validator;
if (!Validator::email()->isValid($email)) {
// handle invalid email
}
use Symfony\Component\Validator\Constraint;
/** @return Constraint[] */
getConstraints(): array
use ProgrammatorDev\FluentValidator\Validator;
$constraints = Validator::notBlank()->email()->getConstraints();
use ProgrammatorDev\FluentValidator\Validator;
// validate that array should have at least one value
// and each value should be between 0 and 100
$errors = Validator::count(min: 1)
->all(Validator::range(min: 0, max: 100)->getConstraints())
->validate($value);
addNamespace(string $namespace): void
use Symfony\Contracts\Translation\TranslatorInterface;
setTranslator(?TranslatorInterface $translator): void
namespace App\Constraint;
use Symfony\Component\Validator\Constraint;
class ContainsAlphanumeric extends Constraint
{
// set configurable options
}
namespace App\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ContainsAlphanumericValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
// custom validation logic
}
}
use ProgrammatorDev\FluentValidator\Validator;
Validator::addNamespace('App\Constraint');
Validator::notBlank()->containsAlphanumeric()->isValid('!'); // false
Validator::notBlank()->containsAlphanumeric()->isValid('v4l1d'); // true
use ProgrammatorDev\FluentValidator\Translator\Translator;
// set translator to Portuguese (Portugal) locale
Validator::setTranslator(new Translator('pt'));
// now all error messages will be in Portuguese
Validator::notBlank()->validate('');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.