Adding structure to form parameters.

Morning Guys,

Today I’ve been doing a bit of research into forms for multiple models. A challenge I’m sure we all come across on a regular basis - and it’s always a PITA.

The big challenge for me has always been relating form fields to the appropriate model, when standard HTML forms can only pass flat single-level structures to the server.

For instance - Let’s say I have a form for a new Company, and I also want to create a new Primary Contact for that company at the same time.

Chances are, both the Company and the PrimaryContact will have a property called ‘Name’, However, I can’t have two inputs with the same name, this means I’ll then use something like:

However, this then breaks my lovely populateModel() method as the input names submitted in the form don’t correspond to the names of the properties on the object.

During my research I came across some excellent behaviour in Rails which allows you, through a naming convention, to add structure to the submitted values when they appear in the request context.

I can then do something like this:

On the server side, the request context then has two keys in it, Company and Contact, both of which contain a structure of the related properties for the appropriate model. i.e. ‘Name’, ‘Email’ etc.

This can then be used to populate a model, with only the given properties, rather than the entire RC, perhaps with a new argument on populateModel() that allows us to select a specific sub-structure in the RC to use.

person = PersonService.new();
populateModel(model=person, rcsource=‘Contact’);

How would people feel about implementing something like this within ColdBox? I’m pretty sure it could be done without breaking any of the existing API.

Thanks.

Robert

I have to say I like this strategy, and had been looking for a nice solution for sometime now.

Grats on sharing this, because it’s simple and separates what I wanted separated.

Great! Glad you like the concept Andrew.

The rails implementation has a bunch of very clever stuff for dealing with arrays of the same type of objects too - such as a form that creates a new Company and adds multiple Contacts all at once.

So long as people are interested, maybe we should flesh out a spec and put some ideas forward to the actual implementation (something i’ve not considered yet)

Robert

Yeah I do, I had a form on my last project that had to be split across a few entities. This info would have been very handy then :slight_smile:

Do you have some links that I can read more up, rather me searching for it.

Sure - If you have time this RailsCast is a good explanation: http://railscasts.com/episodes/73-complex-forms-part-1

Docs on the subject are a little sparse, at least I’ve not been able to find much, but I don’t really know what best to search for.

Robert

Have you ever taken a look at Brian Kotek’s Form Utils CFC? It assists with the approach you mention (or at least something close to it).

Check it out:
http://www.briankotek.com/blog/index.cfm/2007/9/4/Implicit-Creation-of-Arrays-and-Structures-from-Form-Fields
http://formutils.riaforge.org/

Ah Dustin beat me to it :slight_smile:

To add to that it looks like Mr Whish was playing around with it as a ColdBox Interceptor a while back:-
http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/using-brian-kotek-s-formutilities-cfc-with-coldbox-3-244

Cheers,
James

+1

James,

That interceptor looks great. Very cool use of the form utils via an interceptor. I’m definitely going to have to give it a try.

Dustin

Fellas - this is great! thank you!

I will spend some time with the utils and the interceptor today - see how I get on.

Still think this could be an awesome bundle to be absorbed into the core.

Robert