Reload or clear data in existing form in Symfony2.1 - symfony

We upgraded from Symfony 2.0 to 2.1. With 2.0, I used to modify the entity and reload the form like this:
$form->setData($entity);
But this is not allowed anymore with Symfony 2.1 (https://github.com/symfony/symfony/pull/3322). I get the following error:
You cannot change the data of a bound form
Is there a way to rebind the form to the entity/reload the data?

I did something that did the trick… Don't know if it's best way but…
public function contactAction(Request $request){
$task = new myBundle();
$form = $this->createFormBuilder($task)
->add('email', 'text', array('label'=>'E-mail'))
->add('message', 'textarea')
->add('newsletter', 'checkbox', array('label'=>'blabla','required'=>false))
->getForm();
$cloned = clone $form;
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
[... code ...]
$form = $cloned;
}
}
return $this->render(
'myBundle:Default:contact.html.twig',
array('form' => $form->createView())
);
}
By cloning the just instanciated form object, I can switch the «full» one by the empty one, and keep all the params.

And the most obvious way to reset the form after a successful post. Set a "flash", redirect to the form page and display the flash:
public function contactAction(Request $request)
{
$contact = new Contact();
$form = $this->createForm(new ContactType(), $contact);
$form->handleRequest($request);
if ($form->isValid()) {
//...
$session = $this->container->get('session');
$session->getFlashBag()->set('success', 'Your message has been sent.');
return $this->redirect($this->get('router')->generate('contact'));
}
return array(
'form' => $form->createView(),
);
}
And in your twig:
{% if app.session.flashBag.has('success') %}
{{ app.session.flashBag.get('success')[0] }}
{% endif %}

Well, you could create a new instance of the form and re-bind. Seems like overkill, but it would work in a pinch.

Related

Symfony 5 : handling multiple forms in one controller

I am trying to manage multiple forms in the same page in Symfony 5 with the following function, but it seems that every time I try to submit a form, only the first form of the list is handled even if it is not the one that has been submitted:
class ContentController extends AbstractController
{
/**
* #Route("/category/edition/{content}", name="edit_category")
*/
public function edition(Request $request, Category $content): Response
{
$forms = [
"edit_category" => $this->createForm(EditCategoryType::class, $content),
"create_post" => $this->createForm(CreatePostType::class)
];
foreach($forms as $form) {
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
// Always prints edit_category
// even when that is the the create_post that is submitted
return var_dump($form->getName());
}
}
return $this->render(
'content/edition.html.twig',
[
'forms' => \array_map(
function($form) {
return $form->createView();
},
$forms
),
'content' => $content,
]
);
}
}
I have seen in other posts that the name of the forms can sometime raise an issue, but I have checked that the forms do have different names, and I have also tried to call handleRequest() on every form in a separate foreach loop because I have seen this done in some posts, but it (quite expectedly I must say) didn't change the behavior.
And I didn't seem to find any unanimous best practice tips about how to handle multiple forms in the same Controller in Symfony, so I was wondering what is the best way to do it, or if it would be cleaner to define a separate action route for each form in order to avoid this problem altogether.
If needed, the content/edition.html.twig file looks something like that:
{% set edit_category_form = forms['edit_category'] %}
{% set create_post_form = forms['create_post'] %}
{{ form_start(edit_category_form) }}
{{ form_errors(edit_category_form) }}
{{ form_end(edit_category_form) }}
{{ form_start(create_post_form) }}
{{ form_errors(create_post_form) }}
{{ form_end(create_post_form) }}
(Category is a classical Symfony entity, EditCategoryType is a form associated with the Category entity and CreatePostType is a form associated with another Symfony entity)
After some research, it seems for some reason like it works if (and only if?) the form is build just before handling the request:
class ContentController extends AbstractController
{
/**
* #Route("/category/edition/{content}", name="edit_category")
*/
public function edition(Request $request, Category $content): Response
{
$self = $this;
$formBuilders = [
"edit_category" => function() use ($self, $content) {
return $self->createForm(EditCategoryType::class, $content);
},
"create_post" => function() use ($self, $content) {
return $self->createForm(CreatePostType::class);
},
];
$forms = [];
foreach($formBuilders as $key => $formBuilder) {
$form = $formBuilder();
$forms[$key] = $form;
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
// Does print the name of the right form
return var_dump($form->getName());
}
}
return $this->render(
'content/edition.html.twig',
[
'forms' => \array_map(
function($form) {
return $form->createView();
},
$forms
),
'content' => $content,
]
);
}
}
It works but it doesn't feel like a proper way to handle this !

