Repeating form fields on a form - php

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...

Related

Passing a whole table to a blade

I have a table named tags which contains thousands of tags, and increasing. While creating a new post (via newPost blade), I want to pass all tags from controller into this blade and populate them in a dropdown list (with typing functionality) to help the user adding tags. I feel this will be a lot of data to be sent with every post creation, which would affect performance. Am I right ? Any suggestions?

How to build this type of simple but complex form with symfony forms

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.

Search field within a form and then do something with all the chosen results

I would like to offer the ability for users to open a form built using PHP/HTML and then on that form have a box which allows them to search for employees then want to send the form to without having to submit the form at this stage.
They will get a search box which allows them to enter the name or part name, the query then finds all employees which match the input string and they then choose the correct employee and that respondent is added to a list and they then search for the next employee and so on until they have added as few or many as they like.
Once they have selected the names, they then fill in some other standard fields on the form and then a separate entry is created in the "forms" table for each request sent.
Happy how to do the last part in terms of entering it into the database etc and how to do a "normal" search in PHP/MySQL but what should I do for the search within a form and add to the list pre submitting?
the technique you are looking for is AJAX which uses javascript to examine the form field as they type, talk to the php, get data, then show it back to the user without having to reload the page or sumbit the form. The exact thing you describe is called auto complete which there are many prebuilt tools for (or you can build your own custom one with some js knowledge). I am not going to try to post all the code here since it would be rather long and involved but if you search around these terms you will surely find what you need.
This can all be achieved with Javascript.
If you have a small number of employees, you could send an array of employees with the page. Use an 'onchange' event in the form field so that with each keypress you run a lookup matching the employees against the typed text.
It's likely that you don't have a small enough number of employees, and also I'm not sure best practice advocates sending your entire company address book inside a web page! In that case you will need to do the same using Javascript ajax calls within the page. On each keypress, fire a request back to the server asking for employees matching the text string.
You will need to dynamically display the matching employees in another form widget that allows for selection, or maybe as text with checkboxes etc.
There are plugins that manage all the client side part of this but you will still have to put together your own web service to do the employee lookup.

JQuery Dynamic Table

I have a form with a dynamic table on it plus other fields. You start with one row and can add more at execution time. It's used to track incoming and outgoing items on a store. You add each item and then fill some common fields (time, date, person who received them, etc).
General fields are stored in a table, detail ones in another table which is related with ID field of the first table. I send first the general fields with jquery ajax, save them on db and then use the ID for saving the details with other ajax call. When finished saving, I clean the inputs with jquery and if I add another registry the previous items are also added unless I reload the whole page. I use mysql+php+jquery 1.7.1
How do I clean the Array? or what is the best method for doing this?
Tell me if you need some code, thanks in advance...

data that non changing frequently in drupal - how to do it right?

i got a form (using form api and drupal_Get form ) and i got a form with 50 fields and some of them are non frequently chaning data like hobbies, year of birth , city/country etc...
i currently do just them in array in file and put them in #options=>$array ... is that the fastest way to save resources? or should i use taxonomy/variable_set/database query/block/node or whatever?
variable_get/variable_Set : Is generally used to store admin preferences for the site. If these are user specific I don't think it's a good idea to save them using variable_set
taxonomy : Can also be used for populating the values of
drop-downs, that is for options in the listing. In your case create a vocabulary
called hobbies and add
swimming,reading,drupaling as terms.
Then use the content-taxonomy
to use taxonomy terms as drop downs using CCK moudule.
nodes : Anything that is user
specific should go in as nodes. And
any content that can be created by
the users should also go in as nodes.
For administering forms you can use variable_set/variable_get.
But for your task it seems like that users enter their bio, so in this case you should store each form submission to new "row", as you do now for file (wonder why you don't use DB storing).

Categories