Forráskód Böngészése

Fix cascading validation with sf2.1

Thomas Rabaix 12 éve
szülő
commit
5407571f9e
1 módosított fájl, 35 hozzáadás és 23 törlés
  1. 35 23
      Admin/Admin.php

+ 35 - 23
Admin/Admin.php

@@ -1158,29 +1158,6 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     public function getFormBuilder()
     {
-        $admin = $this;
-
-        // add the custom inline validation option
-        $metadata = $this->validator->getMetadataFactory()->getClassMetadata($this->getClass());
-        $metadata->addConstraint(new InlineConstraint(array(
-            'service' => $this,
-            'method'  => function(ErrorElement $errorElement, $object) use ($admin) {
-                /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
-
-                // This avoid the main validation to be cascaded to children
-                // The problem occurs when a model Page has a collection of Page as property
-                if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
-                    return;
-                }
-
-                $admin->validate($errorElement, $object);
-
-                foreach ($admin->getExtensions() as $extension) {
-                    $extension->validate($admin, $errorElement, $object);
-                }
-            }
-        )));
-
         $this->formOptions['data_class'] = $this->getActiveSubClass() ?: $this->getClass();
 
         $formBuilder = $this->getFormContractor()->getFormBuilder(
@@ -1194,6 +1171,9 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
+     * This method is being called by the main admin class and the child class,
+     * the getFormBuilder is only call by the main admin class
+     *
      * @param \Symfony\Component\Form\FormBuilder $formBuilder
      *
      * @return void
@@ -1207,6 +1187,38 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         foreach ($this->getExtensions() as $extension) {
             $extension->configureFormFields($mapper);
         }
+
+        $this->attachInlineValidator();
+    }
+
+    /**
+     * Attach the inline validator to the model metadata, this must be done once per admin
+     */
+    protected function attachInlineValidator()
+    {
+        $admin = $this;
+
+        // add the custom inline validation option
+        $metadata = $this->validator->getMetadataFactory()->getClassMetadata($this->getClass());
+
+        $metadata->addConstraint(new InlineConstraint(array(
+            'service' => $this,
+            'method'  => function(ErrorElement $errorElement, $object) use ($admin) {
+                /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
+
+                // This avoid the main validation to be cascaded to children
+                // The problem occurs when a model Page has a collection of Page as property
+                if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
+                    return;
+                }
+
+                $admin->validate($errorElement, $object);
+
+                foreach ($admin->getExtensions() as $extension) {
+                    $extension->validate($admin, $errorElement, $object);
+                }
+            }
+        )));
     }
 
     /**