naming form files using square brackets - asp.net-mvc

I've noticed in some php applications, that form fields are labelled with [] in them.
Say a shopping cart page, that lists all the items where you can edit the quantity.
is the [] type naming used to get the correct row?
Just trying to figure out how I should name each textbox?
should it be like:
name="quantity-<%= items.RowId %>"
THen when I loop the form fields, I would get the index number?

This is done in PHP when you need to pass <select multiple="multiple"> to the server, otherwise PHP will catch only the first selected value.
Textboxes don't normally require such naming.

This might answer a few of your questions. Here, Phil Haack uses square brackets in order to bind to a model that contains a list.

Related

Rails form changing number of fields

In rails, I have a UserOffer model, that has_many Steps.
When I create a new UserOffer instance, I want that form to include a dropdown that says “select number of steps of that UserOffer”. Based on the number of steps selected (n), I want the form to expand, and include n extra fields, called “step 1 title”, “step 2 title” … “step n title”.
I figured I need to use nested attributed to do this, but I wanted to know how the form would look like, preferably without using JS or Ajax (just with RoR).
I would appreciate any help
If you want to do that, at the same form without refreshing the page, you will need to use an ajax request to the server, in order to add the steps to the UserOffer model. Remember that RoR only let you do things on the render step of the DOM, in other words, if you had rendered the form you will not be able to do another changes on the page without use javascript.

How to insert a page break manually in Rails?

In my project, an Article has many Items within it. Since each Item has different length, so I would like to implement pagination manually, for example, by creating a PageBreakItem model, in order to allow users insert page breaks wherever they want. But I don't know how to use "page" parameter in controller to render views correctly.
Some gems like kaminari or will_paginate only allow me to configure the number of items per page. They don't have options for inserting page breaks manually.
Any suggestions are greatly appreciated.
You don't need a special model for this. You could do this with small adaptation of your Item model:
Add sort_order numeric field to denote order of items within the article and is_on_new_page boolean field to denote a page break occurring before that article.

Inserting form data into SQLite table using Rails

I have created the table 'kids' which will store names as strings and numbers as integers. Inserting the values manually using an SQL query in the terminal works fine.
I am new to Rails and cannot seem to find any simple examples that show how to grab data from an html form and insert it into my SQLite table.
Here is the form I am dealing with:
<form>
Kid #1 Name:<input type="text"><br>
Favorite Number:<input type="text"><br>
Kid #2 Name:<input type="text"><br>
Favorite Number:<input type="text"><br>
<input type="submit" value="Submit">
</form>
What would the Controllers, Models, and Views look like in order to insert data from the form? It is 2 inserts into the table, but 1 form.
Create a method in your controller for making entries into the DB.
Make a method, say add_kids_data in your KidsController. So your URL for form will be /kids/add_kids_data.
Give names to your html text boxes appropriately. Say you name the boxes kid1, num1, kid2, num2, the values will be accessible in the controller as params[:kid1] and so on.
You would want to create a method in you model that will actually create an entry in your DB. You can then simply call the method in your model from the controller with the params that you received from the form.
This RailsGuides post on form helpers will give you step-by-step detail of what you need to do to create a form.

How do I get Grails g:select with multiple-selection with all selections when returning from the controller

I have a page that is a report from a database and I'm working on modifying how the filtering works. The intention is to allow the user to select possible values form a list that will be used to filter the resulting report. There are too many values to do this with checkboxes. I'm defining a multiple selection list box with this:
<g:select name="country" from="${countryDataList.KOUNTRY}" value="${params.country}" multiple="true" />
countryDataList is a List<> of objects with a name and a value which I create in the controller. I'm able to get the selected counties and process them without an issue.
But when the page returns from the controller with the filtered report, only the first selection in the list is selected. It doesn't re-select all of the items that the user selected. I am passing the params.country object back from the controller as
country:params.country
I saw some posts about this not working, but they are all from several years ago. Am I missing a vital step?
Ahh sorry, I was reading it on the phone initially and missed the point.
So what you want is a way of sending a multiple select box to a confirmation page. If I understand correctly?
Anyways how many objects in the select are we talking massive or a dozen couple of dozen or so ?
What I did was use check boxes and did a confirmation which shows the selection ticked in check boxes.. So this is the confirmation page that loads in https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListEmail/confirmcontact.gsp
this page which is where multiple attachments selected from the schedule re-appear...
https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListAttachments/_mailerAttachmentsDisplay.gsp.
Please note advice below is all conceptual stuff and there may be easier ways than this
Other than that You could create a taglib call on the confirmation page https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/taglib/ajaxdependancyselection/AutoCompleteTagLib.groovy#L55 which takes in your arrayList you could probably convert it to JSON pass it into the javascript that you load in within the taglib (on mine further down it loads this page in)
https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/views/autoComplete/_selectJs1.gsp#L23
and look to reselect them using javascript... as I say I haven't tested the last bit, the first bit i.e. checkbox works it is/has been in use.
Years later from you I just had the same problem. What I figured out is: it happens when params.country is an array instead of a Collection (i.e. an ArrayList).
A workaround for this if you want to stick to the array type is at the value attribute of the tag doing this: params.country?.findAll().

What is the best way to handle repeating forms in MVC?

The best public example that I can think of off the top of my head would be the amazon shopping cart. Where you have a page that displays multiple distinct records that can have multiple distinct fields updated.
I can't put each one in a form tag because the user may modify more than one record and then submit.
I can't just update all the records that I get back because:
1. Performance
2. Auditing
3. If someone changed the record that the user 'didn't change' when they were viewing the page and then the user submits those changes would be overwritten.
So how to best handle getting the data back and then getting which records where changed out of that?
Is that clear?
Use binding! Don't be iterating the form collection in your actions.
Steve Sanderson wrote a blog post about how to do it. I wrote a blog post on how to do it with MvcContrib.FluentHtml. Both posts are very detailed and include downloadable code.
Generate your form in a repeater, and append an ID to the form elements that increments with each new form. Save the number of repeated form elements in a hidden field. Then in your controller, read the value of this hidden field - that'll be the number of forms to read. Then, in a loop, retrieve each form's fields by specifying the name of the field, plus the loop index appended to the name, as the key.
You can use some javascript logic to detect when a form's value changes, and update a hidden field in that form's section if that occurs; or you can hide the original values inside a hidden field with each form section (although I don't recommend this as too many fields / forms will bloat your page).
one (but not necessarily the best) approach is to store which items are changed in a js-variable or something on the client side as they are changed, and then only send the data that is actually different from what the user recieved.
and as Erik stated, you could use hidden form elements to make sure that it works without js as well.

Resources