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.
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 new to Laravel and trying to get my head around how to allow a user to repeat a field.
I have a form that allows a user to create a basic page which has a title and description, I'd like to allow the user to then if they choose create another title and description field using a "Add more" button.
They allow them to re-order these field collections.
Cheers,
Chris
This seems to be less about Laravel and more about general DB Schema, PHP and UI practices. So due to the broad spectrum of answers this requires, I will answer mostly using best practice concepts to avoid being overly wordy (using Laravel for context).
Front-end
For the ability to add another set of fields on the fly, you will want to use a form array:
<div class="field-set">
{{ Form::text('fields[0]["title"]', Input::old('fields[0]["title"]')) }}
{{ Form::textarea('fields[0]["description"]', Input::old('fields[0]["description"]')) }}
</div>
+ Add more
Your 'add-more-link' should have a javascript trigger that will duplicate and append the 'field-set' element with an iteration of the index in the form array
(e.g. fields[1]["title"], fields[2]["title"])
Note: You can do this without JS, but you will have to repost the page with some parameter that increments the amount of fields to create and then loop through that many times while rendering the field sets on DOM generation.
To allow for reordering, I recommend jQuery UI's Sortable. Note: You can accomplish this without JS too by POSTing up or down increments for each field set, but more processing would need to occur in the back-end.
Back-end
In the model where you handle the save, grabbing the data via Input::get('fields') will store it into a PHP array that you can iterate through to validate, parse and store the data.
As for your DB schema, you will need to account for the creation of any number of new fields. This means that will likely need a separate table that will be joined on users.
(Columns: id, user_id, title, description)
In Laravel you can define a Has Many Relationship on the users model that will grab all the titles and descriptions for any given user.
http://laravel.com/docs/eloquent#relationships
I'm not sure you are using forms correctly, although its a little hard to tell from you question.
If you want to create a new resource (with a title and description) you dont add new form fields. You can have an 'add more' button, but this button just spawns another instance of your form which can then be posted.
But to be honest, you question is so vaugue its hard to tell what your problem is...
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
I have been toiling with this issue for the past 2 days now, and I can't seem to get some functionality working on a customization for a WordPress plugin. I am using latest versions of both JigoShop and WordPress.
URL to project: http://customcasing.ca/product/ipad-case/
Current Functionality
I have 5 product SKUs on the homepage and when clicked lead to their respective product order pages. On these respective pages are a design canvas which I have implemented fabricjs to handle image manipulation. When the user is satisfied, they save the canvas, which then uses the following example code to serialize the image into a JSON object:
saving/loading canvas with JSON
The JSON object is then set as the value of a hidden input within the "Add to cart" form. Then they would be able to choose product color variations, via dropdown boxes.
Now here is where things fall apart
Submitting the "Add to cart" form is supposed to run through a number of classes and post the hidden JSON object value to an aggregated "cart" object. This is called on various pages and a key/value pair is associated with the various attributes of the product. However, all the other key/value pairs are being executed and echo'd on a subsequent order review page...except my custom variable.
Here is the directory architecture
/wp-content
...
/plugins
...
/jigoshop
jigoshop_template_functions.php
jigoshop_actions.php
/classes
jigoshop_cart.class.php
/themes
/customcasing
Here are the code blocks associated with the above structure (pastebin)
jigoshop_template_functions.php
html generator and form
jigoshop_actions.php
form $_post for hidden field
classes/jigoshop_cart.class.php
form validation and key/value pair creation
I have given as much information as I possibly can about my current conditions, if there were to be anything of help on stackoverflow it would be the following Q/A:
Trying to send a hidden input value to next page
If you need any additional information please feel free to ask and I will do my best to elaborate. Also if you need a wider scope of the code I can provide that as well. I would really appreciate any nudge in the right direction.
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.