PHP code example of guyliangilsing / php-validation
1. Go to this page and download the library: Download guyliangilsing/php-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/ */
guyliangilsing / php-validation example snippets
use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
$strategy = new DefaultValidationStrategy();
$builder = new ValidatorBuilder($strategy);
// Your configuration logic here...
$validator = $builder->build();
use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use PHPValidation\Validator;
use function PHPValidation\Functions\ors([
'field1' => [
// Passes a new validation handler/strategy to the actual validator
$newStrategy = // Your custom strategy here...
$builder->setStrategy($newStrategy);
// Registers a new validator class that will be returned when you build the validator
$builder->setValidatorClassName(Validator::class);
$validator = $builder->build();
namespace PHPValidation\Factories\ValidatorFactory;
$factory = new ValidatorFactory();
$validator = $factory->createDefaultValidator();
use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
$strategy = new DefaultValidationStrategy();
$builder = new ValidatorBuilder($strategy);
// Your configuration logic here...
$validator = $builder->build();
use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use function PHPValidation\Functions\ired()],
]);
$validator = $builder->build();
$isValid = $validator->isValid([]); // Will return false
$errorMessages = $validator->getErrorMessages(); // Will return error messages
declare(strict_types=1);
namespace PHPValidation\Fields;
final class RequiredField implements FieldValidatorInterface
{
// Is used by the validation strategy and will skip this validator if it needs an existing field
public function fieldNeedsToExist(): bool
{
return false;
}
// The unique key for this validator
public function getKey(): string
{
return '
declare(strict_types=1);
namespace PHPValidation\Functions;
use PHPValidation\Fields\FieldValidatorInterface;
use PHPValidation\Fields\RequiredField;
function
use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use PHPValidation\Validator;
$strategy = new DefaultValidationStrategy();
$builder = new ValidatorBuilder($strategy);
$builder->setValidators([
'field1' => [new RequiredField(), new CustomValidator()],
]);
$validator = $builder->build();
use PHPValidation\Builders\ValidatorBuilder;
use PHPValidation\Strategies\DefaultValidationStrategy;
use PHPValidation\Validator;
use function PHPValidation\Functions\]);
$validator = $builder->build();
[
'key1' => [
'nestedKey1' => // YOUR VALUE HERE...
],
]
$builder->setValidators([
'field1' => [confirmValues('field2')], // Targets the key on the first array level
'field2' => [confirmValues('field2.nestedField1')], // Targets the key on the second array level
]);
$builder->setValidators([
'normalField' => [isAlphaNumeric()],
'extraField' => [isAlphaNumeric(['.', ',', '\\', '[', ']'])], // Whitelists each individual character within the array
]);