I have the following User entity:
class User extends BaseUser implements ParticipantInterface
{
/**
* #Exclude()
* #ORM\OneToMany(targetEntity="App\MainBundle\Entity\PreferredContactType", mappedBy="user", cascade={"all"})
*/
protected $preferredContactTypes;
/**
* Add preferredContactType
*
* #param \App\MainBundle\Entity\PreferredContactType $preferredContactType
* #return User
*/
public function addPreferredContactTypes(\App\MainBundle\Entity\PreferredContactType $preferredContactType)
{
$this->preferredContactTypes[] = $preferredContactType;
return $this;
}
/**
* Remove preferredContactType
*
* #param \App\MainBundle\Entity\PreferredContactType $preferredContactType
*/
public function removePreferredContactTypes(\App\MainBundle\Entity\PreferredContactType $preferredContactType)
{
$this->preferredContactTypes->removeElement($preferredContactType);
}
/**
* Get preferredContactType
*
* #return \App\MainBundle\Entity\PreferredContactType
*/
public function getPreferredContactTypes()
{
return $this->preferredContactTypes;
}
}
I wanted to create a form that displays multiple choices of the preferredContactType:
$contactOptions = $em->getRepository('AppMainBundle:ContactType')->findAll();
$settingsForm = $this->createForm(new UserType(), $user, array(
'contact' => $contactOptions,
));
and here's what my UserType looks like:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('preferredContactTypes', 'collection', array('type' => 'choice', 'options' => array('choices' => $options['contact'], 'multiple' => true, 'expanded' => true)))
;
}
but now I am getting an error of:
Expected an array.
How do I solve this?
It fails because you pass to choices parameter instance of ArrayCollection class (that was returned in $em->getRepository('AppMainBundle:ContactType')->findAll();). But choices parameter need to be array (http://symfony.com/doc/2.0/reference/forms/types/choice.html#choices).
You can use toArray() method on your ArrayCollection to retrieve the array but it will not give the expected result because it will be array of instances of ContactType.
The best way to form some choice list from entity is to use entity type instead of choice. See more here: http://symfony.com/doc/current/reference/forms/types/entity.html.
Also you can implement Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface in some class and use it as parameter choice_list in choice form field. For example you can extend Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList and implement loadChoiceList() method.
Related
i use Symfony3.3 and want to make a form for the admin administration. The user should be have a group and the group sould have the roles for the backend access.
The form for groups (name and roles) i finished and the form for the admins (name, passwort...) is finish too.
The admin will be find and have the group. If i load the admin it have the arraycollection with the groups.
Here my classes
admin:
class Admin extends BaseUser
{
/**
* #ORM\Id()
* #ORM\Column(name="idAdmin", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(type="string", length=255, options={"default":NULL})
*/
protected $style;
/**
* #ORM\ManyToMany(targetEntity="AdminBundle\Entity\AdminGroup")
* #ORM\JoinTable(
* name="admin_has_group",
* joinColumns={
* #ORM\JoinColumn(name="idAdmin", referencedColumnName="idAdmin")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="idGroup", referencedColumnName="idGroup")
* }
* )
*/
protected $groups;
/**
* #return string
*/
public function getStyle()
{
return $this->style;
}
/**
* #param string $style
*/
public function setStyle($style)
{
$this->style = $style;
return $this;
}
public function setGroups($groups)
{
$this->groups = $groups;
return $this;
}
public function getGroups()
{
return $this->groups;
}
}
groups
class AdminGroup extends BaseGroup
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="idGroup", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Group constructor.
*
* #param string $name
* #param array $roles
*/
public function __construct($name = '', $roles = array())
{
parent::__construct($name, $roles);
}
}
form generation
$admin = $this->getDoctrine()->getRepository(Admin::class)->find(1);
$admingroupList = $this->getDoctrine()->getRepository(AdminGroup::class)->findAll();
$form = $this->createFormBuilder($admin)
->add("username", TextType::class)
->add('plainPassword', PasswordType::class, $passwordSettings)
->add(
'groups', ChoiceType::class, [
'required' => false,
'multiple' => true,
'choices' => $admingroupList,
])
->add('save', SubmitType::class, ['label' => 'Save'])->getForm()->createView();
save form
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$em->persist($user);
$em->flush();
....
}
The first problem is that i have in the overview only display the id's in the select.
The second problem is that i submit the form (with selected groups) symfony crashed with the following message
Call to a member function contains() on array
I try to convert the grouplist to an normal array they will be crashed at the save the data
Expected argument of type "FOS\UserBundle\Model\GroupInterface", "integer" given
I dont know that i sould do to make a simple symfony form with the admin data and group selection... i dont find any example for a form with fosuserbundle...
Have someone an idea what i can to without manipulate the fosuserbundle entites or the symfonycode?
If you need more source, tell me with part :)
Editing 10.12.17
I try to convert the ChoosenArray into this format
$list = [
'user' => 0,
'admin' => 1
];
but than it will be broken at
$form->handleRequest($request);
with the error:
Expected argument of type "FOS\UserBundle\Model\GroupInterface", "integer" given
I do not think the data returned from
$admingroupList = $this->getDoctrine()->getRepository(AdminGroup::class)->findAll();
Will work as you want it too. choices wants something like this
'choices' => [
'Admin' => 'admin',
'User' => 'user'
]
Where the key of the array is the name the user sees, and the value of the array is the value used in the <option>.
You probably need to manipulate the $admingroupList array to mimic the demo array above. Or write your own query in the AdminGroup Repo to return a pre-formatted array for use with a Symfony form.
I'm trying to use a simple form definition to filter some data, so I create the form with no class attached(expecting to use getData() function) and then work with the array of parameters passed to the form, but the form comes always invalid. Result that the form is trying to validate a parameter that do not belong to the context of form.
I'm getting this validation error on the field "almacen":
This value should not be blank.
With cause:
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).data[almacen].responsable = null
I tried using cascade_validation=false but did't work.
In the controller action I declared:
public function indexAction(Request $request)
{
$informeStock = $this->createForm(new BusquedaInformeStockType());
$informeStock->handleRequest($request);
if ($informeStock->isSubmitted() && $informeStock->isValid()) {
$data = $informStock->getData();
// the action logic...
}
...
}
I have a simple form definition, with an entity form type declared and no data_class asociated to the form.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('almacen', 'entity', array(
'class' => 'BusetaBodegaBundle:Bodega',
'placeholder' => '---Seleccione---',
'required' => false,
'label' => 'Bodega',
'attr' => array(
'class' => 'form-control',
),
))
...
...
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
));
}
And this is the definition of the entity Bodega:
class Bodega
{
...
/**
* #var string
*
* #ORM\Column(name="codigo", type="string", nullable=true)
* #Assert\NotBlank()
*/
private $codigo;
/**
* #var string
*
* #ORM\Column(name="nombre", type="string")
* #Assert\NotBlank()
*/
private $nombre;
/**
* #ORM\ManyToOne(targetEntity="Buseta\BodegaBundle\Entity\Tercero", inversedBy="bodega")
* #Assert\NotBlank()
*/
private $responsable;
...
}
In previous versions of the entity Bodega the parameter "responsable" was left in blank, so there is some rows in the db thas has no "responsable" asociated.
But despite that this should not be happening right? What I'm doing wrong?
You have an entity form field with validation constraints:
/**
* #ORM\ManyToOne(targetEntity="Buseta\BodegaBundle\Entity\Tercero", inversedBy="bodega")
* #Assert\NotBlank()
*/
private $responsable;
This is your problem - Assert not blank
Validates that a value is not blank, defined as not strictly false,
not equal to a blank string and also not equal to null
You have few choices, either add validation group(read this and this) or simple remove that Assert. Also its better to use #Assert\Valid for associations like that.
I am using symfony2 with doctrine 2.
I have a many to many relationship between two entities :
/**
* #ORM\ManyToMany(targetEntity="\AppBundle\Entity\Social\PostCategory", inversedBy="posts")
* #ORM\JoinTable(
* name="post_postcategory",
* joinColumns={#ORM\JoinColumn(name="postId", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={#ORM\JoinColumn(name="postCategoryId", referencedColumnName="id", onDelete="CASCADE")}
* )
*/
private $postCategories;
Now I want to let the user only select one category. For this I use the option 'multiple' => false in my form.
My form:
->add('postCategories', 'entity', array(
'label'=> 'Catégorie',
'required' => true,
'empty_data' => false,
'empty_value' => 'Sélectionnez une catégorie',
'class' => 'AppBundle\Entity\Social\PostCategory',
'multiple' => false,
'by_reference' => false,
'query_builder' => $queryBuilder,
'position' => array('before' => 'name'),
'attr' => array(
'data-toggle'=>"tooltip",
'data-placement'=>"top",
'title'=>"Choisissez la catégorie dans laquelle publier le feedback",
)))
This first gave me errors when saving and I had to change the setter as following :
/**
* #param \AppBundle\Entity\Social\PostCategory $postCategories
*
* #return Post
*/
public function setPostCategories($postCategories)
{
if (is_array($postCategories) || $postCategories instanceof Collection)
{
/** #var PostCategory $postCategory */
foreach ($postCategories as $postCategory)
{
$this->addPostCategory($postCategory);
}
}
else
{
$this->addPostCategory($postCategories);
}
return $this;
}
/**
* Add postCategory
*
* #param \AppBundle\Entity\Social\PostCategory $postCategory
*
* #return Post
*/
public function addPostCategory(\AppBundle\Entity\Social\PostCategory $postCategory)
{
$postCategory->addPost($this);
$this->postCategories[] = $postCategory;
return $this;
}
/**
* Remove postCategory
*
* #param \AppBundle\Entity\Social\PostCategory $postCategory
*/
public function removePostCategory(\AppBundle\Entity\Social\PostCategory $postCategory)
{
$this->postCategories->removeElement($postCategory);
}
/**
* Get postCategories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPostCategories()
{
return $this->postCategories;
}
/**
* Constructor
* #param null $user
*/
public function __construct($user = null)
{
$this->postCategories = new \Doctrine\Common\Collections\ArrayCollection();
}
Now, when editing a post, I also have an issue because it uses a getter which ouputs a collection, not a single entity, and my category field is not filled correctly.
/**
* Get postCategories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPostCategories()
{
return $this->postCategories;
}
It's working if I set 'multiple' => true but I don't want this, I want the user to only select one category and I don't want to only constraint this with asserts.
Of course there are cases when I want to let the user select many fields so I want to keep the manyToMany relationship.
What can I do ?
If you want to set the multiple option to false when adding to a ManyToMany collection, you can use a "fake" property on the entity by creating a couple of new getters and setters, and updating your form-building code.
(Interestingly, I saw this problem on my project only after upgrading to Symfony 2.7, which is what forced me to devise this solution.)
Here's an example using your entities. The example assumes you want validation (as that's slightly complicated, so makes this answer hopefully more useful to others!)
Add the following to your Post class:
public function setSingleCategory(PostCategory $category = null)
{
// When binding invalid data, this may be null
// But it'll be caught later by the constraint set up in the form builder
// So that's okay!
if (!$category) {
return;
}
$this->postCategories->add($category);
}
// Which one should it use for pre-filling the form's default data?
// That's defined by this getter. I think you probably just want the first?
public function getSingleCategory()
{
return $this->postCategories->first();
}
And now change this line in your form:
->add('postCategories', 'entity', array(
to be
->add('singleCategory', 'entity', array(
'constraints' => [
new NotNull(),
],
i.e. we've changed the field it references, and also added some inline validation - you can't set up validation via annotations as there is no property called singleCategory on your class, only some methods using that phrase.
You can setup you form type to not to use PostCategory by reference (set by_reference option to false)
This will force symfony forms to use addPostCategory and removePostCategory instead of setPostCategories.
UPD
1) You are mixing working with plain array and ArrayCollection. Choose one strategy. Getter will always output an ArrayCollection, because it should do so. If you want to force it to be plain array add ->toArray() method to getter
2) Also I understand that choice with multiple=false return an entity, while multiple=true return array independend of mapped relation (*toMany, or *toOne). So just try to remove setter from class and use only adder and remover if you want similar behavior on different cases.
/** #var ArrayCollection|PostCategory[] */
private $postCategories;
public function __construct()
{
$this->postCategories = new ArrayCollection();
}
public function addPostCategory(PostCategory $postCategory)
{
if (!$this->postCategories->contains($postCategory) {
$postCategory->addPost($this);
$this->postCategories->add($postCategory);
}
}
public function removePostCategory(PostCategory $postCategory)
{
if ($this->postCategories->contains($postCategory) {
$postCategory->removePost($this);
$this->postCategories->add($postCategory);
}
}
/**
* #return ArrayCollection|PostCategory[]
*/
public function getPostCategories()
{
return $this->postCategories;
}
In my case, the reason was that Doctrine does not have relation One-To-Many, Unidirectional with Join Table. In Documentations example is show haw we can do this caind of relation by ManyToMany (adding flag unique=true on second column).
This way is ok but Form component mixes himself.
Solution is to change geters and seters in entity class... even those generated automatically.
Here is my case (I hope someone will need it). Assumption: classic One-To-Many relation, Unidirectional with Join Table
Entity class:
/**
* #ORM\ManyToMany(targetEntity="B2B\AdminBundle\Entity\DictionaryValues")
* #ORM\JoinTable(
* name="users_responsibility",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={#ORM\JoinColumn(name="responsibility_id", referencedColumnName="id", unique=true, onDelete="CASCADE")}
* )
*/
private $responsibility;
/**
* Constructor
*/
public function __construct()
{
$this->responsibility = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add responsibility
*
* #param \B2B\AdminBundle\Entity\DictionaryValues $responsibility
*
* #return User
*/
public function setResponsibility(\B2B\AdminBundle\Entity\DictionaryValues $responsibility = null)
{
if(count($this->responsibility) > 0){
foreach($this->responsibility as $item){
$this->removeResponsibility($item);
}
}
$this->responsibility[] = $responsibility;
return $this;
}
/**
* Remove responsibility
*
* #param \B2B\AdminBundle\Entity\DictionaryValues $responsibility
*/
public function removeResponsibility(\B2B\AdminBundle\Entity\DictionaryValues $responsibility)
{
$this->responsibility->removeElement($responsibility);
}
/**
* Get responsibility
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getResponsibility()
{
return $this->responsibility->first();
}
Form:
->add('responsibility', EntityType::class,
array(
'required' => false,
'label' => 'Obszar odpowiedzialności:',
'class' => DictionaryValues::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('n')
->where('n.parent = 2')
->orderBy('n.id', 'ASC');
},
'choice_label' => 'value',
'placeholder' => 'Wybierz',
'multiple' => false,
'constraints' => array(
new NotBlank()
)
)
)
I know its a pretty old question, but the problem is still valid today.
Using a simple inline data transformer did the trick for me.
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('profileTypes', EntityType::class, [
'multiple' => false,
'expanded' => true,
'class' => ProfileType::class,
]);
// data transformer so profileTypes does work with multiple => false
$builder->get('profileTypes')
->addModelTransformer(new CallbackTransformer(
// return first item from collection
fn ($data) => $data instanceof Collection && $data->count() ? $data->first() : $data,
// convert single ProfileType into collection
fn ($data) => $data && $data instanceof ProfileType ? new ArrayCollection([$data]) : $data
));
}
PS: Array functions are available in PHP 7.4 and above.
I'm struggling with a symfony2 form. Basically i would like to manage User's preference to receive (or not) an email for each type of action an User could do.
Here my schema :
User (extending FOSUB)
EmailUserPreference
class EmailUserPreference {
public function __construct(User $user, \Adibox\Bundle\ActionBundle\Entity\ActionType $actionType) {
$this->user = $user;
$this->actionType = $actionType;
$this->activated = true;
}
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Adibox\Bundle\UserBundle\Entity\User", inversedBy="id")
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="Adibox\Bundle\ActionBundle\Entity\ActionType", inversedBy="id")
*/
private $actionType;
/**
* #ORM\Column activated(type="boolean")
*/
private $activated;
/*getters / setters ... */
}
ActionType
class ActionType
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $value
*
* #ORM\Column(name="value", type="string", length=255)
*/
private $value;
/* and some others */
}
Here, i build my form EmailUserPreferenceType :
class EmailUserPreferenceType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('emailPreference', 'entity', array(
'class' => 'AdiboxActionBundle:ActionType',
'property' => 'value',
'expanded' => true,
'multiple' => true,
'query_builder' => function(\Adibox\Bundle\ActionBundle\Entity\ActionTypeRepository $er) {
return $er->getAllActionsWithPreferences();
}
));
}
public function getName() {
return 'emailUserPreference';
}
public function getDefaultOptions(array $options) {
return array('data_class' => 'Adibox\Bundle\UserBundle\Entity\EmailUserPreference');
}
}
And finally the ActionTypeRepository with the function called in the FormType :
class ActionTypeRepository extends EntityRepository {
public function getAllActionsWithPreferences() {
$arrayActionWithPreferences = array(
'share',
'refuse',
'validate',
'validatePayment',
'createPayment',
'estimateChangeState',
'comment',
'createRepetition',
'display',
'DAFLate',
);
$qb = $this->createQueryBuilder('a');
$qb->where($qb->expr()->in('a.value', $arrayActionWithPreferences));
return $qb;
}
}
At this point, I thought it was OK : i got a good rendering, with the right form. But in fact, each checkbox has the same form name than the other. In other words each time the form is submitted, it only send in post a $builderemailUserPreference[emailUserPreference][] data. Obviously, it does not work as i expected.
I show these posts
http://sf.khepin.com/2011/08/basic-usage-of-the-symfony2-collectiontype-form-field/
Here he's using a widget Collection. I'm not sure i should use it or entity (like i did). But what i can read from http://symfony.com/fr/doc/current/reference/forms/types/collection.html, it seems more like an embedding form than an entity.
And finally i saw this : symfony2 many-to-many form checkbox
This one is using (indeed) Collection and many-to-many relations. I read somewhere (can't find the link anymore) that i can't use it since i need to add some attributes to the relation (in this case bool activated). I'm pretty sure the solution is near the link above, but can't find the good way to reach it.
Thank you in advance.
Any advice on what i'm doing wrong or if i should use Collections instead of Entity would be appreciated.
I have two Doctrine entities that have a one-to-many relationship, like this:
License
class License {
/**
* Products this license contains
*
* #var \Doctrine\Common\Collections\ArrayCollection
* #ORM\OneToMany(targetEntity="LicenseProductRelation", mappedBy="license")
*/
private $productRelations;
}
LicenseProductRelation:
class LicenseProductRelation {
/**
* The License referenced by this relation
*
* #var \ISE\LicenseManagerBundle\Entity\License
* #ORM\Id
* #ORM\ManyToOne(targetEntity="License", inversedBy="productRelations")
* #ORM\JoinColumn(name="license_id", referencedColumnName="id", nullable=false)
*/
private $license;
}
And I have this form for the License entity:
class LicenseType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('productRelations', 'collection',
array('type' => new LicenseProductRelationType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label' => 'Produkte'));
}
}
And this form for the LicenseProductRelation entity:
class LicenseProductRelationType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
parent::buildForm($builder, $options);
$builder->add('license', 'hidden');
}
}
The forms and entities do of course contain other fields, not copied here to keep the post relatively short.
Now when I submit the form and bind the request to the form in my controller, I expect the call $license->getProductRelations() to return an array of LicenseProductRelation objects ($license is the entity passed in to the form, thus the object the request values are written to when I call $form->bindRequest()). Instead, it returns an array of arrays, the inner arrays containing the form field names and values.
Is this normal behaviour or did I make an error that somehow prevents the form component from understanding that License#productRelations shound be an array of LicenseProductRelation objects?
Because your LicenseProductRelationType is an embedded form to LicenseProductType you have to implement the getDefaultOptions method on LicenseProductRelationType and set the data_class to LicenseProductRelation (including its namespace).
See the documentation: http://symfony.com/doc/current/book/forms.html#creating-form-classes
and scroll down to the section entitled "Setting the data_class" - it points out for embedded forms that you need to set up the getDefaultOptions method.
Hope this helps.
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\TaskBundle\Entity\Task',
);
}
You have to use the entity type. This one is Doctrine enabled and gives you much love/power to handle collections of entities. Make sure to set "multiple" => true.