symfony2 Submit multiple entities in a single form - forms

i'm using the last symfony version and i'm tring to submit multiple rows of the same entity using the same form.
table 'config' structure (it's key value) :
-----------------------------
| id | key_name | key_value |
-----------------------------
So i'd like to add/update many rows using the same form
any idea to do it ? i tried to follow this solution multiple rows in form for the same entity in symfony2 but it does't work for me

Use collection type. It will allow you to dynamicly add rows in form.

Related

Repeating table with new text field value

I currently have a form in InfoPath with a repeating table eg:
Author | Location | Book (repeating table)
What I am trying to achieve is:
Author | Location | Book (Title1),Book (Title2),Book (Title3) etc
I understand that this is a short explanation - so feel free to request further info, if needed.
The Book field writes the first entry, ignoring book2, book3 etc. I need the field to work like an array field1(book1),field2(Book2), etc. Everytime the text field is repeated a new field should be created, saving the value.
It sounds like you just need a nested repeating table. Here's an example with the nested table having just one column (you can delete the header row).

How to have additional fields in SQLFORM that are not in table in web2py?

I have a form for creating new records in a table generated using SQLFORM. One of the fields of the table will not be directly exposed to the user but instead created based on the values of a few other input elements which themselves are not part of the table.
How do I add additional fields to an SQLFORM that are not part of the table and do not need to be part of the database insert? And where would be the place to calculate the non-exposed field before inserting into the database?
(A hypothetical example would be having latitude and longitude coordinate fields in the table but no address field. In this example, would need address field on create form that could be used to geocode and store coordinates in lat/lng fields)
The way I solved this was by using SQLFORM.factory instead of SQLFORM.
1. I extracted the fields defined on my table
fields = [field for field in db.customer]
2. appended the new fields I wanted
fields += [
Field('customer_type','string', label=T('Customer Type'),
requires=IS_IN_SET(customer_types, zero=None, sort=False)),
Field('another_field','string', label=T('Another Field')),
]
3. used SQLFORM.factory to generate the form
form = SQLFORM.factory(
*fields,
formstyle='bootstrap',
_class='customer form-horizontal',
table_name='customer'
)
* note the asterisk before fields is necessary
4. still used form.process().accepted but then calculated what I needed to and then manually handled the database insert
if form.process().accepted:
...
form.vars.some_nonwritable_field = calculate_complicated_value()
db.customer.insert(**db.customer._filter_fields(form.vars))
...
_filter_fields prevents the dynamically added fields from being part of the insert.
This way I could add additional field to the form that were validated but not part of the insert.
In recent versions of web2py you can do it shorter:
form = SQLFORM.factory(
db.table,
Field('my_field_1'),
Field('my_field_2'),
)

Zend jquery form autocomplete popoulate data in 2 fields

I have Zend autocomplete field, there I can add City name, my question is how to populate data from the list in 2 field, in 1 value and in 2 (hidden) id;
$this->view->autocompleteElement = new ZendX_JQuery_Form_Element_AutoComplete('cmp_name');
$this->view->autocompleteElement->setJQueryParam('source','/client/search');
I think this is possible because I pass array
[{"id":"2","value":"2","label":"Vardenisas!!!!!"},{"id":"23","value":"23","label":"Vardas"},{"id":"32","value":"32","label":"aaaaaaa"}]
If someone know how to in one click pass in 1 filed label and in 2 (hidden) id ?

cakePHP single form, two tables, multiple records

There are two tables:
poducts [id, name, etc..] and specs [id, product_id, spec_name, spec_value],
I'm using a form to edit the product (/products/edit/332 for example)
In the form i wan to add (it's associated and i can access it in the view) the specs. which are a list of records from the specs table.
is it possible to create the specs as inputs in the same form?
also, i would like to enable the feature of "add new spec".
thanks
For saving related model data, you can use saveAll:
$this->Product->saveAll($this->request->data);
And your inputs in Product form:
echo $form->input('Spec.0.spec_name');
echo $form->input('Spec.0.spec_value');
If you need more inputs, just increase the 0 value.
More information: http://book.cakephp.org/2.0/en/models/saving-your-data.html

How to map user generated forms into a database?

I want users to be able to make forms with N fields, text, numbers, e-mail, combo, etc. How do i store each field without knowing how many of them will be?
The first way i thought of doing this is storing all the text fields in one table, all e-mail fields in another, etc. Then, in a table i store the form information with it's own ID.
How do i work around scalability in this form system? For example having multiple servers sharing forms.
Is my design good enough?
What do i need to store to transform then into html with XSLT?
Two solutions:
Create a clean, normalized database structure (avoid EAV Model)
Store meta data (like XML etc.) in the database
1. Solution:
forms table:
id | user_id | name
user_id: The "owner" of this form
name: The name of the form like "Registration" or whatever
form_fields table:
id | form_id | label | type | sorting | values | default
form_id is a foreign key to the forms table
label is the Label for the input like "Password"
type is the input type (like select, email, password, text), based on this you can handle the displaying / validation etc.
sorting gives you the ability to arrange fields in a form
values - predefined values example is a select box, store this as meta data like xml,json or whatever and parse it in your application, you can use the type field to determine how you want to handle these values
default - a default value (maybe a selected value in a select box or pre-defined text for a text input)
if someone fills out a form:
filled_forms table:
id | user_id | form_id
user_id: who filled the form?
form_id: which form?
filled_form_values:
id | filled_form_id | field_id | values
filled_form_id: foreign key to filled_forms table
field_id: the field id of the form
values: the entered values, maybe store as metadata like JSON, XML (example: a select box which allows multiple selections) you can join the form and the form field so you get the type of the field and then you can handle the values properly
Pros:
The database handles data integrity for you
Normalized database schema
Cons:
Static schema, multiple joins to retrieve fields
Goes in the direction of a EAV-Model
No direct transformation with XSLT possible
2. Solution:
You have only two tables:
forms table:
id | user_id | name | metadata
user_id: Owner of the form (foreign key)
name: Name of the form (like Registration etc.)
metadata: Your self-implemented description of the form (HTML,XML,JSON whatever) which describes the form properly, giving IDs to the elements etc.
filled_forms table:
id | form_id | user_id | values
form_id: Foreign-Key to the form
user_id: Foreign-Key to the user who filled out the form
values: Your self-implemented description of the filled out form (HTML, XML, JSON whatever) which describes the input and maps it to the fields correctly
Pros:
Very flexible, no EAV trap, simple structure
Direct transformation with XSLT possible
Cons:
You have to ensure data integrity by your own