Hozii
Hozii

Reputation: 23

Symfony Submit button doesn't work IsSubmitted never verified

I'm learning symfony 4 and I'm testing to see how to update a database by taking data from a form. It seems like isSubmitted's never verified.

Here is my Controller

/**
 * @Route("scheduler/new", name="scheduler_create")
 * @Route("scheduler/{id}/edit", name="scheduler_edit")
 */
public function form(Task $task = null, Request $request, EntityManagerInterface $manager){

    if(!$task) {
        $task = new Task();
    }

    $form = $this->createForm(TaskType::class, $task);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){

        $manager->persist($task);
        $manager->flush();

        return $this->redirectToRoute('scheduler_show', ['id' => $task-> getId()]);
    }

    return $this->render('scheduler/create.html.twig', [
        'formTask' => $form->createView(),
        'editMode'=> $task->getId() !== null
    ]);
}

My view :

{% extends 'base.html.twig' %}

{% form_theme formTask 'bootstrap_4_layout.html.twig' %}

{% block body %}

    <h1> Création d'une tâche </h1>

    {{ form_start(formTask) }}

    <p> 
        Inscrire ici le nom souhaité pour la tâche : {{form(formTask.title, {'attr' : {'placeholder' : "Titre de la tâche"} }) }}
        Veuillez d'écrire l'objet de la tâche : {{form(formTask.content, {'attr' : {'placeholder' : "Description de la tâche"} }) }}
        Séléctionner le groupe destinataire de la tâche : {{form(formTask.groupe) }} 
    </p>
    <p> A quelle date et souhaitez vous executer la tâche ? {{form(formTask.launch_date) }} </p>
    {{form(formTask.activer) }}

    <button type="submit" class="btn btn-success">
        {% if editMode %}
            Sauvegarder les modifications
        {% else %}
            Sauvegarder la tâche
        {% endif %}
    </button>


    {{ form_end(formTask) }}

{% endblock %}

I have been working with symfony for 2 weeks now so not that long. I'm following an online tutorial, and it's working until now I can't see any error message and don't know how can i solve that.

Here is my FormType :

<?php

namespace App\Form;

use App\Entity\Task;
use App\Entity\Groupe;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('launch_date', DateTimeType::class)
            ->add('content')
            ->add('activer')
            ->add('groupe', EntityType::class, [
                'class' => Groupe::class,
                'choice_label' => 'title'
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Task::class,
        ]);
    }
}

Upvotes: 1

Views: 1173

Answers (2)

Ghassen Hnid
Ghassen Hnid

Reputation: 1

i had the same problem : i try to submit using the button, but it doesn't work ! i forgot to put a {{ form_start(my_form) }} ..... {{ form_end(my_form) }}

Upvotes: 0

Elvandar
Elvandar

Reputation: 69

for such a button to work it must be inside the {{ form_start(your_form) }} {{ form_end(your_form) }} twig tags

otherwise it will not count as a form submit button

you can go over this by using $builder->add('submit', SubmitType::class);

Edit after discussion:

To custom the rendering of form rows, form_row(form.children) should be used instead of form(form.children) as this is going to display every informations such as form errors.

for a more precise handling you can refer to the symfony documentation

Upvotes: 1

Related Questions