How to create a site-search in Symfony2? - php

I started creating own Symfony bundles some weeks ago. I've created some entities with all the stuff around (also used the Doctrine CRUD-command), made some twig extensions and created new templates.
Now I wanted to implement a Search Form in my Project. Are there some simlpe ways or some predefined Search-Bundles, which I can use in my Project?
I created my search-form manually (form without an entity). Then form is submitted via GET and the Results (selcted from the Entities in the Controller with a repository-search) are shown below the search form on the same page.
Now I think, that it isn't good to have a very long URL, because of the Fields in the Form. I could go further and submit e.g. a <select> list with multiple entries selected and the URL would even be longer.
Would it be a good choice to submit the Form via POST, "cut" the unnecessary "URL-Array-Elements" (+make the Query-Items more readable for humans) and redirect to the URL with the queries for showing the results?
Edited: Are there any simple ways, how I can create a specific search form or entity-filter which I can use for custom entities? I like to use/create a function, where I can insert the entity, define the search-tags/filters and output a search-form? Has anyone created/used such search/filter-functions?

Related

Symfony : how to "dynamically" generate a (big) form based on another (big) form?

I'm building a website that needs to have public forms where the fields are not statics, and based on the result of a form in the back office. So the only purpose of the back-office form is to specify which fields will be displayed in the related front-end form.
For example, imagine that i have an "Events" entity, with a related "eventsType". This form contains fields like a choiceType "activities" and so on, and is used in the back-office by the admin for creating an event. When this form is submitted, a new event is created.
The page displaying the new event also display a form where each fields is created dynamically based on this "events" data.
This new form is bound to another entity, let's say "Attendees" and people can use this form to register to the event, submitting all the data specifics to this event (using the dynamically generated fields).
For now the only solution i have in mind for that is using the form events, but is there another way? I know there is some bundles out there for related problems like multi-step forms, but is there a bundle which could be used in my case?

Multiple views for the same form in a web application

