Skip to content

Commit 63baa4b

Browse files
committed
Adding in Required constraint
1 parent f93d432 commit 63baa4b

10 files changed

Lines changed: 91 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Since this library is very much alpha, I haven't had time to add all the [valida
5353
* Pattern
5454
* Max
5555
* Min
56+
* Required
5657
5758
## Usage
5859

src/Directive/Field/Constraint/AbstractComparison.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class AbstractComparison extends AbstractConstraint
2323
public function __construct($min, string $errorMessage = null)
2424
{
2525
$this->setValue($min);
26-
$this->setErrorMessageString($errorMessage);
26+
parent::__construct($errorMessage);
2727
}
2828

2929
/**

src/Directive/Field/Constraint/AbstractConstraint.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ abstract class AbstractConstraint implements ConstraintInterface
1818
/** @var ConstraintErrorMessage|null */
1919
private $errorMessage;
2020

21+
/**
22+
* AbstractConstraint constructor.
23+
*
24+
* @param null|string $errorMessageString
25+
* @throws \InvalidArgumentException
26+
*/
27+
public function __construct(string $errorMessageString = null)
28+
{
29+
$this->setErrorMessageString($errorMessageString);
30+
}
31+
2132
/**
2233
* @return null|ConstraintErrorMessage
2334
*/

src/Directive/Field/Constraint/AbstractLength.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(int $min = null, int $max = null, string $errorMessa
2828
{
2929
$this->setMin($min);
3030
$this->setMax($max);
31-
$this->setErrorMessageString($errorMessage);
31+
parent::__construct($errorMessage);
3232
}
3333

3434
/**

src/Directive/Field/Constraint/Email.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@
1010
*/
1111
class Email extends AbstractConstraint
1212
{
13-
/**
14-
* Email constructor.
15-
*
16-
* @param string|null $errorMessage
17-
* @throws \InvalidArgumentException
18-
*/
19-
public function __construct(string $errorMessage = null)
20-
{
21-
$this->setErrorMessageString($errorMessage);
22-
}
23-
2413
/**
2514
* @return string
2615
*/

src/Directive/Field/Constraint/Pattern.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Pattern extends AbstractConstraint
2323
public function __construct(string $pattern, string $errorMessage = null)
2424
{
2525
$this->setPattern($pattern);
26-
$this->setErrorMessageString($errorMessage);
26+
parent::__construct($errorMessage);
2727
}
2828

2929
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace C0ntax\ParsleyBundle\Directive\Field\Constraint;
4+
5+
/**
6+
* Class Required
7+
*
8+
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
9+
*/
10+
class Required extends AbstractConstraint
11+
{
12+
/**
13+
* @return string
14+
*/
15+
public static function getConstraintId(): string
16+
{
17+
return 'required';
18+
}
19+
20+
/**
21+
* @return array
22+
*/
23+
public function getViewAttr(): array
24+
{
25+
return $this->getMergedViewAttr('true');
26+
}
27+
}

src/Factory/ConstraintFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Min;
1212
use C0ntax\ParsleyBundle\Directive\Field\Constraint\MinLength;
1313
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Pattern;
14+
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Required;
1415
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
1516
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
1617
use Symfony\Component\Form\Extension\Core\Type\DateType;
@@ -37,7 +38,13 @@ public static function createFromValidationConstraint(
3738
Constraint $validationConstraint,
3839
FormInterface $form
3940
): ConstraintInterface {
40-
if ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\Length) {
41+
// TODO Change this to use the ViewInterface instead of the FormInterface as that makes a lot more sense!
42+
43+
if ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\NotNull) {
44+
return new Required($validationConstraint->message);
45+
} elseif ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\NotBlank) {
46+
return new Required($validationConstraint->message);
47+
} if ($validationConstraint instanceof \Symfony\Component\Validator\Constraints\Length) {
4148
if ($validationConstraint->min !== null && $validationConstraint->max !== null) {
4249
// TODO Pick a better message!
4350
return new Length(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace C0ntax\ParsleyBundle\Tests\Directive\Field\Constraint;
4+
5+
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Required;
6+
7+
class RequiredTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
public function testGetConstraintId()
11+
{
12+
self::assertEquals('required', Required::getConstraintId());
13+
}
14+
15+
public function testGetViewAttr()
16+
{
17+
$pattern = new Required();
18+
self::assertEquals(['data-parsley-required' => 'true'], $pattern->getViewAttr());
19+
20+
$pattern = new Required('Error message');
21+
self::assertEquals(
22+
['data-parsley-required' => 'true', 'data-parsley-required-message' => 'Error message'],
23+
$pattern->getViewAttr()
24+
);
25+
}
26+
}

tests/Factory/ConstraintFactoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use C0ntax\ParsleyBundle\Contracts\ConstraintInterface;
66
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Pattern;
7+
use C0ntax\ParsleyBundle\Directive\Field\Constraint\Required;
78
use C0ntax\ParsleyBundle\Factory\ConstraintFactory;
89
use C0ntax\ParsleyBundle\Form\Extension\ParsleyTypeExtension;
910
use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestType;
@@ -19,6 +20,8 @@
1920
use Symfony\Component\Validator\Constraints\Length;
2021
use Symfony\Component\Validator\Constraints\LessThan;
2122
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
23+
use Symfony\Component\Validator\Constraints\NotBlank;
24+
use Symfony\Component\Validator\Constraints\NotNull;
2225
use Symfony\Component\Validator\Constraints\Regex;
2326
use Symfony\Component\Validator\ConstraintViolationList;
2427
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -63,6 +66,18 @@ public function getFactoryTestData()
6366
$form = $factory->create(TestType::class, null, []);
6467

6568
return [
69+
[
70+
new NotNull(['message' => 'Give me something']),
71+
$form->get('id'),
72+
new Required('Give me something'),
73+
'NotNull to Required',
74+
],
75+
[
76+
new NotBlank(['message' => 'Give me something']),
77+
$form->get('id'),
78+
new Required('Give me something'),
79+
'NotBlank to Required',
80+
],
6681
[
6782
new Regex(
6883
[

0 commit comments

Comments
 (0)