jcaillot / laminas-before-validator
Laminas Validator; Let you compare two date strings
v1.1
2021-10-11 23:37 UTC
Requires
- php: ^7.4 || ~8.0.0
- laminas/laminas-mvc: ^3.1
- laminas/laminas-validator: ^2.15
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- laminas/laminas-developer-tools: ^2.1.1
- laminas/laminas-test: ^3.4.2
- phpstan/phpstan: ^0.12.99
README
Date Before validator for the Laminas framework
Laminas\Validator\GreaterThan supports only the validation of numbers. This simple custom Validator, will let you compare two date strings.
Installation
composer require jcaillot/laminas-before-validator
Usage
The validator needs to know the name of the other date field for comparison. It needs to know the input date format. Here are the three required options:
- second_date_field (default to endDate): The name the second date field.
- date_format (default to Y-m-d H:i:s): the string format for the two dates.
- strict (default to false): will allow two identical dates to validate.
Add Validator as usual in your fieldset:
use Laminas\Form\Fieldset; use Laminas\InputFilter\InputFilterProviderInterface; use Chaman\Validator\BeforeValidator; class DemoFieldset extends Fieldset implements InputFilterProviderInterface { ... public function getInputFilterSpecification(): array { return [ // date1 'date1' => [ 'required' => true, 'filters' => [ ['name' => 'StringTrim'], ['name' => 'StripTags'], ], 'validators' => [ [ 'name' => 'NotEmpty', 'break_chain_on_failure' => true ], [ 'name' => BeforeValidator::class, 'options' => [ 'second_date_field' => 'date2', 'date_format' => 'Y-m-d', 'strict' => true, 'messages' => [ BeforeValidator::NOT_BEFORE => '"%value%" should be before the second date.', BeforeValidator::NOT_A_VALID_DATE => '"%value%" is not a valid date.', ], ] ] ], ], // date2 'date2' => [ 'required' => true, 'filters' => [ ['name' => 'StringTrim'], ['name' => 'StripTags'], ], 'validators' => [ [ 'name' => 'NotEmpty', 'break_chain_on_failure' => true ], ], ], ]; } ...