How to pass correctly object using redirectToRoute?

This is my simple code
class LuckyController extends Controller
{
public function taskFormAction(Request $request)
{
$task = new Task();
//$task->setTask('Test task');
//$task->setDueDate(new \DateTime('tomorrow noon'));
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Save'))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$task = $form->getData();
return $this->redirectToRoute('task_ok', array('task' => '123'));
}
return $this->render('pre.html.twig', array('pre' => print_r($task, true), 'form' => $form->createView()));
}
public function taskOKAction(Task $task)
{
return $this->render('ok.html.twig', array('msg' => 'ok', 'task' => print_r($task, true)));
}
}
and this line
return $this->redirectToRoute('task_ok', array('task' => '123'));
makes redirection to taskOKAction, but it lets me just send parameters by URL (?task=123).
I need to send object $task to taskOKAction to print on screen what user typed in form.
How can I do that? I've already red on stackoverflow before asking that the good solution is to store data from form (e.g. in database or file) and just pass in parameter in URL the ID of object. I think it's quite good solution but it adds me responsibility to check if user didn't change ID in URL to show other object.
What is the best way to do that?
Best regards,
L.
Use the parameter converter and annotate the route properly.
Something like (if you use annotation for routes)
/**
* #Route("/task/ok/{id}")
* #ParamConverter("task", class="VendorBundle:Task")
*/
public function taskOKAction(Task $task)
You can also omit the #ParamConverter part if parameter is only one and if is type hinted (as in your case)
From your form action you can do it without actual 302 redirect:
public function taskFormAction(Request $request)
{
$task = new Task();
//$task->setTask('Test task');
//$task->setDueDate(new \DateTime('tomorrow noon'));
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Save'))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$task = $form->getData();
return $this-taskOKAction($task)
}
}
This is perfectly legal, and your frontend colleague will thank you for lack of the redirects.

persist in database is not working

i'm trying to add my data into my database , i was trying to not use a formbuilder, inside that i put all my form into the controller,
but when i submit the button i did't got an error but i can't find my data in the database.
here is my code any one have an idea please.
public function AjoutAction()
{
$classe=new Classes();
$formBuilder = $this->get('form.factory')->createBuilder('form', $classe);
$formBuilder
->add('NomClasse', 'text')
->add('save', 'submit')
;
$form = $formBuilder->getForm();
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($classe);
$em->flush();
} return $this->render('MyAppSchoolBundle:Classe:ajout.html.twig',array(
'form' => $form->createView(),
));
}
my twig file is here :
<h3>Formulaire d'annonce</h3>
{{ form(form) }}
thank you for your help
You need to change it to something like this:
public function AjoutAction(Request $request)
{
$classe=new Classes();
$formBuilder = $this->get('form.factory')->createBuilder('form', $classe);
$formBuilder
->add('NomClasse', 'text')
->add('save', 'submit')
;
$form = $formBuilder->getForm();
if ($form->handleRequest($request)->isValid()) {
$objToPersist = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($objToPersist);
$em->flush();
}
return $this->render('MyAppSchoolBundle:Classe:ajout.html.twig',array(
'form' => $form->createView(),
));
}

Prevent duplicates in the database in a many-to-many relationship

