Symfony2 - Dynamic generated form not working while editing form - symfony

Based on documentation: http://symfony.com/doc/2.8/form/dynamic_form_modification.html#form-events-submitted-data
I prepared dynamic generated form. And everything works properly but only when I use form for adding new data (/new) when I use the same form for editing existing data - not working
Simple form for "Appointment". It should work like that: User select client and then second "select" is filling proper data - depends on each client from first select. And this works ok but only when I try add new Appointment. When I try edit no.
class AppointmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('client', EntityType::class, array(
'class' => 'SystemAdminBundle:Client',
'placeholder' => '',
));
$formModifier = function(\Symfony\Component\Form\FormInterface $form, Client $client)
{
$diseases = array();
if($client !== null) {
$diseases = $client->getDiseases();
}
$form->add('disease', EntityType::class, array(
'class' => 'SystemAdminBundle:Disease',
'placeholder' => '',
'choices' => $diseases,
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$formModifier($event->getForm(), $data->getClient());
}
);
$builder->get('client')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$client = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $client);
}
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'System\AdminBundle\Entity\Appointment'
));
}
}
Appointment controller - here is function for add new appointment and edit. For "new" my code works, for "edit" no.
public function newAction(Request $request)
{
$appointment = new Appointment();
$form = $this->createForm(AppointmentType::class, $appointment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->get('appointment');
if(array_key_exists('name', $data)) {
$em = $this->getDoctrine()->getManager();
$em->persist($appointment);
$em->flush();
return $this->redirectToRoute('appointment_show', array('id' => $appointment->getId()));
}
}
return $this->render('appointment/new.html.twig', array(
'appointment' => $appointment,
'form' => $form->createView(),
));
}
public function editAction(Request $request, Appointment $appointment)
{
$deleteForm = $this->createDeleteForm($appointment);
$appointment = new Appointment();
$editForm = $this->createForm('System\AdminBundle\Form\AppointmentType', $appointment);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$data = $request->request->get('appointment');
if(array_key_exists('name', $data)) {
$em = $this->getDoctrine()->getManager();
$em->persist($appointment);
$em->flush();
return $this->redirectToRoute('appointment_show', array('id' => $appointment->getId()));
}
return $this->redirectToRoute('appointment_edit', array('id' => $appointment->getId()));
}
return $this->render('appointment/edit.html.twig', array(
'appointment' => $appointment,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
View for "new" appointment
{% block content %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
window.onload = function() {
var $sport = $('#appointment_client');
$sport.change(function() {
var $form = $(this).closest('form');
var data = {};
data[$sport.attr('name')] = $sport.val();
data['appointment[_token]'] = $('#appointment__token').val();
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
$('#appointment_disease').replaceWith(
$(html).find('#appointment_disease')
);
}
});
});
};
{% endblock %}
View for "edit" appointment - it's almost the same as for "new" appointment
{% block content %}
{{ form_start(edit_form) }}
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
window.onload = function() {
var $sport = $('#appointment_client');
$sport.change(function() {
var $form = $(this).closest('form');
var data = {};
data[$sport.attr('name')] = $sport.val();
data['appointment[_token]'] = $('#appointment__token').val();
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
$('#appointment_disease').replaceWith(
$(html).find('#appointment_disease')
);
}
});
});
};
{% endblock %}

You create a new Appointment in your editAction and then persist it. You should take the one that's in your function parameters, handle the request and just flush, since your object is already persisted.
So remove these lines :
$appointment = new Appointment();
// ...
$em->persist($appointment);

Related

Symfony 5.1 Dynamic Generation for Submitted Forms - No display

