Form submission problems - symfony

I want to create new user and give him a role from combo box but it doesn't work here is what have I done so far:
ROLE FORM
class RoleType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('role','entity',array(
'class' => 'vendor\Bundle\Entity\Role',
'property'=>'role'
))
;
}
USER FORM
class UserType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('password')
->add('email')
->add('firstname')
->add('lastname')
->add('role',new RoleType())
;
}
But I can't save my user to database I get this error
An exception occurred while executing 'UPDATE Roles SET role = ? WHERE role_id = ?' with params [{}, 1]:
CONTROLLER
private function createEditForm(User $entity)
{
$form = $this->createForm(new UserType(), $entity, array(
'action' => $this->generateUrl('user_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing User entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('myBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
}
return $this->render('myBundle:User:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}

Why don't you just define the role field inside the UserType builder?
class UserType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('password')
->add('email')
->add('firstname')
->add('lastname')
->add('role','entity',array(
'class' => 'vendor\Bundle\Entity\Role',
'property'=>'role'
))
;
}

Related

Hide a field according to its role Symfony 3

First sorry for my english because is not great!
So I want to hide a field according to it's role because if I make with Twig the field to display on the bottom form
My code for understand, this's my LinkType :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('link')
->add('description')
// this field to hidden according the role
->add('published', CheckboxType::class);
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Link'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_link';
}
a part of Controler:
public function newAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$categories = $em->getRepository('AppBundle:Category')->findAll();
$subCategories = $em->getRepository('AppBundle:SubCategory')->findAll();
$gestionCategorie = $this->container->get('app.categorie');
$link = new Link();
$repository = $this
->getDoctrine()
->getManager()
->getRepository('AppBundle:Link');
$category = $repository->findCategory();
$subCategory = $repository->findSubCategory();
$form = $this
->get('form.factory')
->create('AppBundle\Form\LinkType', $link)
->add('categories', ChoiceType::class, array(
// on inverse les clés et valeurs
'choices' => array_flip($category),
'label' => "Catégorie",
'attr' => ['class' => 'form-control'],
))
->add('sousCategories', ChoiceType::class, array(
// on inverse les clés et valeurs
'choices' => array_flip($subCategory),
'label' => "Sous-catégorie",
'attr' => ['class' => 'form-control'],
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
If you have other questions, don't hesitate
If you want to do this in the controller, first remove your field in your form class, then add:
$user = $this->getUser();
if (in_array('MY_ROLE_NAME', $user->getRoles())) {
$builder->add('published', CheckboxType::class);
}
A better and cleaner approach is using a form as a service and injecting token storage service into it:
// services.yml
AppBundle\Form\:
resource: '../../src/AppBundle/Form'
public: true
autowire: true
// Form type class
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//...
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user = $this->tokenStorage->getToken()->getUser();
$builder
->add('title')
->add('link')
->add('description');
if (in_array('MY_ROLE_NAME', $user->getRoles())) {
$builder->add('published', CheckboxType::class);
}
}

EntityForm on a ManyToMany bidirectional, for the two sides of the relation

I've 2 entity: User and Strain with a ManyToMany bidirectional relation, the owner of the relation is User.
I want do a form for edit the rights (the User own some Strains), when I do a form for the User where I can select some Strains I want, it works fine (I use an EntityType on Strain). But... Sometimes, I want edit the rights by the other side of the relation: Strain. ie edit the Strain and select the Users I want. But it doesn't work...
I give you my entities User and Strain and the two FormType, and my Uglys Solution...
User.php
/**
* The authorized strains for this user.
*
* #var Strain|ArrayCollection
*
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Strain", inversedBy="authorizedUsers")
*/
private $authorizedStrains;
/**
* User constructor.
*/
public function __construct()
{
$this->authorizedStrains = new ArrayCollection();
}
/**
* Add an authorized strain.
*
* #param Strain $strain
*
* #return $this
*/
public function addAuthorizedStrain(Strain $strain)
{
$this->authorizedStrains[] = $strain;
$strain->addAuthorizedUser($this);
return $this;
}
/**
* Remove an authorized strain.
*
* #param Strain $strain
*/
public function removeAuthorizedStrain(Strain $strain)
{
$this->authorizedStrains->removeElement($strain);
$strain->removeAuthorizedUser($this);
}
/**
* Get authorized strains.
*
* #return Strain|ArrayCollection
*/
public function getAuthorizedStrains()
{
return $this->authorizedStrains;
}
Strain.php
/**
* The authorized user.
* For private strains only.
*
* #var User|ArrayCollection
*
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="authorizedStrains")
*/
private $authorizedUsers;
/**
* Strain constructor.
*/
public function __construct()
{
/**
* Add authorized user.
*
* #param User $user
*
* #return $this
*/
public function addAuthorizedUser(User $user)
{
$this->authorizedUsers[] = $user;
return $this;
}
/**
* Remove authorized user.
*
* #param User $user
*/
public function removeAuthorizedUser(User $user)
{
$this->authorizedUsers->removeElement($user);
}
/**
* Get authorized users.
*
* #return User|ArrayCollection
*/
public function getAuthorizedUsers()
{
return $this->authorizedUsers;
}
UserRightsType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('authorizedStrains', EntityType::class, array(
'class' => 'AppBundle\Entity\Strain',
'choice_label' => 'name',
'expanded' => true,
'multiple' => true,
'required' => false,
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User',
));
}
StrainRightsType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('authorizedUsers', EntityType::class, array(
'class' => 'AppBundle\Entity\User',
'query_builder' => function(UserRepository $ur) {
return $ur->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
'choice_label' => function ($user) {
return $user->getUsername().' ('.$user->getFirstName().' '.$user->getLastName().')';
},
'expanded' => true,
'multiple' => true,
'required' => false,
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Strain',
));
}
StrainController.php the ugly solution
public function userRightsAction(Request $request, Strain $strain)
{
$form = $this->createForm(StrainRightsType::class, $strain);
$form->add('save', SubmitType::class, [
'label' => 'Valid the rights',
]);
foreach($strain->getAuthorizedUsers() as $authorizedUser) {
$authorizedUser->removeAuthorizedStrain($strain);
}
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
foreach($strain->getAuthorizedUsers() as $authorizedUser)
{
$authorizedUser->addAuthorizedStrain($strain);
$em->persist($authorizedUser);
}
$em->flush();
$request->getSession()->getFlashBag()->add('success', 'The user\'s rights for the strain '.$strain->getName().' were successfully edited.');
return $this->redirectToRoute('strain_list');
}
return $this->render('strain/userRights.html.twig', [
'strain' => $strain,
'form' => $form->createView(),
]);
}
As you can see, I do 2 foreach: the first to remove all the rights on the Strain, and the second to give rights.
I think Symfony have anticipated this problem, but I don't know how to do, and I've found nothing in the documentation...
Thank you in advance for your help,
Sheppard
Finaly, I've found.
On the inversed side (Strain.php):
public function addAuthorizedUser(User $user)
{
$user->addAuthorizedStrain($this);
$this->authorizedUsers[] = $user;
return $this;
}
public function removeAuthorizedUser(User $user)
{
$user->removeAuthorizedStrain($this);
$this->authorizedUsers->removeElement($user);
}
And, on the owner side (User.php)
public function addAuthorizedStrain(Strain $strain)
{
if (!$this->authorizedStrains->contains($strain)) {
$this->authorizedStrains[] = $strain;
}
return $this;
}
public function removeAuthorizedStrain(Strain $strain)
{
if ($this->authorizedStrains->contains($strain)) {
$this->authorizedStrains->removeElement($strain);
}
}
And in the FormType (for the inverse side) (StrainRightsType)), add 'by_reference' => false
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('authorizedUsers', EntityType::class, array(
'class' => 'AppBundle\Entity\User',
'query_builder' => function(UserRepository $ur) {
return $ur->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
'choice_label' => function ($user) {
return $user->getUsername().' ('.$user->getFirstName().' '.$user->getLastName().')';
},
'by_reference' => false,
'expanded' => true,
'multiple' => true,
'required' => false,
))
;
}

Symfony2 - Display a form recursively

Hello everybody (please excuse my English).
I want to do an application which needs to allow that the users must fill out on a form their personal data, their children, grandchildren and great-grandchildren (a little family tree).
class Person
{
/**
* #var int
*
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #var string
*
* #ORM\Column(type="string")
*/
private $firstname;
/**
* #var string
*
* #ORM\Column(type="string")
*/
private $lastname;
/**
* #var \DateTime
*
* #ORM\Column(type="datetime")
*/
private $dateOfBirth;
/**
* #var Person
*
* #ORM\ManyToMany(targetEntity="Person")
*/
private $children;
public function __construct()
{
$this->children = new ArrayCollection();
}
}
}
In the PersonType class, I do the following:
class PersonType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstname');
$builder->add('lastname');
$builder->add('dateOfBirth');
$builder->add('children', 'collection', array(
'type' => new PersonType(),
'allow_add' => true,
'by_reference' => false,)
);
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Anything\YourBundle\Entity\Person'
));
}
/**
* #return string
*/
public function getName()
{
return 'person';
}
}
In this way, I use the PersonType in the controller as below:
public function newAction()
{
$entity = new Person();
$form = $this->createForm(new PersonType(), $entity, array(
'action' => $this->generateUrl('person_create'),
'method' => 'POST',
));
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
But the problem is when I request the url of this action, and the view of this action has to be rendered, there is a problem because doesn't give a response, because is in a infinite loop (I think that is the reason). I would like to know if is this possible to do using the Symfony forms, or if I have to look at other alternatives. If this was possible, how could I do that and how could I limit the form to only render the four levels that I need (me, my children, my grandchildren and my great-grandchildren)??
I hope that the problem has been understood.
Thanks in advance.
You could add a custom parameter to your form that indicates the current level of recursion.
To archive this you first need to implement a new option:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Anything\YourBundle\Entity\Person',
'recursionLevel' => 4
));
}
Now you update this value in your buildForm method:
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
if (--$options['recursionLevel'] > 0) {
$resolver = new OptionsResolver();
$resolver->setDefaults(
$options
);
$childType = new PersonType();
$childType->setDefaultOptions($resolver);
$builder->add('children', 'collection', array(
'type' => $childType,
'allow_add' => true,
'by_reference' => false
));
}
}
This is not tested.
I had the same problem and tried the solutions provided here.
They come with significant drawbacks like a depth limitation and performance overhead - you always create form objects even if there is no data submited.
What I did to overcome this problem was to add a listener for the FormEvents::PRE_SUBMIT event and add the collection type field dynamically if there is data to be parsed.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content');
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$node = $event->getData();
$form = $event->getForm();
if (!$node) {
return;
}
if(sizeof(#$node['children'])){
$form->add('children', CollectionType::class,
array(
'entry_type' => NodeType::class,
'allow_add' => true,
'allow_delete' => true
));
}
});
}
I hope this helps someone that has this issue in the future
Thanks for the answer Ferdynator!!
I didn't solve the problem in the way you proposed, but that approach helped me. I passed the recursion level in the constructor of the Person form, and thus, I could know when I had to stop:
class PersonType extends AbstractType
{
private $recursionLevel;
public function __construct( $recursionLevel ){
$this->recursionLevel = $recursionLevel;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if($this->recursionLevel > 0)
{
$builder->add('children', 'collection', array(
'type' => new PersonType(--$this->recursionLevel),
'allow_add' => true,
'by_reference' => false,)
);
}
}
}
Ferdynator, thanks for your answers. And I want to propose my decision based on yours:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Anything\YourBundle\Entity\Person',
'recursionLevel' => 4
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
if (--$options['recursionLevel'] > 0) {
$builder->add('children', 'collection', array(
'type' => $childType,
'allow_add' => true,
'by_reference' => false,
'options' => [
'recursionLevel' => $options['recursionLevel']
],
));
}
}
It solves our problem.

