-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers.cfc
166 lines (113 loc) · 6.18 KB
/
helpers.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<cfcomponent>
<!--- These are reserved form control arguments that will not be treated as HTML attributes --->
<cfset variables.reserved_arguments = "field,label,required,value" />
<!----------------------------------------------------------------------------------------- textfield
Description: Outputs an <input type="text"> field
----------------------------------------------------------------------------------------------------->
<cffunction name="textfield" access="public" output="true">
<cfinvoke method="before" argumentcollection="#arguments#" />
<cfset attributes.set("value", object[arguments.field]) />
<cfset attributes.add("type", "text") />
<input #attributes.string()# />
<cfinvoke method="after" argumentcollection="#arguments#" />
</cffunction>
<!----------------------------------------------------------------------------------------- textarea
Description: Outputs a <textarea> field
----------------------------------------------------------------------------------------------------->
<cffunction name="textarea" access="public" output="true">
<cfinvoke method="before" argumentcollection="#arguments#" />
<textarea #attributes.string()#>#object[arguments.field]#</textarea>
<cfinvoke method="after" argumentcollection="#arguments#" />
</cffunction>
<!----------------------------------------------------------------------------------------- textarea
Description: Outputs a <select> field
----------------------------------------------------------------------------------------------------->
<cffunction name="select" access="public" output="true">
<cfinvoke method="before" argumentcollection="#arguments#" />
<select #attributes.string()#>
</cffunction>
<!----------------------------------------------------------------------------------------- textarea
Description: Outputs a <select> field
----------------------------------------------------------------------------------------------------->
<cffunction name="endselect" access="public" output="true">
</select>
<cfinvoke method="after" argumentcollection="#arguments#" />
</cffunction>
<!----------------------------------------------------------------------------------------- textarea
Description: Outputs a set of <option> tags
----------------------------------------------------------------------------------------------------->
<cffunction name="options" access="public" output="true">
<cfset var query = arguments.query />
<cfset var value = "" />
<cfset var display = "" />
<cfif structKeyExists(arguments, 'default')>
<option value="">#arguments.default#</option>
</cfif>
<cfloop query="query">
<cfset value = evaluate("query.#arguments.value#") />
<cfset display = evaluate("query.#arguments.display#") />
<option value="#value#">#display#</option>
</cfloop>
</cffunction>
<!------------------------------------------------------------------------------------------ before
Description: This function is called at the beginning of every form control.
----------------------------------------------------------------------------------------------------->
<cffunction name="before" access="private" returntype="void">
<cfset variables.object = request.data_object />
<!--- Display the label for the form field --->
<cfinvoke method="label" argumentcollection="#arguments#" />
<!--- Create an attributes object to store the HTML attributes for the form contol --->
<cfobject name="variables.attributes" component="supermodel2.attributes" />
<!--- Initialize the attributes with the passed-in arguments excluding the reserved ones --->
<cfset attributes.init(
argumentcollection = arguments,
reserved_arguments = variables.reserved_arguments) />
<!--- Add some default attributes if they aren't provided as arguments --->
<cfset attributes.set("id", arguments.field) /> <!--- ID MUST be the field name --->
<cfset attributes.add("name", arguments.field) />
<cfif structKeyExists(request.data_object.errors, arguments.field)>
<cfset attributes.add("class", "text invalid_field") />
<cfelse>
<cfset attributes.add("class", "text") />
</cfif>
</cffunction>
<!---------------------------------------------------------------------------------------- after
Description: This function gets called at the end of every form control
----------------------------------------------------------------------------------------------------->
<cffunction name="after" access="private" returntype="void">
<cfinvoke method="error" argumentcollection="#arguments#" />
<br />
</cffunction>
<!--------------------------------------------------------------------------------------- label
Description: Every form field has a corresponding <label> tag with the English description of the
field.
----------------------------------------------------------------------------------------------------->
<cffunction name="label" access="private" returntype="void">
<cfargument name="field" required="yes" />
<cfargument name="label" default="#arguments.field#" />
<cfargument name="required" default="true" />
<cfargument name="accesskey" default="" />
<cfset var pos = 0 />
<cfif accesskey NEQ "">
<cfset pos = findNoCase(accesskey, arguments.label) />
<cfset arguments.label = insert('</em>', arguments.label, pos) />
<cfset arguments.label = insert('<em class="accesskey">', arguments.label, pos - 1) />
</cfif>
<cfoutput>
<label for="#field#" <cfif arguments.required>class="required"</cfif>>
#arguments.label#:
</label>
</cfoutput>
</cffunction>
<!--------------------------------------------------------------------------------------- error
Description: Outputs an error for a given field of a model.
----------------------------------------------------------------------------------------------------->
<cffunction name="error" access="private" returntype="void">
<cfargument name="field" type="string" required="yes" />
<cfif structKeyExists(request.data_object.errors, arguments.field)>
<cfoutput>
<div id="error_#field#" class="error">#errors[arguments.field]#</div>
</cfoutput>
</cfif>
</cffunction>
</cfcomponent>