1

here goes my doubt:

So I created the Form Class acording to the documentation: http://symfony.com/doc/current/book/forms.html#creating-form-classes

// src/AppBundle/Form/Type/TaskType.php
namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('task')
            ->add('dueDate', null, array('widget' => 'single_text'))
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'task';
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Task',
        ));
    }
}

But I cannot figure out WHERE to put the submit handler. In http://symfony.com/doc/current/book/forms.html#handling-form-submissions puts it in the controller, with everything else, and in (...#forms-and-doctrine) hints you what to do, but it doesn't say anything (or I couldn't find it) about where exactly and how to handle the submission when you are ussing a form class. A little help would be greatly appreciated.

Thank you in advance

2 Answers 2

2

Form Types are used so you don't have to keep creating the same form, or just to keep things separate.

Form actions are still handled in the controller.
Given your example form type class, something like;

public function taskAction(Request $request)
{
    // build the form ...
    $type = new Task();
    $form = $this->createForm(new TaskType(), $type);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // do whatever you want ...
        $data = $form->getData(); // to get submitted data

        // redirect, show twig, your choice
    }

    // render the template
}

Take a look at Symfony best practices for forms.

Sign up to request clarification or add additional context in comments.

Comments

0

If you need to give some post validation logic to your form, you can create a form handler which will also embedded the validation or listen to Doctrine events.

But that's just a tip for a more complicated usage ;)

Otherwise, Rooneyl's answer is what you're looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.