简体   繁体   中英

Symfony 4 : Error creating an add entity form (object not found by the @ParamConverter annotation)

I'm trying to make a form to add an entity in my database (a project). I realized exactly the same thing for other entities, everything works, but for this one (a project), it does not work ...

Screen of error : http://image.noelshack.com/fichiers/2019/02/1/1546884788-capture.png

ProjectController :

/**
 * @Route("/projects/add", name="add_projects")
 */
public function addProject(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $project = new Project();

    $form = $this->createForm(AddProjectType::class, $project);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em->persist($project);
        $em->flush();
        return $this->redirectToRoute('index_projects');
    }

    return $this->render('/project/add.html.twig', [
        'form' => $form->createView(),
    ]);
}

AddProjectType :

class AddProjectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, array(
            'attr' => array(
                'placeholder' => 'Nom',
            ),
            'label' => false,
        ))
        ->add('price', IntegerType::class, array(
            'attr' => array(
                'placeholder' => 'Prix',
            ),
            'label' => false,
        ))
        ->add('type', ChoiceType::class, array(
            'choices' => array(
                'Application web' => 'Application web',
                'Site internet' => 'Site internet',
                'Application mobile' => 'Application mobile',
                'Autre' => 'Autre',
            ),
            'label' => false,
        ))
        ->add('client', EntityType::class, array(
            'class' => User::class,
            'choice_label' => function($user) {
                return $user->getUsername();
            },
            'label' => false,
        ))
        ->add('state', ChoiceType::class, array(
            'choices' => array(
                'A faire' => 'A faire',
                'En cours' => 'En cours',
                'Terminé' => 'Terminé',
            ),
            'label' => false,
        ))
        ->add('description', TextareaType::class, array(
            'attr' => array(
                'placeholder' => 'Description',
            ),
            'label' => false,
        ))
        ->add('Ajouter', SubmitType::class, [
            'attr' => [
                'class' => 'btn btn-success',
            ]
        ])
    ;
}

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

add.html.twig :

{% extends "./base.html.twig" %}

{% block title %}{{ parent() }}Ajouter un projet{% endblock %}

{% block stylesheets %}
{{ parent() }}
<style>
    form input, form select, form textarea {
        width: 100%;
        margin: .5em 0;
    }
</style>
{%  endblock %}

{% block body %}
{{ parent() }}
<h1 class="title-page">Ajouter un projet</h1>
<div class="container-fluid">

    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
</div>
{% endblock %}

Did you look into the database whether the project is added? In your controller I don't see a reason for a ParamConverter, therefore I guess that the error happens after the redirect to index_projects

我只是设法找出为什么会出现此错误,因此将解决方案发布在这里,以便其他人使用:在此控制器中,我有一条路由/ projects / {id}(用于访问项目的详细信息),我通过/ projects / details / {id}对其进行了修改

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM