Просмотр исходного кода

Get data from the form in create/update actions. (#4495)

The form might create a brand new (immutable) object with a DataMapper
Oleksandr Zanichkovskyi 8 лет назад
Родитель
Сommit
9655c1eb5f
2 измененных файлов с 65 добавлено и 28 удалено
  1. 33 28
      Controller/CRUDController.php
  2. 32 0
      Tests/Controller/CRUDControllerTest.php

+ 33 - 28
Controller/CRUDController.php

@@ -236,43 +236,46 @@ class CRUDController extends Controller
         $templateKey = 'edit';
 
         $id = $request->get($this->admin->getIdParameter());
-        $object = $this->admin->getObject($id);
+        $existingObject = $this->admin->getObject($id);
 
-        if (!$object) {
+        if (!$existingObject) {
             throw $this->createNotFoundException(sprintf('unable to find the object with id : %s', $id));
         }
 
-        $this->admin->checkAccess('edit', $object);
+        $this->admin->checkAccess('edit', $existingObject);
 
-        $preResponse = $this->preEdit($request, $object);
+        $preResponse = $this->preEdit($request, $existingObject);
         if ($preResponse !== null) {
             return $preResponse;
         }
 
-        $this->admin->setSubject($object);
+        $this->admin->setSubject($existingObject);
 
         /** @var $form Form */
         $form = $this->admin->getForm();
-        $form->setData($object);
+        $form->setData($existingObject);
         $form->handleRequest($request);
 
         if ($form->isSubmitted()) {
             //TODO: remove this check for 4.0
             if (method_exists($this->admin, 'preValidate')) {
-                $this->admin->preValidate($object);
+                $this->admin->preValidate($existingObject);
             }
             $isFormValid = $form->isValid();
 
             // persist if the form was valid and if in preview mode the preview was approved
             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
+                $submittedObject = $form->getData();
+                $this->admin->setSubject($submittedObject);
+
                 try {
-                    $object = $this->admin->update($object);
+                    $existingObject = $this->admin->update($submittedObject);
 
                     if ($this->isXmlHttpRequest()) {
                         return $this->renderJson(array(
                             'result' => 'ok',
-                            'objectId' => $this->admin->getNormalizedIdentifier($object),
-                            'objectName' => $this->escapeHtml($this->admin->toString($object)),
+                            'objectId' => $this->admin->getNormalizedIdentifier($existingObject),
+                            'objectName' => $this->escapeHtml($this->admin->toString($existingObject)),
                         ), 200, array());
                     }
 
@@ -280,21 +283,21 @@ class CRUDController extends Controller
                         'sonata_flash_success',
                         $this->trans(
                             'flash_edit_success',
-                            array('%name%' => $this->escapeHtml($this->admin->toString($object))),
+                            array('%name%' => $this->escapeHtml($this->admin->toString($existingObject))),
                             'SonataAdminBundle'
                         )
                     );
 
                     // redirect to edit mode
-                    return $this->redirectTo($object);
+                    return $this->redirectTo($existingObject);
                 } catch (ModelManagerException $e) {
                     $this->handleModelManagerException($e);
 
                     $isFormValid = false;
                 } catch (LockException $e) {
                     $this->addFlash('sonata_flash_error', $this->trans('flash_lock_error', array(
-                        '%name%' => $this->escapeHtml($this->admin->toString($object)),
-                        '%link_start%' => '<a href="'.$this->admin->generateObjectUrl('edit', $object).'">',
+                        '%name%' => $this->escapeHtml($this->admin->toString($existingObject)),
+                        '%link_start%' => '<a href="'.$this->admin->generateObjectUrl('edit', $existingObject).'">',
                         '%link_end%' => '</a>',
                     ), 'SonataAdminBundle'));
                 }
@@ -307,7 +310,7 @@ class CRUDController extends Controller
                         'sonata_flash_error',
                         $this->trans(
                             'flash_edit_error',
-                            array('%name%' => $this->escapeHtml($this->admin->toString($object))),
+                            array('%name%' => $this->escapeHtml($this->admin->toString($existingObject))),
                             'SonataAdminBundle'
                         )
                     );
@@ -326,7 +329,7 @@ class CRUDController extends Controller
         return $this->render($this->admin->getTemplate($templateKey), array(
             'action' => 'edit',
             'form' => $formView,
-            'object' => $object,
+            'object' => $existingObject,
         ), null);
     }
 