I'm embarking on this new post because despite a lot of research I'm stuck.
I have three entities.
Employee -> ManyToOne -> RefSociety
Employee -> ManyToOne -> RefSite
RefSociety -> OnToMany -> RefSite
RefSociety -> OnToMany -> Employee
RefSite -> OnToMany -> Employee
RefSite -> ManyToOne -> RefSociety
I am using the symfony documentation from FormEvent ; https://symfony.com/doc/current/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
I want that in my Employee form, when I select a society, then, it only offers me the sites attached to this society.
Here is what I have already done. I have tried a lot of different ways. When I manually pass a SocietyId, then it recognizes it and shows me the desired sites.
Also I don't think it's the JavaScript side that is blocking. Do you have an idea ?
EmployeeType.php
class EmployeeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('societe', EntityType::class, [
'class' => RefSociety::class,
'placeholder' => 'Sélectionnez votre société',
'mapped' => false,
])
;
$formModifier = function (FormInterface $form, ?RefSociety $society)
{
$sites = null === $society ? [] : $society->getSites();
$form->add('site', EntityType::class, [
'class' => RefSite::class,
'placeholder' => 'Sélectionnez votre site',
'choices' => $sites,
]);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier)
{
$data = $event->getData();
$formModifier($event->getForm(), $data->getSociete());
}
);
$builder->get('societe')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier)
{
$society = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $society);
}
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Employee::class,
]);
}
}
index.html.twig
{{ form_start(employeeForm) }}
{{form_row(employeeForm.societe) }}
{{form_row(employeeForm.site) }}
{{ form_end(employeeForm) }}
<script>
var $society = $('#employee_societe');
$society.change(function () {
var $employeeForm = $(this).closest('employeeForm');
var data = {};
data[$society.attr('name')] = $society.val();
//console.log(data)
$.ajax({
url: $employeeForm.attr('action'),
type: $employeeForm.attr('method'),
data: data,
success: function (html) {
//console.log(data)
$('#employee_site').replaceWith(
$(html).find('#employee_site')
);
}
});
});
</script>

Populate field dynamically based on the user choice value in the another field in Symfony 5

I am trying to "Update field dynamicly based on the user choice in another field in Symfony 5" from "https://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data" this side. But i faced this exceptions/error "Argument 2 passed to App\Form\SportMeetupType::App\Form{closure}() must be an instance of App\Form\Sport or null, instance of App\Entity\Sport given, called in C:\Apache24\htdocs\dynamicform\src\Form\SportMeetupType.php on line 58" when ajax called.
My Code:
Form :
class SportMeetupType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('sport', EntityType::class, [
'class' => 'App\Entity\Sport',
'placeholder' => '',
])
;
$formModifier = function (FormInterface $form, Sport $sport = null) {
$positions = null === $sport ? [] : $sport->getPositions();
$form->add('position', EntityType::class, [
'class' => 'App\Entity\Position',
'placeholder' => '',
'choices' => $positions,
]);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. SportMeetup
$data = $event->getData();
$formModifier($event->getForm(), $data->getSport());
}
);
$builder->get('sport')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$sport = $event->getForm()->getData();
// since we've added the listener to the child, we'll have to pass on
// the parent to the callback functions!
$formModifier($event->getForm()->getParent(), $sport); // Here line 58 and error show this line
}
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SportMeetup::class,
]);
}
}
Controller:
/**
* #Route("/new", name="sport_meetup_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$sportMeetup = new SportMeetup();
$form = $this->createForm(SportMeetupType::class, $sportMeetup);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($sportMeetup);
$entityManager->flush();
return $this->redirectToRoute('sport_meetup_index');
}
return $this->render('sport_meetup/new.html.twig', [
'sport_meetup' => $sportMeetup,
'form' => $form->createView(),
]);
}
Twig/View:
{% extends 'base.html.twig' %}
{% block title %}New SportMeetup{% endblock %}
{% block body %}
<h1>Create new SportMeetup</h1>
{{ form_start(form) }}
{{ form_row(form.name)}}
{{ form_row(form.sport) }} {# <select id="sport_meetup_sport" ... #}
{{ form_row(form.position) }} {# <select id="sport_meetup_position" ... #}
{# ... #}
{{ form_end(form) }}
{% endblock %}
JavaScript code:
var $sport = $('#sport_meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: "POST",
data : data,
success: function(html) {
// Replace current position field ...
$('#sport_meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#sport_meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
When changed Sport dropdown value then ajax called and show below exceptions/error
"Argument 2 passed to App\Form\SportMeetupType::App\Form{closure}() must be an instance of App\Form\Sport or null, instance of App\Entity\Sport given, called in C:\Apache24\htdocs\dynamicform\src\Form\SportMeetupType.php on line 58"
Please help me....
Make sure that in your SportMeetupType.php you have:
use App\Entity\Sport;
I think you are using wrong class.

Called a function of an entity in twig

I'm looking to retrieve information from an entity by twig by passing the id as a parameter. But I block on the function in my entity:
My entity (function need call):
public function getNameFournisseur($id)
{
???
}
My twig:
{{ staticFournisseur.getNameFournisseur(idFournisseur) }}
My controller:
/**
* #Route("/new", name="new_invoice", methods={"GET","POST"})
*/
public function new(Request $request, SessionInterface $session, ArticlesRepository $articlesRepository, FournisseursRepository $fournisseursRepository): Response
{
$invoice = new Invoice();
$form = $this->createForm(InvoiceType::class, $invoice);
$form->handleRequest($request);
$articles = $session->get('articleInvoice', []);
$articleData = [];
foreach ($articles as $k => $article) {
$articleData [] = [
'articleInvoice' => $articlesRepository->find($k),
'quantityInvoice' => $article
];
}
$total = 0;
foreach ($articleData as $totalArticle) {
$totalArticles = $totalArticle['articleInvoice']->getPrice() * $totalArticle['quantityInvoice'];
$total += $totalArticles;
}
$session->set('totalHt', $total);
$totalAllArticles = $session->get('totalHt');
$tauxDiscount = $session->get('discountTaux');
if (!$tauxDiscount) {
$totalWithDiscount = $total;
} else {
$totalWithDiscount = $totalAllArticles - ($totalAllArticles * $tauxDiscount) / 100;
}
if ($form->isSubmitted()) {
//$session->remove('idFournisseur');
$id_fournisseur = (int)$request->request->get('order')['fournisseur_id'];
$fournisseur = $fournisseursRepository->findOneBy(['id' => $id_fournisseur]);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($invoice);
$entityManager->flush();
return $this->redirectToRoute('invoices');
}
return $this->render('admin/invoices/new.html.twig', [
'staticFournisseur' => new Fournisseur(),
'idFournisseur' => $session->get('idFournisseur'),
'discountTaux' => $tauxDiscount,
'totalHt' => $totalWithDiscount,
'art' => $articleData,
'form' => $form->createView()
]);
}
I therefore seek to recover the name of the supplier thanks to the id that I pass as a parameter in my twig.
getNameFournisseur($id) is a function that retrieves data from the database, not from a single (already loaded) entity; in other words, it should be in FournisseursRepository instead
Also, if you're using Doctrine, you usually want to load the full entity instead of just a field
'staticFournisseur' => $fournisseursRepository->findOneById($session->get('idFournisseur'));
and in twig:
{{ staticFournisseur.getName() }}
or even
{{ staticFournisseur.name }}

