Browse Source

add create action

Thomas 14 years ago
parent
commit
82d3f53c48
2 changed files with 52 additions and 15 deletions
  1. 22 2
      Controller/CRUDController.php
  2. 30 13
      Resources/views/CRUD/base_edit.twig

+ 22 - 2
Controller/CRUDController.php

@@ -319,7 +319,9 @@ class CRUDController extends Controller
 
             $action = 'edit';
         } else {
-            $object = new $this->getClass();
+            $class = $this->getClass();
+            $object = new $class;
+
             $action = 'create';
         }
 
@@ -344,9 +346,27 @@ class CRUDController extends Controller
         ));
     }
 
-    public function createAction()
+    public function createAction($form = null)
     {
+        $this->get('session')->start();
+
+        $fields = $this->getFormFields();
 
+        if($form instanceof Form) {
+            $object = $form->getData();
+        } else {
+            $class = $this->getClass();
+            $object = new $class;
+
+            $form   = $this->getForm($object, $fields);
+        }
+
+        return $this->render($this->getEditTemplate(), array(
+            'form'   => $form,
+            'object' => $object,
+            'fields' => $fields,
+            'urls'   => $this->getUrls()
+        ));
     }
 
     public function setBaseControllerName($base_controller_name)

+ 30 - 13
Resources/views/CRUD/base_edit.twig

@@ -10,19 +10,36 @@ file that was distributed with this source code.
 #}
 
 
-<h1>View</h1>
-
-<form action="{{ url(urls.update.url, {'id': object.id}) }}" method="POST">
-
-    {{ form|render_hidden }}
-
-    {% for field in fields %}
-        {{ field|render_form_element(form, object, {'urls' : urls}) }}
-    {% endfor %}
-
+{% block title %}
     {% if object.id %}
-        <input type="submit" value="{% trans "btn_update" from "BaseApplicationBundle" %}"/>
+        <h1>Edit</h1>
     {% else %}
-        <input type="submit" value="{% trans "btn_create" from "BaseApplicationBundle" %}"/>
+        <h1>Create</h1>
     {% endif %}
-</form>
+{% endblock %}
+
+{% block actions %}
+    <div>
+        <ul>
+            <li><a href="{{ url(urls.create.url) }}">{% trans "link_action_create" from "BaseApplicationBundle" %}</a></li>
+            <li><a href="{{ url(urls.list.url) }}">{% trans "link_action_list" from "BaseApplicationBundle" %}</a></li>
+        </ul>
+    </div>
+{% endblock %}
+
+{% block form %}
+    <form action="{{ url(urls.update.url, {'id': object.id}) }}" method="POST">
+
+        {{ form|render_hidden }}
+
+        {% for field in fields %}
+            {{ field|render_form_element(form, object, {'urls' : urls}) }}
+        {% endfor %}
+
+        {% if object.id %}
+            <input type="submit" value="{% trans "btn_update" from "BaseApplicationBundle" %}"/>
+        {% else %}
+            <input type="submit" value="{% trans "btn_create" from "BaseApplicationBundle" %}"/>
+        {% endif %}
+    </form>
+{% endblock %}