From 98983fcfe932e749e3d9978708cffe1ccdb8ca1f Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Thu, 25 Jan 2018 12:25:45 +0000 Subject: [PATCH 1/3] Cleaning the code a little --- src/Factory/ConstraintFactory.php | 54 +++++++++++++------------------ 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/src/Factory/ConstraintFactory.php b/src/Factory/ConstraintFactory.php index 23ac879..015b1a5 100644 --- a/src/Factory/ConstraintFactory.php +++ b/src/Factory/ConstraintFactory.php @@ -4,19 +4,13 @@ namespace C0ntax\ParsleyBundle\Factory; use C0ntax\ParsleyBundle\Contracts\ConstraintInterface; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Email; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Length; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Max; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\MaxLength; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Min; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\MinLength; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Pattern; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Required; +use C0ntax\ParsleyBundle\Directive\Field\Constraint as ParsleyConstraint; use Symfony\Component\Form\Extension\Core\Type\BirthdayType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints as SymfonyConstraint; use Symfony\Component\Validator\Constraints\AbstractComparison; /** @@ -40,66 +34,64 @@ public static function createFromValidationConstraint( ): ?ConstraintInterface { // TODO Change this to use the ViewInterface instead of the FormInterface as that makes a lot more sense! - if ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\Valid) { + if ($validationConstraint instanceof SymfonyConstraint\Valid) { // This case is not an error. There just isn't a 'like-for-like' replacement return null; - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\NotNull) { - return new Required($validationConstraint->message); - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\NotBlank) { - return new Required($validationConstraint->message); - } if ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\Length) { + } elseif ($validationConstraint instanceof SymfonyConstraint\NotNull) { + return new ParsleyConstraint\Required($validationConstraint->message); + } elseif ($validationConstraint instanceof SymfonyConstraint\NotBlank) { + return new ParsleyConstraint\Required($validationConstraint->message); + } if ($validationConstraint instanceof SymfonyConstraint\Length) { if ($validationConstraint->min !== null && $validationConstraint->max !== null) { // TODO Pick a better message! - return new Length( + return new ParsleyConstraint\Length( $validationConstraint->min, $validationConstraint->max, self::convertParameters($validationConstraint->exactMessage) ); } elseif ($validationConstraint->min !== null) { - return new MinLength( + return new ParsleyConstraint\MinLength( $validationConstraint->min, self::convertParameters($validationConstraint->minMessage) ); } else { - return new MaxLength( + return new ParsleyConstraint\MaxLength( $validationConstraint->max, self::convertParameters($validationConstraint->maxMessage) ); } - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\Regex) { - return new Pattern($validationConstraint->pattern, self::convertParameters($validationConstraint->message)); - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\Email) { - return new Email(self::convertParameters($validationConstraint->message)); + } elseif ($validationConstraint instanceof SymfonyConstraint\Regex) { + return new ParsleyConstraint\Pattern($validationConstraint->pattern, self::convertParameters($validationConstraint->message)); + } elseif ($validationConstraint instanceof SymfonyConstraint\Email) { + return new ParsleyConstraint\Email(self::convertParameters($validationConstraint->message)); } elseif ($validationConstraint instanceof AbstractComparison) { // This is an interesting case that requires the context of the form element. If any of these validations // happen to contain a value that is a string, it is assumed that the string is a dateTime. We should // only do dateTime evaluations if the field input type is 'date', otherwise Parsley just wont bother! - $innerType = $form->getConfig()->getType()->getInnerType(); - if (is_string($validationConstraint->value) && !static::isFormHtml5DateType($form)) { throw new \RuntimeException('Date evaluation called on a non-DateType field: '.$form->getName()); } - if ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\GreaterThanOrEqual) { - return new Min( + if ($validationConstraint instanceof SymfonyConstraint\GreaterThanOrEqual) { + return new ParsleyConstraint\Min( static::convertMinMaxValue($validationConstraint->value), self::convertParameters($validationConstraint->message) ); - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\GreaterThan) { + } elseif ($validationConstraint instanceof SymfonyConstraint\GreaterThan) { // Bit of a trickey one as isn't an analogous Parsley - return new Min( + return new ParsleyConstraint\Min( static::convertMinMaxValue($validationConstraint->value, true, true), self::convertParameters($validationConstraint->message) ); - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\LessThanOrEqual) { - return new Max( + } elseif ($validationConstraint instanceof SymfonyConstraint\LessThanOrEqual) { + return new ParsleyConstraint\Max( static::convertMinMaxValue($validationConstraint->value), self::convertParameters($validationConstraint->message) ); - } elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\LessThan) { + } elseif ($validationConstraint instanceof SymfonyConstraint\LessThan) { // Bit of a trickey one as isn't an analogous Parsley - return new Max( + return new ParsleyConstraint\Max( static::convertMinMaxValue($validationConstraint->value, true, false), self::convertParameters($validationConstraint->message) ); From 5409bd3b82e4e69b2dd39b38fc9c3e8936769a8e Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Thu, 25 Jan 2018 13:21:45 +0000 Subject: [PATCH 2/3] Added Range support --- README.md | 7 + .../Field/Constraint/AbstractComparison.php | 6 +- src/Directive/Field/Constraint/Range.php | 103 ++++++++++++ src/Factory/ConstraintFactory.php | 15 +- .../Directive/Field/Constraint/RangeTest.php | 32 ++++ tests/Factory/ConstraintFactoryTest.php | 151 ++++++++++-------- 6 files changed, 240 insertions(+), 74 deletions(-) create mode 100644 src/Directive/Field/Constraint/Range.php create mode 100644 tests/Directive/Field/Constraint/RangeTest.php diff --git a/README.md b/README.md index e60756c..6aa39dc 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,13 @@ Since this library is very much alpha, I haven't had time to add all the [valida * Max * Min * Required +* Range + +## Specific Validaton Notes + +Sometimes there is not a 1-2-1 mapping of Symfony error messages to Parsley. This is because with Parsley, you supply only one error message for the validator which is then displayed at runtime, whereas with Symfony constraints, they can pick a specific error message based on what didn't validate. Listed here are any Symfony validators where I've had to pick a specific error message that you should override using the ConstraintErrorMessage directive (so that if server based validations kick in, they will still have a message that makes sense) + +* Range ## Usage diff --git a/src/Directive/Field/Constraint/AbstractComparison.php b/src/Directive/Field/Constraint/AbstractComparison.php index b590fb4..ad769a1 100644 --- a/src/Directive/Field/Constraint/AbstractComparison.php +++ b/src/Directive/Field/Constraint/AbstractComparison.php @@ -16,13 +16,13 @@ abstract class AbstractComparison extends AbstractConstraint /** * Min constructor. * - * @param \DateTime|float|int $min + * @param \DateTime|float|int $value * @param string|null $errorMessage * @throws \InvalidArgumentException */ - public function __construct($min, string $errorMessage = null) + public function __construct($value, string $errorMessage = null) { - $this->setValue($min); + $this->setValue($value); parent::__construct($errorMessage); } diff --git a/src/Directive/Field/Constraint/Range.php b/src/Directive/Field/Constraint/Range.php new file mode 100644 index 0000000..f73d545 --- /dev/null +++ b/src/Directive/Field/Constraint/Range.php @@ -0,0 +1,103 @@ +setMin($min); + $this->setMax($max); + parent::__construct($errorMessageString); + } + + /** + * @return string + */ + public static function getConstraintId(): string + { + return 'range'; + } + + /** + * @return array + */ + public function getViewAttr(): array + { + $minValue = $this->createStringValue($this->getMin()); + $maxValue = $this->createStringValue($this->getMax()); + + return $this->getMergedViewAttr('["'.$minValue.'", "'.$maxValue.'"]'); + } + + /** + * @param $value + * @return string + */ + private function createStringValue($value): string + { + if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) { + return $value->format('Y-m-d H:i:s'); + } else { + return (string) $value; + } + } + + /** + * @return \DateTime|\DateTimeImmutable|float|int + */ + private function getMin() + { + return $this->min; + } + + /** + * @param \DateTime|\DateTimeImmutable|float|int $min + * @return Range + */ + private function setMin($min): Range + { + $this->min = $min; + + return $this; + } + + /** + * @return \DateTime|\DateTimeImmutable|float|int + */ + private function getMax() + { + return $this->max; + } + + /** + * @param \DateTime|\DateTimeImmutable|float|int $max + * @return Range + */ + private function setMax($max): Range + { + $this->max = $max; + + return $this; + } +} diff --git a/src/Factory/ConstraintFactory.php b/src/Factory/ConstraintFactory.php index 015b1a5..0a937fc 100644 --- a/src/Factory/ConstraintFactory.php +++ b/src/Factory/ConstraintFactory.php @@ -34,14 +34,15 @@ public static function createFromValidationConstraint( ): ?ConstraintInterface { // TODO Change this to use the ViewInterface instead of the FormInterface as that makes a lot more sense! - if ($validationConstraint instanceof SymfonyConstraint\Valid) { + if ($validationConstraint instanceof SymfonyConstraint\Valid) { // This case is not an error. There just isn't a 'like-for-like' replacement return null; } elseif ($validationConstraint instanceof SymfonyConstraint\NotNull) { return new ParsleyConstraint\Required($validationConstraint->message); } elseif ($validationConstraint instanceof SymfonyConstraint\NotBlank) { return new ParsleyConstraint\Required($validationConstraint->message); - } if ($validationConstraint instanceof SymfonyConstraint\Length) { + } + if ($validationConstraint instanceof SymfonyConstraint\Length) { if ($validationConstraint->min !== null && $validationConstraint->max !== null) { // TODO Pick a better message! return new ParsleyConstraint\Length( @@ -96,7 +97,17 @@ public static function createFromValidationConstraint( self::convertParameters($validationConstraint->message) ); } + } elseif ($validationConstraint instanceof SymfonyConstraint\Range) { + // No error message translation here + + if (is_string($validationConstraint->min) && !static::isFormHtml5DateType($form)) { + throw new \RuntimeException('Date evaluation called on a non-DateType field: '.$form->getName()); + } + return new ParsleyConstraint\Range( + static::convertMinMaxValue($validationConstraint->min), + static::convertMinMaxValue($validationConstraint->max) + ); } throw new \RuntimeException('Unsupported Symfony Constraint: '.get_class($validationConstraint)); diff --git a/tests/Directive/Field/Constraint/RangeTest.php b/tests/Directive/Field/Constraint/RangeTest.php new file mode 100644 index 0000000..0c6320e --- /dev/null +++ b/tests/Directive/Field/Constraint/RangeTest.php @@ -0,0 +1,32 @@ + '["1", "10"]'], $pattern->getViewAttr()); + + $pattern = new Range(1, 10, 'Error message'); + self::assertEquals(['data-parsley-range' => '["1", "10"]', 'data-parsley-range-message' => 'Error message'], $pattern->getViewAttr()); + + $minDate = (new \DateTime('now'))->sub(new \DateInterval('P2W')); + $maxDate = (new \DateTime('now'))->add(new \DateInterval('P2W')); + + $minDateString = $minDate->format('Y-m-d H:i:s'); + $maxDateString = $maxDate->format('Y-m-d H:i:s'); + + $pattern = new Range($minDate, $maxDate, 'Error message'); + self::assertEquals(['data-parsley-range' => '["'.$minDateString.'", "'.$maxDateString.'"]', 'data-parsley-range-message' => 'Error message'], $pattern->getViewAttr()); + } +} diff --git a/tests/Factory/ConstraintFactoryTest.php b/tests/Factory/ConstraintFactoryTest.php index 014ec42..2678e82 100644 --- a/tests/Factory/ConstraintFactoryTest.php +++ b/tests/Factory/ConstraintFactoryTest.php @@ -3,8 +3,7 @@ namespace C0ntax\ParsleyBundle\Tests\Factory; use C0ntax\ParsleyBundle\Contracts\ConstraintInterface; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Pattern; -use C0ntax\ParsleyBundle\Directive\Field\Constraint\Required; +use C0ntax\ParsleyBundle\Directive\Field\Constraint as ParsleyConstraint; use C0ntax\ParsleyBundle\Factory\ConstraintFactory; use C0ntax\ParsleyBundle\Form\Extension\ParsleyTypeExtension; use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestType; @@ -14,23 +13,13 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\Forms; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Constraints\Email; -use Symfony\Component\Validator\Constraints\GreaterThan; -use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; -use Symfony\Component\Validator\Constraints\Length; -use Symfony\Component\Validator\Constraints\LessThan; -use Symfony\Component\Validator\Constraints\LessThanOrEqual; -use Symfony\Component\Validator\Constraints\NotBlank; -use Symfony\Component\Validator\Constraints\NotNull; -use Symfony\Component\Validator\Constraints\Regex; -use Symfony\Component\Validator\Constraints\Valid; +use Symfony\Component\Validator\Constraints as SymfonyConstriant; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Validator\ValidatorInterface; class ConstraintFactoryTest extends \PHPUnit_Framework_TestCase { - /** * @dataProvider getFactoryTestData */ @@ -44,7 +33,6 @@ public function testCreateFromValidationConstraint( try { $return = ConstraintFactory::createFromValidationConstraint($constraint, $form); } catch (\RuntimeException $exception) { - } self::assertEquals($expected, $return, $testName); } @@ -68,36 +56,36 @@ public function getFactoryTestData() return [ [ - new Valid(), + new SymfonyConstriant\Valid(), $form->get('id'), null, 'Valid to Null', ], [ - new NotNull(['message' => 'Give me something']), + new SymfonyConstriant\NotNull(['message' => 'Give me something']), $form->get('id'), - new Required('Give me something'), + new ParsleyConstraint\Required('Give me something'), 'NotNull to Required', ], [ - new NotBlank(['message' => 'Give me something']), + new SymfonyConstriant\NotBlank(['message' => 'Give me something']), $form->get('id'), - new Required('Give me something'), + new ParsleyConstraint\Required('Give me something'), 'NotBlank to Required', ], [ - new Regex( + new SymfonyConstriant\Regex( [ 'pattern' => '/pattern/', 'message' => 'This doesn\'t look right', ] ), $form->get('id'), - new Pattern('/pattern/', 'This doesn\'t look right'), + new ParsleyConstraint\Pattern('/pattern/', 'This doesn\'t look right'), 'Regex to Pattern', ], [ - new Length( + new SymfonyConstriant\Length( [ 'min' => 1, 'max' => 20, @@ -105,118 +93,118 @@ public function getFactoryTestData() ] ), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Length(1, 20, 'This doesn\'t look right'), + new ParsleyConstraint\Length(1, 20, 'This doesn\'t look right'), 'Length to Length', ], [ - new Length( + new SymfonyConstriant\Length( [ 'min' => 1, 'minMessage' => 'This doesn\'t look right', ] ), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\MinLength(1, 'This doesn\'t look right'), + new ParsleyConstraint\MinLength(1, 'This doesn\'t look right'), 'Length to MinLength', ], [ - new Length( + new SymfonyConstriant\Length( [ 'max' => 20, 'maxMessage' => 'This doesn\'t look right', ] ), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\MaxLength(20, 'This doesn\'t look right'), + new ParsleyConstraint\MaxLength(20, 'This doesn\'t look right'), 'Length to MaxLength', ], [ - new Email( + new SymfonyConstriant\Email( [ 'message' => 'This doesn\'t look right', ] ), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Email('This doesn\'t look right'), + new ParsleyConstraint\Email('This doesn\'t look right'), 'Email to Email', ], // Max 'n' Min (integer) [ - new GreaterThanOrEqual(['value' => 10, 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThanOrEqual(['value' => 10, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min(10, 'Ouch'), + new ParsleyConstraint\Min(10, 'Ouch'), 'GreaterThanOrEqual to Min (integer)', ], [ - new GreaterThan(['value' => 10, 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThan(['value' => 10, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min(11, 'Ouch'), + new ParsleyConstraint\Min(11, 'Ouch'), 'GreaterThan to Min (integer)', ], [ - new LessThanOrEqual(['value' => 10, 'message' => 'Ouch']), + new SymfonyConstriant\LessThanOrEqual(['value' => 10, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max(10, 'Ouch'), + new ParsleyConstraint\Max(10, 'Ouch'), 'LessThanOrEqual to Max (integer)', ], [ - new LessThan(['value' => 10, 'message' => 'Ouch']), + new SymfonyConstriant\LessThan(['value' => 10, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max(9, 'Ouch'), + new ParsleyConstraint\Max(9, 'Ouch'), 'LessThan to Max (integer)', ], // Max 'n' Min (float) [ - new GreaterThanOrEqual(['value' => 10.0, 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThanOrEqual(['value' => 10.0, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min(10, 'Ouch'), + new ParsleyConstraint\Min(10, 'Ouch'), 'GreaterThanOrEqual to Min (float)', ], [ - new GreaterThan(['value' => 10.0, 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThan(['value' => 10.0, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min(10.0000001, 'Ouch'), + new ParsleyConstraint\Min(10.0000001, 'Ouch'), 'GreaterThan to Min (float)', ], [ - new LessThanOrEqual(['value' => 10.0, 'message' => 'Ouch']), + new SymfonyConstriant\LessThanOrEqual(['value' => 10.0, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max(10, 'Ouch'), + new ParsleyConstraint\Max(10, 'Ouch'), 'LessThanOrEqual to Max (float)', ], [ - new LessThan(['value' => 10.0, 'message' => 'Ouch']), + new SymfonyConstriant\LessThan(['value' => 10.0, 'message' => 'Ouch']), $form->get('id'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max(9.9999999, 'Ouch'), + new ParsleyConstraint\Max(9.9999999, 'Ouch'), 'LessThanOrEqual to Max (float)', ], // Max 'n' Min (DateTime) (The id field isn't a DateType so we shouldn't get a result back!) [ - new GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('id'), null, 'GreaterThanOrEqual to Null (date (non-date field))', ], [ - new GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('id'), null, 'GreaterThan to Null (date (non-date field))', ], [ - new LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('id'), null, 'LessThanOrEqual to Null (date (non-date field))', ], [ - new LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('id'), null, 'LessThan to Null (date (non-date field))', @@ -225,83 +213,108 @@ public function getFactoryTestData() // Max 'n' Min (DateTime) (The date field is a DateType so we should get a result back!) [ - new GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('date'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min('2018-01-19 00:00:00', 'Ouch'), + new ParsleyConstraint\Min('2018-01-19 00:00:00', 'Ouch'), 'GreaterThanOrEqual to Min (date (date field))', ], [ - new GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('date'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min('2018-01-19 00:00:01', 'Ouch'), + new ParsleyConstraint\Min('2018-01-19 00:00:01', 'Ouch'), 'GreaterThan to Min (date (date field))', ], [ - new LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('date'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max('2018-01-19 00:00:00', 'Ouch'), + new ParsleyConstraint\Max('2018-01-19 00:00:00', 'Ouch'), 'LessThanOrEqual to Max (date (date field))', ], [ - new LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('date'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max('2018-01-18 23:59:59', 'Ouch'), + new ParsleyConstraint\Max('2018-01-18 23:59:59', 'Ouch'), 'LessThan to Max (date (date field))', ], // Max 'n' Min (DateTime) (The dob field is a BirthdayType so we shoul get a result back!) [ - new GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dob'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min('2018-01-19 00:00:00', 'Ouch'), + new ParsleyConstraint\Min('2018-01-19 00:00:00', 'Ouch'), 'GreaterThanOrEqual to Min (date (birthday field))', ], [ - new GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dob'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Min('2018-01-19 00:00:01', 'Ouch'), + new ParsleyConstraint\Min('2018-01-19 00:00:01', 'Ouch'), 'GreaterThan to Min (date (birthday field))', ], [ - new LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dob'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max('2018-01-19 00:00:00', 'Ouch'), + new ParsleyConstraint\Max('2018-01-19 00:00:00', 'Ouch'), 'LessThanOrEqual to Min (date (birthday field))', ], [ - new LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dob'), - new \C0ntax\ParsleyBundle\Directive\Field\Constraint\Max('2018-01-18 23:59:59', 'Ouch'), + new ParsleyConstraint\Max('2018-01-18 23:59:59', 'Ouch'), 'LessThan to Min (date (birthday field))', ], // Max 'n' Min (DateTime) (The dateNotHtml field is a DateType but not HTML5 so we should get null back) [ - new GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dateNotHtml5'), null, 'GreaterThanOrEqual to Null (date (non-html5-date field))', ], [ - new GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\GreaterThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dateNotHtml5'), null, 'GreaterThan to Null (date (non-html5-date field))', ], [ - new LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThanOrEqual(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dateNotHtml5'), null, 'LessThanOrEqual to Null (date (non-html5-date field))', ], [ - new LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), + new SymfonyConstriant\LessThan(['value' => '2018-01-19', 'message' => 'Ouch']), $form->get('dateNotHtml5'), null, 'LessThan to Null (date (non-html5-date field))', ], + + // Range - Numeric + + [ + new SymfonyConstriant\Range(['min' => 1, 'max' => 10, 'minMessage' => 'Too small', 'maxMessage' => 'Too big']), + $form->get('id'), + new ParsleyConstraint\Range(1, 10), + 'Range to Range (numeric)', + ], + + // Range - DateTime + [ + new SymfonyConstriant\Range(['min' => '2018-01-25', 'max' => '2018-01-25', 'minMessage' => 'Too small', 'maxMessage' => 'Too big']), + $form->get('id'), + null, + 'Range to Range (date invalid)', + ], + + [ + new SymfonyConstriant\Range(['min' => '2018-01-25', 'max' => '2018-01-26', 'minMessage' => 'Too small', 'maxMessage' => 'Too big']), + $form->get('date'), + new ParsleyConstraint\Range('2018-01-25 00:00:00', '2018-01-26 00:00:00'), + 'Range to Range (date valid)', + ], + ]; } From 9b48c72bc9fa5b953f5e341cd02030efb1b7bf42 Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Thu, 25 Jan 2018 13:22:16 +0000 Subject: [PATCH 3/3] Feature add --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 036b20e..b631b63 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "c0ntax/parsley-bundle", - "version": "0.3.0", + "version": "0.4.0", "type": "symfony-bundle", "description": "A bridge between Symfony and Parsley.js", "license": "Apache-2.0",