Symfony2 Advanced (Elasticsearch) Search Filter - php

I'm developing an application using Symfony2 and Elasticsearch. To communicate the ORM-way I use the Ongr ElasticsearchBundle (https://github.com/ongr-io/ElasticsearchBundle).
In my project I have a page which displays data in a table. The user should be able to filter this data. So I made a form (without entities) containing a formcollection. A FormType can be added (several times) to this collection using Javascript. This FormType contains a textfield and a dropdown. The dropdown is filled with all possible fields for the particular Elasticsearch document (comparable to a Doctrine entity). So, the user chooses a field from the dropdown and this field should match the value in the textfield.
This actually works, but I try to find a way to validate this. The value is filled in in a textfield, so the form is always valid. But some fields should be integers or dates.

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?

How to render form fields in Symfony dynamically (Forms generator)

Im writing simple forms generator where user can add various types of fields.
For example (text,text,email,file,checkbox).
Then i put data about those inputs to DB ( name, placeholder, default value etc).
How to generate symfony form without knowing what types of inputs and amount will it have?
Thanks for your help

Add dynamic input boxes in a Symfony form

What I want to do
I'm trying to build a form in Symfony 2.8 that requests the user's choice between many types of credentials and add an input box to the form it's similar to this one but I use a select option field instead of buttons.
What I did
I was able to create this form with HTML and JavaScript.The user can select more than one credential type only once except the OTV Option that can be selected without limits.
image here
What's my problem
I need a way to store user's data in a session and get them in my controller.
Is it possible to pass submitted data from HTML form to a Symfony controller and if not how can I build a similar form with Symfony's formBuilder ?
CollectionType is probably what you are looking for.
For your purposes, you can think of it as adding rows to the end of a two-column list. The Left Column has credential_type with Name, Email etc in a <select>; and the Right Column allows for your text values.
You'll have two FormTypes: one for the list, and another for the list item. In the list item FormType, you should be able to set a CallbackConstraint to validate e.g. that email values are valid upon form submission.
All user changes are saved at the same time, only when the form as valid.
I think you can do that. There is CollectionType in Symfony form. You can read about it here and how to implement it here
P.S. The main problem in this case is to generate specific names for dynamically created fields to provide a normal handling data in controller.

Symfony2 - numeric fields in form collection trigger database update

I have two Entities, called Bookings and Rooms. The Rooms are embedded as a collection in to the Booking Form. Everything works fine.
I then installed SimpleThings/EntityAuditBundle to version changes to the records. This all works fine and as expected except for one thing.
Say I have two Rooms defined in a Booking. I edit the Booking, but made zero changes to the form, and save it. Strange - the room_audit table now has a record saying I changed its values to one of the Room records via the embedded form collection etc. But I made no changes before saving.
Then via trial and error, I determine that if I remove the price form field from the RoomType embedded collection, which is a currency form type, then this additional audit record does not appear. If I change the field type of price to numeric then it appears again on saving. If I then change the field type to text then this additional record stops appearing.
SUMMARY
It seems that having a numeric field type such as a currency or a number in a embedded form collection is forcing the preUpdate lifecycle callback to be triggered even when there is nothing to update as no changes have been made. Change the field type to text and the preUpdate handler is not called unless there are values altered in the form.
Does anyone know why this behaviour seems to be based on using numeric form field types?
I'm using Symfony2.5.2 standard edition on MySQL with PHP 5.5.9
Thanks
I had a very similar issue when processing a form collection of entities that contained a text field. If I loaded the form to edit - but did not make any changes - it still schedule updates for all of the entities in the collection.
I discovered that in my Doctrine2 annotations - when defining the entity - the property was declared as an integer - not a varchar. When I changed the form field to integer - the form handler no longer attempted to update all of the entities in the collection.
So - perhaps you should look at your Doctrine2 annotations and make sure that the form field type matches your database schema.

Drupal 7: Field input generated by wizard

I'm new to Drupal, but not new to PHP/MySQL/etc...
Problem:
I have several forms that are completed by the user when performing various actions (upload, edit, create, etc). One of the fields contains project information that needs to be input in a specific manner. To prevent human error, I'd like the field to be presented as a guided input. I could create each of the required fields on every type of form, but it seems more efficient to create one dynamic field and reference it on other forms. Is there a straightforward method of accomplishing this? Would creating a custom field type that stores array-based values achieve this?
Desired outcome:
A modular way to add a field into applicable forms that guides user input.
I ended up solving this by creating a custom field using hook_field_widget_info() and hook_field_widget_view() to make a textarea. The guided input is returned to the textarea with JS as a JSON string (this is the field that stores in the database). The JSON string is processed in JS for display in the formatter.

Categories