Saving / embedding associated form types

I have 2 Entities User and Member
The User has all of the normal stuff associated (username, password, email)
The Member has some other fields, including, First Name, Last Name, Age etc
I'm trying to create a new Member and to display a form with both the User fields and the Member fields.
I currently have the following:
User.php
$protected $username;
$protected $email;
/**
* #ORM\OneToMany(targetEntity="BM\UserBundle\Entity\Member", mappedBy="user")
*/
protected $member;
Member.php
/**
* #ORM\ManyToOne(targetEntity="BM\UserBundle\Entity\User", inversedBy="member")
*/
protected $user;
I have 2 form types as well:
MemberType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', new UserType())
->add('firstname')
->add('lastname')
->add('age')
;
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'BM\UserBundle\Entity\User'
);
}
UserType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('password')
;
}
public function getName()
{
return 'user';
}
When I refresh the page where the form is rendered, I get the following error:
Catchable Fatal Error: Argument 1 passed to BM\UserBundle\Entity\Member::setUser() must be an instance of BM\UserBundle\Entity\User, array given, called in /var/www/proj/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 345 and defined in /var/www/proj/src/BM/UserBundle/Entity/Member.php line 259
Line 259 is:
public function setUser(\BM\UserBundle\Entity\User $user = null)
Am I approaching this the right way?
EDIT:
MemberController.php
$member = new Member();
$form = $this->createForm(new MemberType(), $member);
if($request->isMethod('post')) {
$form->submit($request);
if ($form->isValid()) {
$em->persist($member);
$em->flush();
$request->getSession()->getFlashBag()->add('success', 'Member has been saved');
} else {
$request->getSession()->getFlashBag()->add('error', 'Could not save the member');
}
}
Try in this way
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', new UserType())
->add('firstname')
->add('lastname')
->add('age')
;
}
public function setDefaultOptions(array $options)
{
return array(
'data_class' => 'BM\UserBundle\Entity\Member'
);
}
# UserType.php #
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('password')
;
}
public function setDefaultOptions(array $options)
{
return array(
'data_class' => 'BM\UserBundle\Entity\User'
);
}
Take a look the documentation: http://symfony.com/doc/current/cookbook/form/form_collections.html

