@@ -18,7 +18,6 @@ class ValidationListener implements EventSubscriberInterface
1818{
1919 protected $ validator ;
2020
21- protected $ rules ;
2221 protected $ data ;
2322
2423 public function __construct (ValidationFactory $ validator )
@@ -32,37 +31,61 @@ public function __construct(ValidationFactory $validator)
3231 public static function getSubscribedEvents ()
3332 {
3433 return array (
34+ FormEvents::PRE_SUBMIT => 'gatherData ' ,
3535 FormEvents::POST_SUBMIT => 'validateRules ' ,
3636 );
3737 }
3838
39- public function validateRules (FormEvent $ event )
39+ /**
40+ * Get the original data, before submitting
41+ *
42+ * @param FormEvent $event
43+ */
44+ public function gatherData (FormEvent $ event )
4045 {
41- $ this ->rules = [] ;
42- $ this -> data = [];
46+ $ this ->data = $ event -> getData () ;
47+ }
4348
49+ /**
50+ * Find all rules, apply them to the original data and add errors.
51+ *
52+ * @param FormEvent $event
53+ */
54+ public function validateRules (FormEvent $ event )
55+ {
4456 if ($ event ->getForm ()->isRoot ()) {
57+
4558 $ root = $ event ->getForm ();
46- $ this ->parseChildren ($ root );
59+ $ rules = $ this ->findRules ($ root );
60+
61+ $ validator = $ this ->validator ->make ($ this ->data , $ rules );
4762
48- $ validator = $ this ->validator ->make ($ this ->data , $ this ->rules );
4963 if ($ validator ->fails ()) {
64+
65+ // Add all messages to the original name
5066 foreach ($ validator ->getMessageBag ()->toArray () as $ name => $ messages ) {
5167 foreach ($ messages as $ message ) {
5268 $ form = $ this ->getByDotted ($ root , $ name );
5369 $ form ->addError (new FormError ($ message ));
5470 }
5571 }
5672 }
73+
5774 }
5875 }
5976
60- protected function parseChildren (FormInterface $ parent , $ parentName = null )
77+ /**
78+ * Recursively find all rules.
79+ *
80+ * @param FormInterface $parent
81+ * @param array $rules
82+ * @return array
83+ */
84+ protected function findRules (FormInterface $ parent , $ rules = [])
6185 {
6286 foreach ($ parent ->all () as $ form ) {
6387 $ config = $ form ->getConfig ();
6488 $ name = $ form ->getName ();
65- $ this ->data [$ name ] = $ form ->getData ();
6689
6790 if ($ config ->hasOption ('rules ' ) ) {
6891
@@ -74,16 +97,26 @@ protected function parseChildren(FormInterface $parent, $parentName = null)
7497 $ name .= '.* ' ;
7598 }
7699
77- if ($ parentName ) {
78- $ name = $ parentName . '. ' . $ name ;
100+ if ( ! $ parent -> isRoot () ) {
101+ $ name = $ parent -> getName () . '. ' . $ name ;
79102 }
80- $ this ->rules [$ name ] = $ rule ;
103+
104+ $ rules [$ name ] = $ rule ;
81105 }
82106
83- $ this ->parseChildren ($ form , $ form -> getName () );
107+ $ rules = $ this ->findRules ($ form , $ rules );
84108 }
109+
110+ return $ rules ;
85111 }
86112
113+ /**
114+ * Recursively get the form using the dotted name.
115+ *
116+ * @param FormInterface $form
117+ * @param $name
118+ * @return FormInterface
119+ */
87120 protected function getByDotted (FormInterface $ form , $ name )
88121 {
89122 $ parts = explode ('. ' , $ name );
@@ -95,6 +128,13 @@ protected function getByDotted(FormInterface $form, $name)
95128 return $ form ;
96129 }
97130
131+ /**
132+ * Add default rules based on the type
133+ *
134+ * @param FormTypeInterface $type
135+ * @param array $rules
136+ * @return array
137+ */
98138 protected function addTypeRules (FormTypeInterface $ type , array $ rules )
99139 {
100140 if (
0 commit comments