浏览代码

Merge pull request #2290 from pokap/try_catch_crud_controller

Try catch on create/update action
Thomas 10 年之前
父节点
当前提交
30ed001301
共有 1 个文件被更改,包括 33 次插入20 次删除
  1. 33 20
      Controller/CRUDController.php

+ 33 - 20
Controller/CRUDController.php

@@ -318,19 +318,26 @@ class CRUDController extends Controller
 
             // persist if the form was valid and if in preview mode the preview was approved
             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
-                $object = $this->admin->update($object);
 
-                if ($this->isXmlHttpRequest()) {
-                    return $this->renderJson(array(
-                        'result'    => 'ok',
-                        'objectId'  => $this->admin->getNormalizedIdentifier($object)
-                    ));
-                }
+                try {
+                    $object = $this->admin->update($object);
+
+                    if ($this->isXmlHttpRequest()) {
+                        return $this->renderJson(array(
+                            'result'    => 'ok',
+                            'objectId'  => $this->admin->getNormalizedIdentifier($object)
+                        ));
+                    }
 
-                $this->addFlash('sonata_flash_success', $this->admin->trans('flash_edit_success', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
+                    $this->addFlash('sonata_flash_success', $this->admin->trans('flash_edit_success', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
 
-                // redirect to edit mode
-                return $this->redirectTo($object);
+                    // redirect to edit mode
+                    return $this->redirectTo($object);
+
+                } catch (ModelManagerException $e) {
+
+                    $isFormValid = false;
+                }
             }
 
             // show an error message if the form failed validation
@@ -534,19 +541,25 @@ class CRUDController extends Controller
                     throw new AccessDeniedException();
                 }
 
-                $object = $this->admin->create($object);
+                try {
+                    $object = $this->admin->create($object);
 
-                if ($this->isXmlHttpRequest()) {
-                    return $this->renderJson(array(
-                        'result' => 'ok',
-                        'objectId' => $this->admin->getNormalizedIdentifier($object)
-                    ));
-                }
+                    if ($this->isXmlHttpRequest()) {
+                        return $this->renderJson(array(
+                            'result' => 'ok',
+                            'objectId' => $this->admin->getNormalizedIdentifier($object)
+                        ));
+                    }
+
+                    $this->addFlash('sonata_flash_success', $this->admin->trans('flash_create_success', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
 
-                $this->addFlash('sonata_flash_success', $this->admin->trans('flash_create_success', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
+                    // redirect to edit mode
+                    return $this->redirectTo($object);
 
-                // redirect to edit mode
-                return $this->redirectTo($object);
+                } catch (ModelManagerException $e) {
+
+                    $isFormValid = false;
+                }
             }
 
             // show an error message if the form failed validation