Get All Form Fields in ModelGlue the Elegant Way
arguments.event.getValue("eventVariableName")
where "eventVariableName" is the name of the variable in the form/url scope. Simple enough. However, what happens if you have a form with 10 fields. You could recreate the above with 10 lines with a line for each variable in the form/url scope. Not nice and a bit of a pain. An alternative method would be to great a model cfc containing your getters/setters for each form field. Then using ModelGlue, you would call the "makeEventBean" function passing it the instance of your model and then use model.getName() to get the value of the "name" field. <cfset formModelCFC = component.createObject("component", "path.to.formModelCFC") />
<cfset arguments.event.makeEventBean(formModelCFC) />
<cfset name = formModelCFC.getName() />
If you are interested in traveling that route, the method is described in more detail on Dan Willson's blog under So you want to create a ModelGlue:Unity application? (Part 3) This method can also be a pain since you have to update your bean every time you add a new field to the form. After a short dicussion with Ray Camden (see below), there is a even easier way using a built in Model-Glue method (that I had initially overlooked). The code is pretty self-explanatory.
<cfset var eventValues = structnew() /> <!--- Get the form/url values ---> <cfset eventValues = arguments.event.getAllValues() />
- Login or register to post comments
- 508 reads
- Flag as offensive
- Printer-friendly version







Comments
Raymond Camden replied on Mon, 2008/02/18 - 1:36pm
I have a few small problems with this. First off - you mention that everytime you change your form you have to change your bean. Normally you change your model first. Ie, if I add a new column to my Foo type, I'd update my model CFCs, and then any views (including the form), that would take it.
Secondly, your use of getValue("fieldNames") is a mistake in my book. It ties your controller code to a HTTP Form. What if you switch to a Flex front end? MG already has a built in way to get all the values - event.getAllValues().This will return a struct of all values in the Event scope and is (imho) the "proper" way to do it.
Boyan Kostadinov replied on Mon, 2008/02/18 - 2:14pm
in response to: cfjedimaster
This has a specifc purpose and that is to get all field values the form. It does not take into account the tight coupleing of ModelGlue with http form as I am not conserned with resuing this code through Flex.
Second, getValue("fieldNames") is valid since it's in the event scope. Mistake or not, why would it be there if it shouldn't be? I agree that it could be improved by getAllValues() but that doesn't make this solution bad.
Raymond Camden replied on Mon, 2008/02/18 - 3:15pm
But if part of switcihng to MG is to remove dependancies on things like Form versus URL, why would you _add_ dependancies back in when you have a NON dependant way of getting the same info?
"Bad" may be too strong a word - but I do think it would be "better". ;)
Boyan Kostadinov replied on Tue, 2008/02/19 - 8:55am
in response to: cfjedimaster