Welcome Guest, Not a member yet? Register   Sign In
Multiple forms and sub-tables in a single view
#1

Let me start by saying this question isn't about how to have multiple forms in a single view, nor is it about how to validate multiple forms via a single controller.  I already know how to do that.

The question is a bit more complex so I've put some visual examples together.

Let's say I have a model called "order" which in the database would consist of an `orders` table.  Orders would also contain items so there would be a sub table called `order_items` which contain the items for an order, with a foreign key back to the `orders` table.

Keeping it simple, if an order consists of a customer name and email address I could have a view like this:

[Image: screen_1.jpg]

Then, to add items to an order, I could have a separate controller and view which looks like this:

[Image: screen_2.jpg]

This works fine, but it's cumbersome to have to switch between two pages/views to see data from the two different tables.

Another option is to have two forms in one view, like this:

[Image: screen_3.jpg]

These two forms can be linked to the same controller, or to two separate controllers (which call the same view), it doesn't really matter.  However the issue with this is that it's unclear whether after clicking on "Add Product" you also have to click on "Save Details" (as it happens, you don't, but it's not clear).  Furthermore, if you change the customer's name and don't click on "Save Details" but instead click on "Add Product" the customer's name change would be lost.  So the whole thing becomes ambiguous from a usability point of view.

I guess what I want in an ideal world is this:

[Image: screen_4.jpg]

I think this makes the most sense from a user's perspective - there is a single "Save" button and if you don't click it your changes don't get saved (including the addition of items). But I don't know how to implement this.

At the moment adding an item to an order physically adds it to the `order_items` table in the database.  The order model calculates things like tax and shipping at the time of adding items to the `order_items` table, as well as calling some database triggers.  So I can't just add a "faux" item to the html view prior to clicking on "Save Details" as the tax and shipping (as well as various other things that I've omitted from this example) would be unknown.

I guess what I am looking for is some way of inserting items into the `order_items` table temporarily but then rolling those changes back if the "Save Order" button isn't clicked and the changes aren't committed.

Does anyone have any advice as to the best way to handle this?
Reply


Messages In This Thread
Multiple forms and sub-tables in a single view - by CINewb - 10-28-2017, 05:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB