Add dynamic input boxes in a Symfony form - php

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.

Related

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

Symfony2 Advanced (Elasticsearch) Search Filter

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.

Symfony Forms - Is it possible to create a single entity from two forms?

I understand that you can embed forms within another form. I wish to make 2 base forms for a User entity, with the following fields:
UserCoreType: username, password, email
UserPersonalType: name, location
With this I'd like to make a registration form, that included both of those types, and I'd like to make another form to only update the personal info (i.e. not really a new form, just use the UserPersonalType).
How can I do this without it trying to make 2 separate user entities, one for each form? I need one entity to be made.
OR
How can I restrict certain form fields? If I were to make just a single UserType, how could I make it so that only the name and location were editable after the entity had already been made?
Just embed UserPersonalType into UserCoreType for registration form, and use UserPersonalType separately to update personal info.
UPDATE
You can also use single form for registration and updating info. Use EventListener on PRE_SET_DATA to make some of the fields readonly.

Prepopulating Yii form, from GET/POST

My problem is I have an HTML page that includes a short form on it. What I'd like is when this page posts/gets into my Yii model form, to be able to grab and pre-populate the empty form for the model with the values from the incoming form...
I dont think I can use the pagination widget because my initial page is HTML. Is there some way I can just pull this POST value in if it's set rather than the model.
Im still new to Yii so if this is a simple answer, I apologize
Edit: To clarify, my initial form has a few values from my main page form. The HTML page has 3 fields, say first name, last name, email address. That form then posts in to my full page form, which asks for additional information to complete the model. I'm hoping though that I can pre-populate the first and last name in the new php/yii form.
Basically I'm hoping when I first render _form.php I can grab a GET/POST value and assign it to the current model, or add it as a default value on the form.
"I'm hoping ... I can grab a GET/POST value and assign it to the current model"
You sure can. Do this in your controller and you'll be set:
$model->first_name = $_GET['first_name'];
Of course, you may also want to validate those values and set a model scenario to make sure you don't end up with bad data being passed in by an attacker. Otherwise you could run into XSS attacks. But the ability to directly assign model attributes is nice and powerful ...

Allow a User to Add Fields to a Form Created With Zend Framework

I'm using Zend Framework and I currently have an existing form using zend-form which functions as required.
I want to add another optional text field to it, but allow the user to choose to display the field and also display it multiple times. e.g. A user registration form with an 'alternative emails' area, allowing the user to add further text fields for each of their email addresses.
Unfortunately, I'm not sure how to go about this. I think sub-forms might be the way forward, but not too sure.
Ideally, once the form is submitted I'd want the data in an array so that I can process it for storing in a MySQL table afterwards.
See this excellent post by Jeremy Kendall on dynamically adding fields to a form:
Dynamically Adding Elements to Zend_Form
Upshot is to use jQuery on the client-side to add fields and maintain a registry of the new fields. Then on the server-side, call a new preValidate() method on the form object which checks the posted registry and adds the required fields into the $form object before standard processing - like isValid() and getValues() - is invoked.
Why don't you make an AJAX call to an addfieldAction() method in which you generate the HTML of another textfield and return that? You can still make use of Zend Form and the whole form can be processed the normal way.

Categories