Could not load type "locality"

I hope to use a form inside my controller,but I get everytime the following error :
Could not load type "locality"
and here is ly form class :
class LocationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$factory = $builder->getFormFactory();
$builder->add('province','entity',array(
'class' => 'Acme\DemoBundle\Entity\Province',
'property' => 'name'));
$refreshLocality = function ($form, $province) use ($factory) {
$form->add($factory->createNamed('entity','locality',null, array(
'class' => 'Acme\DemoBundle\Entity\Locality',
'property' => 'name',
'label' => 'Locality',
'query_builder' => function (EntityRepository $repository) use ($province) {
$qb = $repository->createQueryBuilder('locality')
->innerJoin('locality.province', 'province');
if($province instanceof Province) {
$qb = $qb->where('locality.province = :province')
->setParameter('province', $province);
} elseif(is_numeric($province)) {
$qb = $qb->where('province.id = :province_id')
->setParameter('province_id', $province);
} else {
$qb = $qb->where('province.id = 1');
}
return $qb;
}
)));
};
$builder->add('address','text',array(
'required' => false));
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($refreshLocality) {
$form = $event->getForm();
$data = $event->getData();
if($data == null)
$refreshLocality($form, null); //As of beta2, when a form is created setData(null) is called first
if($data instanceof Location) {
$refreshLocality($form, $data->getLocality()->getProvince());
}
});
$builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) use ($refreshLocality) {
$form = $event->getForm();
$data = $event->getData();
if(array_key_exists('province', $data)) {
$refreshLocality($form, $data['province']);
}
});
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Location'
));
}
public function getName()
{
return 'acme_demobundle_locationtype';
}
}
Then I called this class in my controller :
public function indexAction()
{
$form = $this->get('form.factory')->create(new \Acme\DemoBundle\Form\LocationType());
$request = $this->get('request');
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
}
}
return array('form' => $form->createView());;
}
and here my twig :
<form action="{{ path('_demo') }}" method="POST" id="contact_form">
{{ form_errors(form) }}
{{ form_rest(form) }}
<input type="submit" value="Send" class="symfony-button-grey" />
</form>
when I had the error above I tried to register your form in the section services in service.xml :
<service id="form.type.acme_demobundle_locationtype" class="Acme\DemoBundle\Form\LocationType">
<tag name="form.type" alias="acme_demobundle_locationtype" />
</service>
but I get the same error,any idea?
You need to swap the arguments that you passed to createNamed():
$form->add($factory->createNamed('entity','locality',null, array(
should be
$form->add($factory->createNamed('locality', 'entity', null, array(
In fact, you can even simplify your code to
$form->add('locality', 'entity', array(

How to render collections of forms

I have problem with rendering forms in Twig. I'm trying to embed a collection of forms. When I render the collection, it is not shown, while it is shown the name of form. The aim was to make the forms with an add button to add at runtime the form for each element of the collection. I get a look to the Symfony Docs and I think I followed it step by step.
This is my controller:
function new_resultAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$test = $em->getRepository('LadelaOdeskTesterBundle:Test')->find($id);
$categories =
$em->getRepository('LadelaOdeskTesterBundle:Category')->findByTest($test);
if (!$test) {
throw $this->createNotFoundException('Unable to find Test entity.');
}
$resultCategory = new Category();
$form = $this->createForm(new CategoryType() , $resultCategory);
$request = $this->getRequest();
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$em->persist($resultCategory);
$em->flush();
$this->get('session')->setFlash('success', 'New result Categories were
saved!');
return $this->redirect($this->generateUrl('questions', array(
'id' => $resultCategory->getId(),
)));
}
}
return array(
'test' => $test,
'categories' =>$categories,
'form' => $form->createView(),
);
}
My forms:
class ResultCategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('value', 'integer');
$builder->add('category', 'entity', array(
'class' => 'Ladela\OdeskTesterBundle\Entity\Category',
'query_builder' => function ($repository) { return
$repository->createQueryBuilder('p')->orderBy('p.name', 'ASC'); },
'property' => 'name' ,
'expanded' => false ,
'multiple' => false , ));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Ladela\OdeskTesterBundle\Entity\ResultCategory'
));
}
public function getName()
{
return 'ResultCategory';
}
}
class CategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('resultCategories','collection', array(
'type' => new ResultCategoryType(),
'allow_add' => true,
'by_reference' => false,
'prototype' => true,
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Ladela\OdeskTesterBundle\Entity\Category'
));
}
public function getName()
{
return 'category';
}
}
And my twig file:
<form method="post" action="{{ path('questions',{'id': test.id }) }}" {{
form_enctype(form) }} >
<ul class="tags" data-prototype="{{
form_widget(form.resultCategories.vars.prototype)|e
}}">
{# iterate over each existing tag and render its only field: name #}
{% for ResultCategory in form.resultCategories %}
<li>{{ form_row(ResultCategory.category) }}</li>
{% endfor %}
Add a tag
</ul>
{{ form_rest(form) }}{# form.items's prototype is rendered twice #}
{{ form_errors(form) }}
<input type="submit" value ="add" />
</form>
{% block javascripts %}
<script type="text/javascript">
// Get the div that holds the collection of tags
var collectionHolder = $('ul.tags');
// setup an "add a tag" link
var $addTagLink = $('Add a tag');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// add the "add a tag" anchor and li to the tags ul
collectionHolder.append($newLinkLi);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm(collectionHolder, $newLinkLi);
});
});
// Get the div that holds the collection of tags
var collectionHolder = $('ul.tags');
// setup an "add a tag" link
var $addTagLink = $('Add a tag');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// add the "add a tag" anchor and li to the tags ul
collectionHolder.append($newLinkLi);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm(collectionHolder, $newLinkLi);
});
});
{% endblock %}
The collection of forms is not rendered since you don't initiate it! See the official docs. There are "dummy" tags object used to create the tag forms.

Resources