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,71 +34,80 @@ 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+ }
45+ if ($ validationConstraint instanceof SymfonyConstraint \Length) {
5146 if ($ validationConstraint ->min !== null && $ validationConstraint ->max !== null ) {
5247 // TODO Pick a better message!
53- return new Length (
48+ return new ParsleyConstraint \ Length (
5449 $ validationConstraint ->min ,
5550 $ validationConstraint ->max ,
5651 self ::convertParameters ($ validationConstraint ->exactMessage )
5752 );
5853 } elseif ($ validationConstraint ->min !== null ) {
59- return new MinLength (
54+ return new ParsleyConstraint \ MinLength (
6055 $ validationConstraint ->min ,
6156 self ::convertParameters ($ validationConstraint ->minMessage )
6257 );
6358 } else {
64- return new MaxLength (
59+ return new ParsleyConstraint \ MaxLength (
6560 $ validationConstraint ->max ,
6661 self ::convertParameters ($ validationConstraint ->maxMessage )
6762 );
6863 }
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 ));
64+ } elseif ($ validationConstraint instanceof SymfonyConstraint \Regex) {
65+ return new ParsleyConstraint \ Pattern ($ validationConstraint ->pattern , self ::convertParameters ($ validationConstraint ->message ));
66+ } elseif ($ validationConstraint instanceof SymfonyConstraint \Email) {
67+ return new ParsleyConstraint \ Email (self ::convertParameters ($ validationConstraint ->message ));
7368 } elseif ($ validationConstraint instanceof AbstractComparison) {
7469 // This is an interesting case that requires the context of the form element. If any of these validations
7570 // happen to contain a value that is a string, it is assumed that the string is a dateTime. We should
7671 // only do dateTime evaluations if the field input type is 'date', otherwise Parsley just wont bother!
7772
78- $ innerType = $ form ->getConfig ()->getType ()->getInnerType ();
79-
8073 if (is_string ($ validationConstraint ->value ) && !static ::isFormHtml5DateType ($ form )) {
8174 throw new \RuntimeException ('Date evaluation called on a non-DateType field: ' .$ form ->getName ());
8275 }
8376
84- if ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \GreaterThanOrEqual) {
85- return new Min (
77+ if ($ validationConstraint instanceof SymfonyConstraint \GreaterThanOrEqual) {
78+ return new ParsleyConstraint \ Min (
8679 static ::convertMinMaxValue ($ validationConstraint ->value ),
8780 self ::convertParameters ($ validationConstraint ->message )
8881 );
89- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \GreaterThan) {
82+ } elseif ($ validationConstraint instanceof SymfonyConstraint \GreaterThan) {
9083 // Bit of a trickey one as isn't an analogous Parsley
91- return new Min (
84+ return new ParsleyConstraint \ Min (
9285 static ::convertMinMaxValue ($ validationConstraint ->value , true , true ),
9386 self ::convertParameters ($ validationConstraint ->message )
9487 );
95- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \LessThanOrEqual) {
96- return new Max (
88+ } elseif ($ validationConstraint instanceof SymfonyConstraint \LessThanOrEqual) {
89+ return new ParsleyConstraint \ Max (
9790 static ::convertMinMaxValue ($ validationConstraint ->value ),
9891 self ::convertParameters ($ validationConstraint ->message )
9992 );
100- } elseif ($ validationConstraint instanceof \ Symfony \ Component \ Validator \ Constraints \LessThan) {
93+ } elseif ($ validationConstraint instanceof SymfonyConstraint \LessThan) {
10194 // Bit of a trickey one as isn't an analogous Parsley
102- return new Max (
95+ return new ParsleyConstraint \ Max (
10396 static ::convertMinMaxValue ($ validationConstraint ->value , true , false ),
10497 self ::convertParameters ($ validationConstraint ->message )
10598 );
10699 }
100+ } elseif ($ validationConstraint instanceof SymfonyConstraint \Range) {
101+ // No error message translation here
102+
103+ if (is_string ($ validationConstraint ->min ) && !static ::isFormHtml5DateType ($ form )) {
104+ throw new \RuntimeException ('Date evaluation called on a non-DateType field: ' .$ form ->getName ());
105+ }
107106
107+ return new ParsleyConstraint \Range (
108+ static ::convertMinMaxValue ($ validationConstraint ->min ),
109+ static ::convertMinMaxValue ($ validationConstraint ->max )
110+ );
108111 }
109112
110113 throw new \RuntimeException ('Unsupported Symfony Constraint: ' .get_class ($ validationConstraint ));
0 commit comments