Many to many relations Symfony2

I am using many to many relations in my symfony2 project, i have successfully made two entities 'Users' and Groups and i have also successfully persisted data which inserts data into users,groups and users_groups table by using
$user = new User();
$user->getGroups()->add($group);
now i want to edit the user which should also edit users_groups record..
I have searched a lot but no luck.Any help would be appreciated ...
CONTROLLER
public function editAction()
{
if($this->getRequest()->query->get("id")){
$id = $this->getRequest()->query->get("id");
$request = $this->getRequest();
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('DesignAppBundle:Users')->find($id);
$editForm = $this->createForm(new UserType());
if ($this->getRequest()->getMethod() == 'POST') {
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$postData = $request->request->get('users');
$repository = $this->getDoctrine()
->getRepository('Bundle:groups');
$group = $repository->findOneById($postData['group']);
$entity->addGroups($group );
$em->flush();
return $this->redirect($this->generateUrl('index'));
}
}
return $this->render('User.html.twig',array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
}
FORM
<?php
use and include .....
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$request = Request::createFromGlobals();
$builder->add('group', 'entity', array(
'class' => 'Bundle:Groups',
'property' => 'name',
'empty_value' => 'All',
'required' => true,
'multiple'=>true,
'data' => $request->get('group_id')
));
}
public function getDefaultOptions(array $options)
{
return array(
'validation_groups' => array('users'),
'csrf_protection' => false,
);
}
public function getName()
{
return 'users';
}
}
User
/**
* #ORM\ManyToMany(targetEntity="Groups", inversedBy="users")
* #ORM\JoinTable(name="users_groups",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
**/
protected $group;
public function __construct()
{
$this->group= new ArrayCollection();
}
public function addgroups(\Design\AppBundle\Entity\Categories $group)
{
$this->group[] = $group;
}
Group
/**
* #ORM\ManyToMany(targetEntity="Users", mappedBy="group")
*/
protected $users;
public function __construct()
{
$this->users= new ArrayCollection();
}
/**
* Add users
*
* #param Bundle\Entity\Users $users
*/
public function addUsers(Bundle\Users $users)
{
$this->users[] = $users;
}
/**
* Get users
*
* #return Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
You should not be adding the group_id, you should be adding the groups property
$builder->add('groups');
The group_id column is automatically handled by Doctrine and you should not be handling it yourself directly. Do you have a group_id property? If you do, you should remove it.
The group entity should be:
/**
* #ORM\ManyToMany(targetEntity="Users", mappedBy="group")
*/
protected $users;
public function __construct()
{
$this->users= new ArrayCollection();
}
/**
* Add users
*
* #param Bundle\Entity\User $user
*/
public function addUsers(Bundle\User $users)
{
$this->users[] = $users;
}
/**
* Get users
*
* #return Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
EDIT
This is how your controller should be:
public function editAction($request)
{
$id = $this->getRequest()->query->get("id");
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('DesignAppBundle:Users')->find($id);
$editForm = $this->createForm(new UserType(),$entity);
if ($this->getRequest()->getMethod() == 'POST') {
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$em->persits($entity);
$em->flush();
return $this->redirect($this->generateUrl('index'));
}
}
return $this->render('User.html.twig',array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
and this is how your UserType should be:
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('group', 'entity', array(
'class' => 'Bundle:Groups',
'property' => 'name',
'empty_value' => 'All',
'required' => true,
'multiple'=>true,
));
}
public function getDefaultOptions(array $options)
{
return array(
'validation_groups' => array('users'),
'csrf_protection' => false,
);
}
public function getName()
{
return 'users';
}
}

Resources