How to handle autopopulated selects (from oneToMany relation) in Symfony form? - forms

I have a ProductCategory class that has a parent and some children, properly annotated using Doctrine.
Then I have a ProductCategoryType (form class) that I use to render the html form.
Doctrine does its magic and creates a select box for parent which consists of previously added categories.
My problem: How to I prepend a default option (say '0' => 'No parent category') and how do I remove a particular category from list (ex: the currently edited category, so user can't select the very category to be its own parent)?

This can easily be achieved by using DataTransformers.
You can find more information in the documentation chapter How to use Data Transformers.

Related

Could not get element id from s58c147845272f_products_2_suppliers_0_contacts Failing part: contacts sonata_admin symfony3

The Setup
I am working on a project using Symfony 3 and SonataAdminBundle 3.1.
I am using the nested form method of sonata admin where I have 4 entities:
Category, Product, Supplier, and Contact. They all have one-to-many relation with each other respectively.
I am using sonata_type_collection to put products in category form, and using the same to put suppliers in product form, and using the same to put contacts in the supplier form. The contact form has a sonata_type_model_list field for zipcodes.
The Problem
I am facing the following error when I click the add new contact button on the supplier form from within the category > product form.
Could not get element id from
s58c147845272f_products_2_suppliers_0_contacts Failing part: contacts
The error does not show up when I save the form step by step - like i add a product to the category form then save the form, then add a supplier to product and then save the form, and when i add the contact - the contact form is loaded and the above mentioned exception is not thrown. Also When I go directly to Supplier form and add the contact there, the exception is not thrown.
It is only when the supplier is not saved from within the category form that the above exception is thrown.
any help is highly appreciated.
Check if any of your properties have underscore in the name, like my_file. Try to change it into myFile and also accordingly change code in your Admin class:
$formMapper->add('my_file', 'file'); => $formMapper->add('myFile', 'file');.
Update:
I think I know what it is specifically - you probably have reference to your parent in your children. If that's the case, then add in your Admin classes for entities that have reference to the parents:
$formMapper->add('[parent_reference_attribute]', 'sonata_type_model_hidden');
Replacing [parent_reference_attribute] with name of your field referencing the parent.
Then exception should be gone, at least it was in my case.

How can I create a Symfony form base class for fields used on every entity?

I'm trying to come up with a way to create a 'base form' that contains fields used on every entity, i.e. id, created_at, updated_at, etc.
I want to either extend the base form when I create a new form or easily include it in each new form I make. I can't find much documentation on it, is this a common thing to do?

Symfony2: Collection of dropdown select lists for a many-to-many relationship

The objective:
Having a many-to-many relation be displayed as a dynamic list of select inputs(single choice dropdown list)
User arrives on page with a single select field (multiple = false) populated with persisted entities and add/remove buttons. By clicking the add button, a new select field with the same options appears below the first, which adds a new entry in the M2M relation. By clicking remove the field disappears and the entry should be removed.
The model:
Two entities: User & Manager. A User has exactly one "special" Manager and unlimited normal Managers.
Managers manage unlimited users.To model this I have created two relationships for which the user is the "owner" (not sure how to translate this)
ManyToOne specialManager
ManyToMany normalManagers
I haven't created a many to many relationship with attribute "special" because the requirement is exactly one special manager and I wasn't sure if Symfony/Doctrine would cause problems down the line.
What I have:
I can display a multiple select field with the existing entities using Entity field type, as per the documentation. Functionally this is what I need, visually it is not.
I can also use the Collection field type to display a single text field, and add or remove more with JS, as per the documentation. Visually this is what I need, but The text fields (entity attribute) need to be replaced by choice field.
The question:
Before I continue digging, is there a simple way to achieve this list of select tags?
For anyone else who may eventually need a dynamic list of select fields:
I initially solved this issue by detaching the field(s) in event listeners, and handling the display/submission manually in the controller.
However I wasn't satisfied with this clunky solution and when I encountered the same need I used a second solution: creating an intermediary entity xxxChoice (in this case ManagerChoice) which is Mto1 inversed related to User and Mto1 related to Manager. Then by creating a ManagerChoiceType form with "Manager" entity field type I was able to easily display my collection of dropdown select lists.

Best practise to hide related entity in a form

I have two entities called 'Member' and 'Role'. At the same page as I show the data of the 'Member' entity I would like to add a form for adding roles (one 'Role' per request) to this 'Member'.
Adding a new 'Role' entity includes the information of the related 'Member'. So the form must include a 'Member' entity to that handleRequest($request) works properly. What is the easiest way to hide this 'Member' entity in the form?
I have already googled a bit, but without satisfactorily results. I turned out following approaches:
using the symfony-generate-crud-default-settings: and
and hide them with css
using a hidden textfield with the ID
of the member Data Transformers to transform the hidden textfield
with a number back into a 'Member' entity
Option 1) looks more like a hack, while option 2) needs a lot of code for just a simple task. Is there no possibility to put the whole Member entity into a form component so that handleRequest($request) can handle the related entities properly?
If I understood you right, Member and Role entities has ManyToMany association. In this case you should use 'collection' form type.

Foreign entity in form into different kind of input

I have two entities: product and category (Symfony 2.3).
I want to create a form in which an user can choose a product by first selecting the category. A user selects the category by clicking on image, then I want to set the image's value into a hidden input, but I don't see how can I change a foreign entity choice list to a hidden input (http://symfony.com/doc/current/reference/forms/types/entity.html).
How can I accomplish this? (how to change form input to hidden)
If I set cascade validation to true, will it for example check if a category really exist. (To prevent putting products with non-existing category from malicious users) ?
Part 1
To do this you need to use a data transformer to do two things:
transform an entity into an identifier that is either a string or integer so a form can render it as a hidden field.
transform the string or integer identifier into the entity when the form is submitted so that the parent entity can be saved with the correct relationship
The symfony docs I linked to above (here too) actually walk though an entire example of using a data transformer with a form.
As a shameless plug (because I believe it is helpful) I have written a little tutorial on using a data transformer for a hidden field with an entity id: http://lrotherfield.com/blog/symfony2-forms-entity-as-hidden-field/
Part 2
If you are using the data transformer then you don't need to worry about malicious users. The data transformer will fail because it will not be able to reverse transform the category from the fake id. In my tutorial the transformer will throw a Symfony\Component\Form\Exception\TransformationFailedException exception.
You can also write a validator (potentially using a call back) if you wanted that checks that the submitted category is real if you want an error to show in the form. Doctrine wont allow you to persist a fake category relationship as the foreign key constraint will fail.