I'm working on a back office of a restaurant's website. When I add a dish, I can add ingredients in two ways.
In my form template, I manually added a text input field. I applied on this field the autocomplete method of jQuery UI that allows:
Select existing ingredients (previously added)
Add new ingredients
However, when I submit the form, each ingredients are inserted in the database (normal behaviour you will tell me ).
For the ingredients that do not exist it is good, but I don't want to insert again the ingredients already inserted.
Then I thought about Doctrine events, like prePersist(). But I don't see how to proceed. I would like to know if you have any idea of ​​the way to do it.
Here is the buildForm method of my DishType:
<?php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', 'entity', array('class' => 'PrototypeAdminBundle:DishCategory',
'property' => 'name',
'multiple' => false ))
->add('title', 'text')
->add('description', 'textarea')
->add('price', 'text')
->add('ingredients', 'collection', array('type' => new IngredientType(),
'allow_add' => true,
'allow_delete' => true,
))
->add('image', new ImageType(), array( 'label' => false ) );
}
and the method in my controller where I handle the form :
<?php
public function addDishAction()
{
$dish = new Dish();
$form = $this->createForm(new DishType, $dish);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($dish);
$em->flush();
return $this->redirect($this->generateUrl('prototype_admin_get_dish', array('slug' => $dish->getSlug())));
}
}
return $this->render('PrototypeAdminBundle:Admin:addDish.html.twig', array(
'form' => $form->createView(),
));
}
I was having the same problem. My entities were projects (dishes in your case) and tags (ingredients).
I solved it by adding an event listener, as explained here.
services:
my.doctrine.listener:
class: Acme\AdminBundle\EventListener\UniqueIngredient
tags:
- { name: doctrine.event_listener, event: preUpdate }
- { name: doctrine.event_listener, event: prePersist }
The listener triggers both prePersist (for newly added dishes) and preUpdate for updates on existing dishes.
The code checks if the ingredient already exists. If the ingredient exists it is used and the new entry is discarded.
The code follows:
<?php
namespace Acme\AdminBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Acme\AdminBundle\Entity\Dish;
use Acme\AdminBundle\Entity\Ingredient;
class UniqueIngredient
{
/**
* This will be called on newly created entities
*/
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
// we're interested in Dishes only
if ($entity instanceof Dish) {
$entityManager = $args->getEntityManager();
$ingredients = $entity->getIngredients();
foreach($ingredients as $key => $ingredient){
// let's check for existance of this ingredient
$results = $entityManager->getRepository('Acme\AdminBundle\Entity\Ingredient')->findBy(array('name' => $ingredient->getName()), array('id' => 'ASC') );
// if ingredient exists use the existing ingredient
if (count($results) > 0){
$ingredients[$key] = $results[0];
}
}
}
}
/**
* Called on updates of existent entities
*
* New ingredients were already created and persisted (although not flushed)
* so we decide now wether to add them to Dishes or delete the duplicated ones
*/
public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
// we're interested in Dishes only
if ($entity instanceof Dish) {
$entityManager = $args->getEntityManager();
$ingredients = $entity->getIngredients();
foreach($ingredients as $ingredient){
// let's check for existance of this ingredient
// find by name and sort by id keep the older ingredient first
$results = $entityManager->getRepository('Acme\AdminBundle\Entity\Ingredient')->findBy(array('name' => $ingredient->getName()), array('id' => 'ASC') );
// if ingredient exists at least two rows will be returned
// keep the first and discard the second
if (count($results) > 1){
$knownIngredient = $results[0];
$entity->addIngredient($knownIngredient);
// remove the duplicated ingredient
$duplicatedIngredient = $results[1];
$entityManager->remove($duplicatedIngredient);
}else{
// ingredient doesn't exist yet, add relation
$entity->addIngredient($ingredient);
}
}
}
}
}
NOTE: This seems to be working but I am not a Symfony / Doctrine expert so test your code carefully
Hope this helps!
pcruz
Since this post is 2 years old I don't know if help is still needed here, anyway..
You should place an addIngredient function within your Dish entity, this function checks if the Ingredient object exists within the current ingredient collection.
addIngredient(Ingredient $ingredient){
if (!$this->ingredients->contains($ingredient)) {
$this->ingredients[] = $ingredient;
}
}
Hopefully it could still help you out.

Symfony2 get item type in forms

I have a form, and need the field type in the bindrequest, the method getType dont works fine:
$peticion = $this->getRequest();
if ($peticion->getMethod() == 'POST')
{
$form->bindRequest($peticion);
if ($form->isValid()) {
foreach($form as $key => $per)
$per->getType(); // i want the type of item [text,checkbox,etc] the method getType() dont work
}}
Use this:
foreach($form as $key => $per) {
$per->getConfig()->getType()->getName();
}

Resources