So I'm trying to create a base form with Zend_Form, which I'm going to somehow map to a model.
The thing is that some properties of the model can have multiple values, for instance tags. One piece of content can have unlimited tags. How can I reflect this in Zend_Form. Would some kind of a JS-PHP combo be possible here? (I want people to have a plus sign which lets the add another textbox for a new tag)
Use subform for the tags
Use js to clone html
Once form is submitted count number of tags, and adjust form config before validation
Related
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.
I am looking at an entity called content where I will store data about the content (textfield1, textfield2, bgcolor etc.) in an array inside the entity content (if this is not the best way to go please advice me).
The reason I want to put it in an array instead of just making separate entity fields for it is that I will have different 'content templates' so the amount and type of data fields will be different for each template (and each template of course has its own formbuilder; ContentXType.php, contentYType.php etc.). While one content type might have only one textfield, another might have 10.
I initially started on a design with datafield1, datafield2 etc. but realized that will leave me with a bunch of nullvalues and won't really be pretty =)
At savetime I will generate an html output for this content in a different field called contentRendered.
At edit time I will again want to be able to open up the different datafields from my array in different form widgets, so for example textfield1 in a textfield, textfield2 in a textarea, and bgcolor (the third value of my array) in a colorpicker form widget (I guess I will use a textfield with a jQuery color picker widget).
So my little issue is whether entity type array is the best way to go for this, can I even from within my form builder pick out value 1 from the array and put that in one field, value 2 in a different form field etc.?
Or do I need to go with say a new entity called content_data and use relations?
Or would a better way to go be to define a new entity for each kind of content I plan on using, and then embedding a form for that content type in my main content form?
After some more research I will define a new entity for template in which I will specify my templates, and have different twig files for the rendering of each template.
The actual data for each template, which will be of variable amount of fields, will be stored in a serialized array.
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.
I'm working with Zend Form and I need to dynamically (when filling out the form by user) add groups of elements.
For example I have form with few fields describing offer and one subform to set offer price. But offer can have more than one price and price is not only 1 element, its composed of
offer regular price
offer discount price
offer items count
So there are 3 different elements in one gruop.
I can create elements with javascript but when should I add them to Zend Form Object?
you could make each of the groupings a subform that way when you add via javascript, you could ajax that portion of the form in by just rending the subform with a 'Belongsto'. Then, you could read in your post when it comes back to you and do a foreach on it and add the elements back that way.
check out http://www.stephenrhoades.com/?p=364
Otherwise, in your ajax to get the form markup, you could be building a form object that you could save to the session, that way it will already be built at posting time.
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.