Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a simple form allowing the user to create a new thread

Updated the Entity to use Assert for validation Updated the controller
to create and handle create/update Updated the views to link to the
actions and render the forms
  • Loading branch information...
commit 9e3f35b8995fe8ceaf017ba1f590dec7133763be 1 parent bdcc695
Cameron Manderson cammanderson authored
43 Symfony/src/MelbSymfony2/ForumExampleBundle/Controller/ForumController.php
View
@@ -42,4 +42,47 @@ public function threadViewAction(Entity\Thread $thread)
return array('thread' => $thread);
}
+ /**
+ * @Route("/thread-create", name="thread_create")
+ * @Template("MelbSymfony2ForumExampleBundle:Forum:thread-edit.html.twig")
+ */
+ public function threadCreateAction() {
+ return $this->threadEdit();
+ }
+
+ /**
+ * @Route("/thread/{id}/edit", name="thread_edit")
+ * @Template("MelbSymfony2ForumExampleBundle:Forum:thread-edit.html.twig")
+ */
+ public function threadEditAction(Entity\Thread $thread = null)
+ {
+ return $this->threadEdit($thread);
+ }
+
+ private function threadEdit(Entity\Thread $thread = null)
+ {
+ if(empty($thread)) $thread = new Entity\Thread();
+ $form = $this->createFormBuilder($thread)
+ ->add('title', 'text')
+ ->add('body', 'textarea')
+ ->getForm();
+
+ $request = $this->get('request');
+ if ($request->getMethod() == 'POST') {
+ $form->bindRequest($request);
+
+ if ($form->isValid()) {
+ // perform some action, such as save the object to the database
+
+ $entityManager = $this->getEntityManager();
+ $entityManager->persist($thread);
+ $entityManager->flush();
+
+ return $this->redirect($this->generateUrl('forum_default'));
+ }
+ }
+
+ return array('form' => $form->createView());
+ }
+
}
13 Symfony/src/MelbSymfony2/ForumExampleBundle/Entity/Thread.php
View
@@ -7,6 +7,7 @@
namespace MelbSymfony2\ForumExampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Validator\Constraints as Assert;
/**
*
@@ -25,6 +26,7 @@ class Thread
/**
* @ORM\Column
+ * @Assert\NotBlank()
*/
private $title;
@@ -36,6 +38,7 @@ class Thread
/**
* @ORM\Column(type="text")
+ * @Assert\NotBlank()
*/
private $body;
@@ -97,16 +100,6 @@ public function getTitle()
return $this->title;
}
- public function setUser($user)
- {
- $this->user = $user;
- }
-
- public function getUser()
- {
- return $this->user;
- }
-
public function setPostCount($postCount)
{
$this->postCount = $postCount;
3  Symfony/src/MelbSymfony2/ForumExampleBundle/Resources/views/Forum/index.html.twig
View
@@ -4,17 +4,20 @@
{% block body %}
<h1>Threads</h1>
+ <a href="{{ path ('thread_create') }}">Create new</a>
<table>
<tr>
<th>Title<th>
<th>Date</th>
<th>Thread Count</th>
+ <th>Options</th>
</tr>
{% for thread in threads %}
<tr>
<td><a href="{{ path ('thread_view', {'id' : thread.id}) }}">{{ thread.title }}</a><td>
<td>{{ thread.createdAt|date("d/m/Y") }}</td>
<td>{{ thread.postCount }}</td>
+ <td><a href="{{ path ('thread_edit', {'id' : thread.id}) }}">edit</a></td>
</tr>
{% endfor %}
</table>
14 Symfony/src/MelbSymfony2/ForumExampleBundle/Resources/views/Forum/thread-edit.html.twig
View
@@ -0,0 +1,14 @@
+{% extends '::base.html.twig' %}
+
+{% block title %}New Thread{% endblock %}
+
+{% block body %}
+
+
+<h1>New Thread</h1>
+<form action="#" method="post" {{ form_enctype(form) }}>
+ {{ form_widget(form) }}
+
+ <input type="submit"/>
+</form>
+{% endblock %}
6 Symfony/src/MelbSymfony2/ForumExampleBundle/Resources/views/Forum/thread-view.html.twig
View
@@ -21,4 +21,10 @@
</tr>
{% endfor %}
</table>
+
+<div>
+ <a href="{{ path('forum_default') }}">Back</a>
+</div>
+
+
{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.