Skip to content

Commit

Permalink
feature #236 Add an example about how to use the form's buttons (voro…
Browse files Browse the repository at this point in the history
…nkovich)

This PR was squashed before being merged into the master branch (closes #236).

Discussion
----------

Add an example about how to use the form's buttons

Commits
-------

cdcd412 Add an example about how to use the form's buttons
  • Loading branch information
javiereguiluz committed Nov 1, 2015
2 parents 36a93fd + cdcd412 commit e2d723c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/Resources/translations/messages.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
<source>label.create_post</source>
<target>Create post</target>
</trans-unit>
<trans-unit id="label.save_and_create_new">
<source>label.save_and_create_new</source>
<target>Save and create new</target>
</trans-unit>
<trans-unit id="action.back_to_list">
<source>action.back_to_list</source>
<target>Back to the post list</target>
Expand Down
2 changes: 0 additions & 2 deletions app/Resources/views/admin/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
{% block main %}
<h1>{{ 'title.post_list'|trans }}</h1>

{{ include('default/_flash_messages.html.twig') }}

<table class="table table-striped">
<thead>
<tr>
Expand Down
18 changes: 15 additions & 3 deletions app/Resources/views/admin/blog/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
{% block main %}
<h1>{{ 'title.post_new'|trans }}</h1>

{{ include('admin/blog/_form.html.twig', {
include_back_to_home_link: true,
}) }}
{{ form_start(form) }}
{{ form_row(form.title) }}
{{ form_row(form.summary) }}
{{ form_row(form.content) }}
{{ form_row(form.authorEmail) }}
{{ form_row(form.publishedAt) }}

<input type="submit" value="{{ 'label.create_post'|trans }}" class="btn btn-primary" />

{{ form_widget(form.saveAndCreateNew, { label: 'label.save_and_create_new'|trans, attr: { class: 'btn btn-primary' } }) }}

<a href="{{ path('admin_post_index') }}" class="btn btn-link">
{{ 'action.back_to_list'|trans }}
</a>
{{ form_end(form) }}
{% endblock %}

{% block sidebar %}
Expand Down
2 changes: 2 additions & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
{% block body %}
<div class="row">
<div id="main" class="col-sm-9">
{{ include('default/_flash_messages.html.twig') }}

{% block main %}{% endblock %}
</div>

Expand Down
9 changes: 8 additions & 1 deletion src/AppBundle/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function newAction(Request $request)
{
$post = new Post();
$post->setAuthorEmail($this->getUser()->getEmail());
$form = $this->createForm(new PostType(), $post);

// See http://symfony.com/doc/current/book/forms.html#submitting-forms-with-multiple-buttons
$form = $this->createForm(new PostType(), $post)
->add('saveAndCreateNew', 'submit');

$form->handleRequest($request);

Expand All @@ -93,6 +96,10 @@ public function newAction(Request $request)
// See http://symfony.com/doc/current/book/controller.html#flash-messages
$this->addFlash('success', 'post.created_successfully');

if ($form->get('saveAndCreateNew')->isClicked()) {
return $this->redirectToRoute('admin_post_new');
}

return $this->redirectToRoute('admin_post_index');
}

Expand Down

0 comments on commit e2d723c

Please sign in to comment.