I need a symfony 2 example for drop down select box - php

I am new to symfony 2. I have a table categories and a table products. Each product is linked to a category with a categoryid field in product table. I want that the category select box should populate at product form inside a select box (combo box) and the list of categories should come from categories table. If i save the product form and re-open the form to edit then category should be auto-selected that i had saved during creating the new product.
I have really spent a lot of time to search such example but could find. Can someone please give me such example or link to such tutorial?
Thanks in advance.

You have to use Form Events. Here is an example:
http://showmethecode.es/php/symfony/symfony2-4-dependent-forms/

Entity Choices
you should use an Entity form type (it may be useful to override __toString of the category entity) or if you don't want to expose too much stuff you should use data transformers

Related

Symfony : Create severals fields from EntityType, like Collection Type

I have a form to create a Provider Entity.
The provider have a name and a list of products he can provide.
In the form, I want to show all the basics products (stored in DB), and so the user check the different products he can provide.
This is quite easy to do with the EntityType with multiple and expanded to true.
BUT i want also the user to add a description field of the selected product.
Example :
List of products : Vegetables, Meat, Honey...
The user check "Vegetables", and then a textfield appear so the user can write "carrots and salads".
I could create a Collection Type with a form entry where there is a select and a text field, but it will looks like :
select your product -- Description field ------------------------- Add new product
And I would prefer a multiple and extended list of all available products (with checkbox) and every time a checkbox is checked, a description fields appear at the bottom of the list.
The thing is I've done it quite easy with basic javascript and php, but I'm riting again my website using Symfony...
Thank for your help !

Select a category from a dropdown list -> print the subcategories of that category in a different list

I have 2 dropdown lists. In the first i have Parent Categories with Car Brands ( Seat,Audi etc). Each Category for example Seat has many subCategories that are the diffent models : Seat Cordoba,Seat Leon etc.
What i cant find out is, how i actually can pass the id of the Category selected in list 1 to server side so i Can make the query to db and parse the children of first category.
To be as clear as i can, Here is a more detailed example with picture from what im trying to do :
After the user selects "Audi" , need to get the value from the <option> and pass it somehow again at server-side to send a new query to the database,so i can create the second list also,with the models from Audi.
Is this even doable at all,to make the client-side communicate with server-side?
Or i need to find out a completly different approach?
Read this tutorial. It will help you solve your problem. It's Ajax + Php + MySQL
PHP Ajax and MySQL
You will require to pass the value of category drop down list.
For the same you have to use Ajax and call a php file/script which will populate the sub category according to the value received from the category drop down list.
Ajax is required for it.

How to manage other option in any listing?

I have one Category List user will select a category from the list if user didn't find a category in the list, then he will select "other" option and it will display a text box there user will enter a new category name. This newly added category will go for approval to Site Admin till then it should be mentioned as "Uncategorised".
So my question is how to achieve this using a mysql table should I create a new table as uncategorized category or should I add one extra column to category table as "isApproved". As the solution should be for both add and edit of new category.
As you described
Admin must approve newly added category.
To acheive that you definitely will have a field status or something like that to check if this category is approved or not. You can simply use that field. If it is not active, it is "Uncategorized.
A more flexible way than adding one isApproved (or rather a more generic name like status) column to your table is to create a whole new temporary table. There are number of reasons why this is the better approach:
You can save diagnostical information like who created this category, when did he create it, and so on.
You separate your logic: An unapproved entry is, simply put, a temporary one. Approving it is nothing more than moving it over to your categories table and thus making it permanent.
Your categories table doesn't get clustered with unnecessary entries.

Dependent Drop down in Grocery CRUD (CodeIgniter)

I stucked in a Problem When Playing with Drop Downs.I tried to use this
http://www.grocerycrud.com/forums/topic/1087-updated-24112012-dependent-dropdown-library/
But actually my requirement is quite different. I have a table fwld_products in which i am adding all other table's categories, from
fwld_cat_main (main Category's ID),
fwld_cat_sub1 (sub1 Category's id)
fwld_cat_sub2 (sub2 Category's id)
fwld_cat_sub3 (sub3 Category's id)
I want to Display Dropdown in such a way, when user Selects main
Category, the Drop Down Appear (sub1) Having Data related to main
category and when sub1 selected drop down appear (sub2) showing data
related to sub1, and sub2 selected and drop down appear(sub3) to show
data related to Drop down (sub2).
When submitted Finnally data inserted to [fwld_products].
Here I am attaching ERD, and result as well.
Please help
Hold on, it seems that DB structure of your categories table needs to be improved. What I suggest is that you follow footprints from some of the popular CMS like Opencart. It will give you a great sense to accomplish your task. You can easily optimize your DB by using just one "category" table (instead of main, sub1, sub2, and sub3 category tables) like this:
For category names, description, and meta keywords etc you can create this table "category_description":
Finally to assign categories to the products you can simply create another table "product_to_category":
In this way you can easily manage your data in DB and you can now easily tackle your situation using Codeigniter and Grocerycrud.
Try, Chained Selects Plugin for jQuery and Zepto (Github Project | Project Home)
If you use jquery or zepto in your project, this plugin will help you to solve your problem, Specially the remote version. You can create related select boxes easily.
Hope this helps :)

How to display all taxonomy term fields when adding node in Drupal

I'm trying to make form to post advert in newspaper and want to let user select style of future post and show how it will be printed. So i create taxonomy vocab. "style" with name, sample image and max. characters to this type.
Next i'm creating field taxonomy term relation in advert content type.
Now i need to show in adding form not just radios or select with titles but title, sample image and other fields from taxonomy.
I think i need to use hook_form_alter so i use mymodule_form_ad_node_form_alter(&$form, &$form_state, $form_id) but what i can't understand is how in drupal way i can get rest of taxonomy fields? In $form array is only tids and titles.
Is it possible not to query DB again but setup existing query to retrieve all fields i need?
Also how i can insert that additional information into form? Is it other way than #suffix #prefix on radios?
If this is a Drupal 7 then you can use an Entity Reference field along with the Entity Reference view widget instead of a term reference field. That will allow you to specify a view to use to populate the field options, and you can create an entity reference view that display the additional taxonomy term fields you want to display.

Categories