@@ -486,38 +489,40 @@ class CRUDController extends Controller
             );
         }
 
-        $object = $this->admin->getNewInstance();
+        $newObject = $this->admin->getNewInstance();
 
-        $preResponse = $this->preCreate($request, $object);
+        $preResponse = $this->preCreate($request, $newObject);
         if ($preResponse !== null) {
             return $preResponse;
         }
 
-        $this->admin->setSubject($object);
+        $this->admin->setSubject($newObject);
 
         /** @var $form \Symfony\Component\Form\Form */
         $form = $this->admin->getForm();
-        $form->setData($object);
+        $form->setData($newObject);
         $form->handleRequest($request);
 
         if ($form->isSubmitted()) {
             //TODO: remove this check for 4.0
             if (method_exists($this->admin, 'preValidate')) {
-                $this->admin->preValidate($object);
+                $this->admin->preValidate($newObject);
             }
             $isFormValid = $form->isValid();
 
             // persist if the form was valid and if in preview mode the preview was approved
             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
-                $this->admin->checkAccess('create', $object);
+                $submittedObject = $form->getData();
+                $this->admin->setSubject($submittedObject);
+                $this->admin->checkAccess('create', $submittedObject);
 
                 try {
-                    $object = $this->admin->create($object);
+                    $newObject = $this->admin->create($submittedObject);
 
                     if ($this->isXmlHttpRequest()) {
                         return $this->renderJson(array(
                             'result' => 'ok',
-                            'objectId' => $this->admin->getNormalizedIdentifier($object),
+                            'objectId' => $this->admin->getNormalizedIdentifier($newObject),
                         ), 200, array());
                     }
 
@@ -525,13 +530,13 @@ class CRUDController extends Controller
                         'sonata_flash_success',
                         $this->trans(
                             'flash_create_success',
-                            array('%name%' => $this->escapeHtml($this->admin->toString($object))),
+                            array('%name%' => $this->escapeHtml($this->admin->toString($newObject))),
                             'SonataAdminBundle'
                         )
                     );
 
                     // redirect to edit mode
-                    return $this->redirectTo($object);
+                    return $this->redirectTo($newObject);
                 } catch (ModelManagerException $e) {
                     $this->handleModelManagerException($e);
 
@@ -546,7 +551,7 @@ class CRUDController extends Controller
                         'sonata_flash_error',
                         $this->trans(
                             'flash_create_error',
-                            array('%name%' => $this->escapeHtml($this->admin->toString($object))),
+                            array('%name%' => $this->escapeHtml($this->admin->toString($newObject))),
                             'SonataAdminBundle'
                         )
                     );
@@ -565,7 +570,7 @@ class CRUDController extends Controller
         return $this->render($this->admin->getTemplate($templateKey), array(
             'action' => 'create',
             'form' => $formView,
-            'object' => $object,
+            'object' => $newObject,
         ), null);
     }
 

+ 32 - 0
Tests/Controller/CRUDControllerTest.php

@@ -1512,6 +1512,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->disableOriginalConstructor()
             ->getMock();
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->admin->expects($this->once())
             ->method('getForm')
             ->will($this->returnValue($form));
@@ -1634,6 +1638,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isValid')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->admin->expects($this->once())
             ->method('getNormalizedIdentifier')
             ->with($this->equalTo($object))
@@ -1737,6 +1745,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isValid')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->admin->expects($this->once())
             ->method('toString')
             ->with($this->equalTo($object))
@@ -1852,6 +1864,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isValid')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->admin->expects($this->any())
             ->method('getForm')
             ->will($this->returnValue($form));
@@ -2031,6 +2047,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isValid')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->admin->expects($this->once())
             ->method('toString')
             ->with($this->equalTo($object))
@@ -2086,6 +2106,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isSubmitted')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $form->expects($this->once())
             ->method('isValid')
             ->will($this->returnValue(true));
@@ -2203,6 +2227,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isSubmitted')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->request->setMethod('POST');
 
         $formView = $this->createMock('Symfony\Component\Form\FormView');
@@ -2269,6 +2297,10 @@ class CRUDControllerTest extends PHPUnit_Framework_TestCase
             ->method('isValid')
             ->will($this->returnValue(true));
 
+        $form->expects($this->once())
+            ->method('getData')
+            ->will($this->returnValue($object));
+
         $this->admin->expects($this->any())
             ->method('getClass')
             ->will($this->returnValue('stdClass'));