Over a million developers have joined DZone.

Get All Form Fields in ModelGlue the Elegant Way

·
If you are using the ModelGlue framework for your ColdFusion development, you know how to get variable values from the form/url scope. Assuming you are inside a controller method that takes the argument "event" of type "ModelGlue.Core.Event", ModelGlue makes this as simple as
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() />
Topics:

Published at DZone with permission of Boyan Kostadinov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}