I have a situation where I have several ways to perform the same activity in my php web app.
There is the "Manage Widgets" section of the app which has a form for creating new widgets and a list of existing widgets.
Then somewhere else in the app there is a button that pops up a dialog to add a new widget.
Then on the home page of the app there is another place where a form is embedded to add a widget (think home page portal).
My question is: What is the best practice for this? In this case all of the forms will be essentially the same. So my first instinct is to use the same code for all three of these scenarios. On the other hand, space on the home page could be smaller and layouts may have to differ between the three.
So even though it would be repetition, is it better to duplicate this form 3 times (there is a proper model layer, so the duplicated code would not include the logic to add/edit the widget)? Or try to force a single view in all of these scenarios? Both seem wrong to me and I am hoping for some ideas to discover some sort of middle ground.
One approach would be to have the markup (not the styles) for the form as a standalone file, which can then be included from anywhere you like.
You can then use AJAX to submit the form to a specific PHP script that handles the form submission and returns a meaningful JSON response. You can then display and style this JSON response on the page in question.
This way you have a single form (that can be styled differently) and a single handler for any view that's required to use the form.
i mean, the best way is compose form from other forms (Dont repeat yourself). You can use different templates for same form to change appearance of final form.
For example/idea you can check forms what is used in Nette Framework (http://doc.nette.org/en/forms)
T.
If you are just changing the styles, not the markup, I think the best approach is to add a specific class to the form element and then use Javascript (not Ajax, justa Javascript) to alternate between these clases as you need.
If your application do not use Ajax at all and you just generate web pages with PHP, is a simple matter of decide which class you form shoud have.
In CSS, you do something like this:
form.main { ... }
/* main form rules */
form.other { ... }
/* other form rules */

How to build this type of simple but complex form with symfony forms

I have tried a lot and have not yet able to find solution to this problem. i am not sure if thats possible or not.
I have one table called Student then table called Assigment and Solutions
Now there are questions Stored in Assignment Table which are common to all students. But different student will submit different solutions.
On the form i want to have label as Question text and then text box as solution to that question.
IN the beginning i have the empty answers in database.
I am confused how can i use symfony to build this form
Becasue all solutions will be submiited togetehr with one submit button
Class AssignmentFormType{
$builder-> add('answer','collection' ....)
This is working fine but only thing here i want is to display that QuestionText as label for that Answer and i am not able to find that for 1 week
What you are trying to do is to dynamically create a form element based on information from your entity (i.e. setting label = $entity->getQuestion)
Two approaches:
A. Dynamic forms
http://symfony.com/doc/master/cookbook/form/dynamic_form_generation.html
Basically involves setting up a listener then using it to create your form element with access to each individual entity. Bit of a pain but it works.
B. Inside of the form template
Since all you want to do is to change the label then it might be easier to just set it inside of your form template. Something like:
{{ form_label(form.assignment, form.assignment.vars.value.question) }}
The .data should give you the actual assignment entity from which you can pull the question.

Using Drupal 6's form ahah to have one dropdown field update the values of a second dropdown field

I am working on a pair of form fields the directly effect one another. Specifically the problem I am dealing with is that we have thousands of groups users can join on this website that I have been working on. We are creating some administrative analytics forms for the site. Part of those forms is filtering by group.
The problem is that most of the people don't know the name of a group off the top of their heads, only what it starts with. So instead of using auto-complete we came up with two dropdowns. The first contains the options A through Z, letters of the alphabet which the group names begin with. The second dropdown contains all of the groups beginning with the selected letter.
The concept is simple enough and we were able to implement the AJAX pretty easy with jQuery. But this causes validation errors. After some research I found that this has to do with Drupal form caching. The solution is to use the ahah functions.
The problem with that is that all the instructions I have found so far deal with ahah actions for the specified field updating itself, not another field. So I am trying to figure out how to get the list of letters to update the group list using ahah and registering the new values with the form cache.
One solution I found was to simply load all the values on the page in a hidden dropdown and then just load up those in the set in the visible one as needed. But that creates a bunch of problems because the client uses almost exclusively IE and we have seen IE choke when given huge lists like that. Especially with the lists stored in JavaScript. So we need a light-weight solution.
Any thoughts? Ideas?
In a nutshell AHAH works like this:
Form gets generated with an empty $form_state
When an element that has the #ahah property gets changed an ajax call is made to the url specified as path in the #ahah property.
The callback at that url does this:
Retrieves the form from the form cache
Processes the form
Rebuilds the form with a current $form_state so that it can add / change elements based on that
The form is put in the form cache to avoid validation errors
The form is rendered and returned as JSON
Drupal replaces the contents of the wrapper div with the new form.
In your case you'll need to make your form generation function aware of the values selected in $form_state. It would have to:
Generate the letters drop-down
Look into $form_state and see if there is a letter selected. If so, generate the second drop-down based on the selected value.
The callback function would implement the form caching and rebuilding.
There are good explanations with code for the callback function here:
http://drupal.org/node/348475
http://drupal.org/node/331941
I actually got the answer on another StackExchange site. His suggestion to use the example module to come up with the solution worked very well.
Solution found here: https://drupal.stackexchange.com/questions/35048/using-drupal-6s-form-ahah-to-have-one-dropdown-field-update-the-values-of-a-sec

Can I use dynamically created form fields with the Security Component in CakePHP 1.3?

Using CakePHP 1.3, I have a (working) form that has dynamically created form fields (via Javascript). Everything works great, multiple models are saved via saveAll(), and it's just beautiful.
But, I get black-holed to a 404 whenever I enable the Security component (hoping to get some of the auto-magic CSRF protection).
I understand that this may be (probably is!) caused by the dynamically created form fields, as mentioned in the docs.
Is there a way to get them to play nicely together?
You can't have your Cake and eat it, too. (Cha-ching!)
CSRF protection means precisely that only a certain list of form fields is allowed to be submitted. This list is decided upon and fixed at the time the form is created. You can't be CSRF protected and dynamically alter the fields in the form.
There are two solutions:
If the number and names of the dynamically created fields are limited, create them all in the form and hide them using CSS, then show them using Javascript. This way you're not dynamically creating the fields, but are only dynamically showing them.
If that doesn't work, you can either whitelist the fields using the $disabledFields option (again, only if their names are known in advance) or disable CSRF altogether with the $validatePost option.

Categories