Sorry in advance for the quality of my English
I would create a form to create a Employee. Employee and Team are connected with the ManytoOne bidirectional Relation, Team and Division are connected with the ManytoOne bidirectional Relation, and Division and Center are connected with the manytoOne bidirectional Relation. In my form I want to be able to filter the list of Teams with Javascript, according to the choice made on a first select on Center then Division to reduce the list of Teams.
Here is my code :
EmployeeType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('birthDate', DateType::class)
->add('firstName', TextType::class)
->add('name', TextType::class)
->add('isActive', CheckboxType::class, array('required' => false))
->add('employeePicture', EmployeePictureType::class)
->add('team', TeamType::class)
->add('save', SubmitType::class);
}
TeamType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shortName',EntityType::class, array(
'class' => 'RTELiveWorkingBundle:Team',
'choice_label' => 'shortName',
'placeholder' => 'Choisir une équipe'
))
->add('divsion', DivisionType::class);
}
DivisionType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shortName',EntityType::class, array(
'class' => 'RTELiveWorkingBundle:Division',
'choice_label' => 'shortName',
'placeholder' => 'Choisir un GMR'
))
->add('center', CenterType::class);
}
And CenterType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shortName',EntityType::class, array(
'class' => 'RTELiveWorkingBundle:Center',
'choice_label' => 'shortName',
'placeholder' => 'Choisir un centre'
));
}
My controller :
public function addAction(Request $request) {
$employee = new Employee();
$form = $this->createForm(EmployeeType::class, $employee);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($employee);
$em->flush();
return $this->redirectToRoute('rte_live_working_employee_view', array('id' => $employee->getId()));
}
return $this->render('RTELiveWorkingBundle:Employee:add.html.twig', array('form' => $form->createView()));
}
My forms :
I have the error :
Catchable Fatal Error: Method
RTE\LiveWorkingBundle\Entity\Team::__toString() must return a string
value
But I have to implement __toString() in my entity :
public function __toString()
{
return $this->getShortName();
}
have you got an idea ?
Related
I'm trying to fill my ChoiceType with an array, but it looks like it's filled with the IDs and not with the values. The form is correctly displayed, but the choices are '0', '1'... instead of the names in the array.
This is my controller:
$categories = $this->getDoctrine()->getRepository('myBundle:Category')->findAll();
$techChoices = array();
$i = 0;
foreach($categories as $t) {
$techChoices[$i] = $t->getName();
$i = $i + 1;
}
$formOptions = array('categories' => $techChoices);
$document = new Document($categories);
$form = $this->createForm(DocumentType::class, $document, $formOptions);
And this is my buildForm:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class)
->add('file_name', TextType::class)
->add('file_description', TextType::class)
->add('file_group', ChoiceType::class, array(
'choices' => $options['categories'],
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'categories' => array(),
));
}
Depending on your Symfony's version (since 2.8), you are building the choice array the wrong way.
From 3.3 documentation :
... where the array key is the item's label and the array value is the item's value.
The correct way to show the categories in your case is using EntityType, this will free up your code mess. You don't have to get/pass categories to form anymore.
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('file', FileType::class)
->add('file_name', TextType::class)
->add('file_description', TextType::class)
->add('file_group', \Symfony\Bridge\Doctrine\Form\Type\EntityType::class, array(
// query choices from this entity
'class' => 'AppBundle:Category',
'choice_label' => 'name',
))
;
}
If you want to use an array directly in form choice type then see https://symfony.com/doc/current/reference/forms/types/choice.html#example-usage, or if you want to use data from a table(entity) then see https://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage
Answer to your question is the array format should be like
['data_to_be_seen1' => value1(id), 'data_to_be_seen2' => value2(id),...]
(see the first link),
all the best
you can directly do this :
$builder
->add('file', FileType::class)
->add('file_name', TextType::class)
->add('file_description', TextType::class)
->add('file_group', ChoiceType::class, array(
'choices' => 'here you pass your categories entities directly',
'choice_label' => 'name',
));
like that, it will do the mapping alone
I have 3 tables/entities. I am trying to create form to persist data to all 3 table in one form. Entitities are to long that is why I haven't added here.
Entity Client I made an array collection in Client entity for other 2 entities.
protected $Clientservice;
protected $Hostingaccount;
public function __construct()
{
$this->Clientservice = new ArrayCollection();
$this->Hostingaccount= new ArrayCollection();
}
public function getClientservice()
{
return $this->Clientservice;
}
public function getHostingaccount()
{
return $this->Hostingaccount;
}
public function setClientservice($Clientservice)
{
$this->Clientservice = $Clientservice;
return $this;
}
public function setHostingaccount($Hostingaccount)
{
$this->Hostingaccount = $Hostingaccount;
return $this;
}
And I Have 3 forms:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('CustomerID',IntegerType::class)
->add('Sex',TextType::class)
->add('Name',TextType::class)
->add('FirstName',TextType::class)
->add('LastName',TextType::class)
->add('Email', EmailType::class)
->add('Invoiceemail', EmailType::class)
->add('Iban', TextType::class)
->add('Bic', TextType::class)
->add('SEPAMandateID', TextType::class)
->add('LogoUrl', TextType::class)
->add('CreationDate', DateType::class)
->add('Clientservice', CollectionType::class, array(
'entry_type' => WhmAdminClientserviceType::class
))
->add('Hostingaccount', CollectionType::class, array(
'entry_type' => WhmAdminHostingAccountType::class
))
->getForm();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Client::class
));
}
Clientservice form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('Description',TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Clientservice::class
));
}
Hostingaccount form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('Domain',TextType::class)
->add('Domainip',IntegerType::class)
->add('UserName',TextType::class)
->add('Owner',TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Hostingaccount::class
));
}
In my controller I have the code like this:
public function AddWhmAdminAction(Request $request)
{
$Client = new Client();
$form = $this->createForm(WhmAdminType::class, $Client);
$form->add('submit', SubmitType::class, array(
'label' => 'Create',
'attr' => array('class' => 'btn btn-default pull-left')
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($Client);
$em->flush();
$this->get('session')->getFlashBag()->add('green', 'Product created!');
return $this->redirectToRoute('whmAdmin');
// flash msg
}
//return $form->createView();
$build['form'] = $form->createView();
return $this->render('whm/WhmAdminForm.html.twig', $build);
}
and in my view
{{ form_start(form) }}
{% for flashMessage in app.session.flashbag.get('green') %}
{{ flashMessage }}
{% endfor %}
{{ form_end(form) }}
I followed http://symfony.com/doc/current/form/form_collections.html and this is how far I got.
My form only shows the fields from Client. It does not shows field from Clientservice and hostingaccount. How can I show the fields from clientservice and hostingaccount in the same form as Client.
If the Clientservice and Hostingaccount are in OneToOne or ManyToOne relationship then change:
->add('Clientservice', CollectionType::class, array(
'entry_type' => WhmAdminClientserviceType::class
))
->add('Hostingaccount', CollectionType::class, array(
'entry_type' => WhmAdminHostingAccountType::class
))
to:
->add('Clientservice', WhmAdminClientserviceType::class)
->add('Hostingaccount', WhmAdminHostingAccountType::class)
Otherwise leave your form builders and read about adding and removing items in collection type.
Whats the best way to set a collection of key/values pairs (obtained from MySQL) as a choice field 'choices' inside a controller?
I think about something similar to :
$form = $this->createForm(new AddNews(), $news);
$newsList = $this->getDoctrine()
->getRepository('BakaMainBundle:News')->getAllNews();
$titlesList = ...($newsList); // some fuction that extract title=>id
// array from news object collection
$form->get('newsList')->setData($titlesList);
where the AddNews() form looks like :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(...)
->add(...)
->add('accept' , 'submit')
->add('newsList', 'choice', array
(
'mapped' => false,
'required' => true
));
}
Perhaps something like below (assuming Symfony >= 2.7). See docs for field options:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(...)
->add(...)
->add('accept' , 'submit')
->add('newsList', 'entity', array
(
'class' => ''BakaMainBundle:News'',
'choice_label' => 'title',
'mapped' => false,
'required' => true
));
}
You could get your "news" directly from your formType file, using your repository, like that:
private function getNews(){
$newsList = $this->getDoctrine()
->getRepository('BakaMainBundle:News')->getAllNews();
$titlesList = ...($newsList); // some fuction that extract title=>id
// array from news object collection
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(...)
->add(...)
->add('accept' , 'submit')
->add('newsList', 'choice', array
(
'mapped' => false,
'required' => true,
'choices' => $this->getNews()
));
}
I am trying to implement a one to one relation. Each User can be affected at a building (Etablissement for me). A building can have many people but each people can be affected at one building at most.
I have this error :
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Intranet\UserBundle\Entity\User::setUserEtab() must be an instance of Intranet\RhBundle\Entity\Etablissement, array given, called in C:\wamp\www\projet\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 360 and defined in C:\wamp\www\projet\src\Intranet\UserBundle\Entity\User.php line 322
The line 322 of User.php is :
public function setEtablissement(\Intranet\RhBundle\Entity\Etablissement $etablissement = null)
It occurs at the line $form->handleRequest($request); of my controller. This is my controller :
public function editerAction(Request $request, User $user){
$form = $this->createForm(new EditerFormType, $user);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
die("ici");
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$this->get('session')->getFlashBag()->add('success', "L'utilisateur ". $user->getNom() ." ". $user->getPrenom() . " a été édité avec succès !");
$em->flush();
return $this->redirect($this->generateUrl('intranet_rh_homepage'));
}else
$this->get('session')->getFlashBag()->add('danger', "Erreur de formulaire !");
}
return $this->render('IntranetRhBundle:User:editer.html.twig',array('user' => $user, 'form' => $form->createView()));
}
The die doesn't work. Before adding the form, it was working.
This is the EditForm :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('nom', 'text')
->add('prenom', 'text')
->add('naissance','date',array(
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'attr' => array('class' => 'date', 'readonly' => 'readonly')
))
->add('sexe', 'choice', array(
'choices' => array('Homme' => 'Homme', 'Femme' => 'Femme'),
'multiple' => false
))
->add('etablissement', new UserEtabType())
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => 'Intranet\Userbundle\Entity\User'));
}
This is my UserEtab form :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('etablissement', 'entity', array('class' => 'IntranetRhBundle:Etablissement', 'property' => 'nom', 'empty_value' => 'Aucun', 'empty_data' => -1, 'required' => false))
;
}
And my relation on User entity :
/**
* #ORM\OneToOne(targetEntity="Intranet\RhBundle\Entity\Etablissement", cascade={"persist", "remove"})
**/
private $etablissement;
The form's view is okay, there is a select list with all the building and an empty value. But when I post, I have this error I can't understand and solve.
I have the setter and getter if User entity :
/**
* Set Etablissement
*
* #param \Intranet\RhBundle\EntityEtablissement $etablissement
* #return User
*/
public function setEtablissement(\Intranet\RhBundle\Entity\Etablissement $etablissement = null)
{
$this->etablissement = $etablissement;
return $this;
}
/**
* Get Etablissement
*
* #return \Intranet\RhBundle\Entity\Etablissement
*/
public function getEtablissement()
{
return $this->etablissement;
}
But when I var_dump $request->get('user')->get('etablissement') it doesn't work :
Error: Call to undefined method Intranet\UserBundle\Entity\User::get() in C:\wamp\www\projet\src\Intranet\RhBundle\Controller\UserController.php line 69
And the building doesn't appear in var_dump of $request->get('user').
EDIT :
I don't have a setDefaultOptions method for my UserEtabType because I don't know what I have to do. I tried to implements it but I have this kind of error :
The form's view data is expected to be an instance of class Intranet\UserBundle\Entity\User, but is an instance of class Proxies__CG__\Intranet\RhBundle\Entity\Etablissement. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Proxies__CG__\Intranet\RhBundle\Entity\Etablissement to an instance of Intranet\UserBundle\Entity\User.
For the new UserEtabType :
namespace Intranet\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserEtabType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('etablissement', 'entity', array('class' => 'IntranetRhBundle:Etablissement', 'property' => 'nom', 'empty_value' => 'Aucun', 'empty_data' => -1, 'required' => false))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => 'Intranet\UserBundle\Entity\User'));
}
public function getName()
{
return 'intranet_userbundle_useretablissementtype';
}
}
Can you show the new UserEtabType() class ?
EDIT 1 : Try putting this line :
->add('etablissement', 'entity', array('class' => 'IntranetRhBundle:Etablissement', 'property' => 'nom', 'empty_value' => 'Aucun', 'empty_data' => -1, 'required' => false));
in the main EditForm
EDIT 2:
plus be careful :
$resolver->setDefaults(array('data_class' => 'Intranet\UserBundle\Entity\User'));
must be
$resolver->setDefaults(array('data_class' => 'Intranet\UserBundle\Entity\Etablissement'));
in your UserEtabType() class
Are you getting this error when you try to insert a record ? Or before that. For your information, setUserEtab() in the User entity expects an object of Etablissement, because of the one-to-one relationship. Passing the value of an array is not sufficient. Try passing the object of Etablissement it self (the selected object after performing dql).
Hope this helps someone.
Cheers!
I'm on a problem related to a Symfony2 application which i'm building. The problem concerns an article (News) linked to one or many pictures (Illustration). It seems pretty simple. But i'm stacking on the controller which should persist the News, the Illustration and uploading the picture file.
My News form type:
namespace Fcbg\NewsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class NewsType extends AbstractType
{
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder
->add('date', 'date')
->add('titre', 'text')
->add('contenu', 'textarea')
->add('publication', 'checkbox', array('required' => false))
->add('type', 'entity', array( 'class' => 'FcbgNewsBundle:Illustration', 'property' => 'value', 'multiple' => true ));
}
public function getName()
{
return 'News';
}
}
My picture(s) form type:
namespace Fcbg\NewsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class IllustrationType extends AbstractType
{
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder
->add('file', 'file');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Fcbg\NewsBundle\Entity\Illustration',
'cascade_validation' => true,
));
}
public function getName()
{
return 'News';
}
}
My controller action:
public function addAction()
{
//link works properly I think
$news = new News();
$illustration = new Illustration();
$illustration->setNews($news);
$news->addIllustration($illustration);
$form = $this->createForm(new NewsType(), $news);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$doctrine = $this->getDoctrine();
$newsManager = $doctrine->getManager();
$newsManager->persist($news);
$newsManager->persist($illustration);
$newsManager->flush();
return $this->redirect(...);
}
}
return $this->render('base.html.twig',
array(
'content' => 'FcbgNewsBundle:Default:formulaireNews.html.twig',
'form' => $form->createView(),
'name' => "add a news"
)
);
}
The error i get on the execution:
Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
The problem here is that my entity gets a method called "getIllustrations()" which returns logically an arrey of Illustration. So i'm not able to understand this error/question. I assume that my "illustration should be a file field and not a choice field...
Any idea about how i can go further? thx a lot!
I think the problem is that you are using the 'entity' form field here:
->add('type', 'entity', array( 'class' => 'FcbgNewsBundle:Illustration', 'property' => 'value', 'multiple' => true ));
and this type of form field acts as a choice and it's used to work with elements created in the database. You can see this in http://symfony.com/doc/current/reference/forms/types/entity.html
A possible solution could be use the "prototype" like here http://symfony.com/doc/current/cookbook/form/form_collections.html#cookbook-form-collections-new-prototype
where you could have:
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder
->add('date', 'date')
->add('titre', 'text')
->add('contenu', 'textarea')
->add('publication', 'checkbox', array('required' => false))
->add('type', 'collection', array(
'type' => new IllustrationType(),
'allow_add' => true,
));
}
I hope that this be useful for you.
Kind regards.
So, the code which I was talking about:
public function buildForm( FormBuilderInterface $builder, array $options )
{
$builder
->add('date', 'date')
->add('titre', 'text')
->add('contenu', 'textarea')
->add('publication', 'checkbox', array('required' => false))
->add('illustrations', 'collection', array(
'type' => new IllustrationType(),
'allow_add' => true
));
}