Pez

Días que tiene el animal

Gramos de peso

El número de aletas que tiene


Ir al artículo

Controller Code - Github

    /**
     * @Route("/forms/virtual/fish/new", name="examples_virtual_forms_fish_create")
     * @Template("MainBundle:Example\VirtualForms:new_fish.html.twig")
     */
    public function fishNewAction(Request $request)
    {
        $fish = new Fish();

        $form = $this->createForm(new FishType(), $fish);

        if ($request->isMethod("POST")) {
            $form->bind($request);

            if ($form->isValid()) {

                $flashBag = $this->get('session')->getFlashBag();
                $flashBag->add('smtc_success', 'Se ha creado un pez:');
                $flashBag->add('smtc_success', sprintf('Nombre: %s', $fish->name));
                $flashBag->add('smtc_success', sprintf('Edad: %s', $fish->age));
                $flashBag->add('smtc_success', sprintf('Peso: %s', $fish->weight));
                $flashBag->add('smtc_success', sprintf('Número de aletas: %s', $fish->finsNumber));

                return $this->redirect($this->generateUrl('examples_virtual_forms_fish_create'));
            }
        }

        return array(
            'form' => $form->createView()
        );
    }

Template Code - Github

{% extends 'MainBundle::layout.html.twig' %}

{% import 'MainBundle:Macros:breadcrumb.html.twig' as breadcrumb %}

{% set section = 'examples' %}

{% block breadcrumb %}
{{ breadcrumb.show_example('Form Extensions') }}
{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-md-12">
            <h2>Pez</h2>
            <form action="{{ path('examples_virtual_forms_fish_create') }}" method="POST" novalidate>
                {{ form_widget(form) }}
                <div class="form-actions">
                    <button type="submit" class="btn btn-success">
                        Enviar
                    </button>
                </div>
            </form>
        </div>
    </div>
{% endblock %}


{% set article_link = 'http://showmethecode.es/php/symfony/symfony2-form-extensions/' %}

Fork me on GitHub