44namespace C0ntax \ParsleyBundle \Factory ;
55
66use C0ntax \ParsleyBundle \Contracts \ConstraintInterface ;
7- use C0ntax \ParsleyBundle \Directive \Field \Constraint \Email ;
8- use C0ntax \ParsleyBundle \Directive \Field \Constraint \Length ;
9- use C0ntax \ParsleyBundle \Directive \Field \Constraint \Max ;
10- use C0ntax \ParsleyBundle \Directive \Field \Constraint \MaxLength ;
11- use C0ntax \ParsleyBundle \Directive \Field \Constraint \Min ;
12- use C0ntax \ParsleyBundle \Directive \Field \Constraint \MinLength ;
13- use C0ntax \ParsleyBundle \Directive \Field \Constraint \Pattern ;
14- use C0ntax \ParsleyBundle \Directive \Field \Constraint \Required ;
7+ use C0ntax \ParsleyBundle \Directive \Field \Constraint as ParsleyConstraint ;
158use Symfony \Component \Form \Extension \Core \Type \BirthdayType ;
169use Symfony \Component \Form \Extension \Core \Type \DateTimeType ;
1710use Symfony \Component \Form \Extension \Core \Type \DateType ;
1811use Symfony \Component \Form \FormInterface ;
1912use Symfony \Component \Validator \Constraint ;
13+ use Symfony \Component \Validator \Constraints as SymfonyConstraint ;
2014use Symfony \Component \Validator \Constraints \AbstractComparison ;
2115
2216/**
@@ -40,66 +34,64 @@ public static function createFromValidationConstraint(
4034 ): ?ConstraintInterface {
4135 // TODO Change this to use the ViewInterface instead of the FormInterface as that makes a lot more sense!
4236
43- if ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \Valid) {
37+ if ($ validationConstraint instanceof SymfonyConstraint \Valid) {
4438 // This case is not an error. There just isn't a 'like-for-like' replacement
4539 return null ;
46- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \NotNull) {
47- return new Required ($ validationConstraint ->message );
48- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \NotBlank) {
49- return new Required ($ validationConstraint ->message );
50- } if ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \Length) {
40+ } elseif ($ validationConstraint instanceof SymfonyConstraint \NotNull) {
41+ return new ParsleyConstraint \ Required ($ validationConstraint ->message );
42+ } elseif ($ validationConstraint instanceof SymfonyConstraint \NotBlank) {
43+ return new ParsleyConstraint \ Required ($ validationConstraint ->message );
44+ } if ($ validationConstraint instanceof SymfonyConstraint \Length) {
5145 if ($ validationConstraint ->min !== null && $ validationConstraint ->max !== null ) {
5246 // TODO Pick a better message!
53- return new Length (
47+ return new ParsleyConstraint \ Length (
5448 $ validationConstraint ->min ,
5549 $ validationConstraint ->max ,
5650 self ::convertParameters ($ validationConstraint ->exactMessage )
5751 );
5852 } elseif ($ validationConstraint ->min !== null ) {
59- return new MinLength (
53+ return new ParsleyConstraint \ MinLength (
6054 $ validationConstraint ->min ,
6155 self ::convertParameters ($ validationConstraint ->minMessage )
6256 );
6357 } else {
64- return new MaxLength (
58+ return new ParsleyConstraint \ MaxLength (
6559 $ validationConstraint ->max ,
6660 self ::convertParameters ($ validationConstraint ->maxMessage )
6761 );
6862 }
69- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \Regex) {
70- return new Pattern ($ validationConstraint ->pattern , self ::convertParameters ($ validationConstraint ->message ));
71- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \Email) {
72- return new Email (self ::convertParameters ($ validationConstraint ->message ));
63+ } elseif ($ validationConstraint instanceof SymfonyConstraint \Regex) {
64+ return new ParsleyConstraint \ Pattern ($ validationConstraint ->pattern , self ::convertParameters ($ validationConstraint ->message ));
65+ } elseif ($ validationConstraint instanceof SymfonyConstraint \Email) {
66+ return new ParsleyConstraint \ Email (self ::convertParameters ($ validationConstraint ->message ));
7367 } elseif ($ validationConstraint instanceof AbstractComparison) {
7468 // This is an interesting case that requires the context of the form element. If any of these validations
7569 // happen to contain a value that is a string, it is assumed that the string is a dateTime. We should
7670 // only do dateTime evaluations if the field input type is 'date', otherwise Parsley just wont bother!
7771
78- $ innerType = $ form ->getConfig ()->getType ()->getInnerType ();
79-
8072 if (is_string ($ validationConstraint ->value ) && !static ::isFormHtml5DateType ($ form )) {
8173 throw new \RuntimeException ('Date evaluation called on a non-DateType field: ' .$ form ->getName ());
8274 }
8375
84- if ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \GreaterThanOrEqual) {
85- return new Min (
76+ if ($ validationConstraint instanceof SymfonyConstraint \GreaterThanOrEqual) {
77+ return new ParsleyConstraint \ Min (
8678 static ::convertMinMaxValue ($ validationConstraint ->value ),
8779 self ::convertParameters ($ validationConstraint ->message )
8880 );
89- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \GreaterThan) {
81+ } elseif ($ validationConstraint instanceof SymfonyConstraint \GreaterThan) {
9082 // Bit of a trickey one as isn't an analogous Parsley
91- return new Min (
83+ return new ParsleyConstraint \ Min (
9284 static ::convertMinMaxValue ($ validationConstraint ->value , true , true ),
9385 self ::convertParameters ($ validationConstraint ->message )
9486 );
95- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \LessThanOrEqual) {
96- return new Max (
87+ } elseif ($ validationConstraint instanceof SymfonyConstraint \LessThanOrEqual) {
88+ return new ParsleyConstraint \ Max (
9789 static ::convertMinMaxValue ($ validationConstraint ->value ),
9890 self ::convertParameters ($ validationConstraint ->message )
9991 );
100- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \LessThan) {
92+ } elseif ($ validationConstraint instanceof SymfonyConstraint \LessThan) {
10193 // Bit of a trickey one as isn't an analogous Parsley
102- return new Max (
94+ return new ParsleyConstraint \ Max (
10395 static ::convertMinMaxValue ($ validationConstraint ->value , true , false ),
10496 self ::convertParameters ($ validationConstraint ->message )
10597 );
0 commit comments