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();
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.