1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Helpers
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
22 /// Helper that provides client-side validation.
24 /// <remarks>The javascript core lib is extension of Peter Bailey's
25 /// fValidate(http://www.peterbailey.net/fValidate/).</remarks>
26 public class ValidationHelper
: AbstractHelper
28 private IDictionary _submitOptions
;
33 public ValidationHelper()
35 _submitOptions
= new Hashtable();
37 _submitOptions
["confirm"] = false;
38 _submitOptions
["disable"] = false;
39 _submitOptions
["groupError"] = false;
40 _submitOptions
["errorMode"] = 0;
44 /// Automatic Script installer.
46 /// <returns></returns>
47 public String
InstallScripts()
49 return InstallScripts(string.Empty
);
53 /// Installs the scripts.
55 /// <param name="locale">The locale.</param>
56 /// <returns></returns>
57 public string InstallScripts(string locale
)
59 string queryString
= null;
60 if (!string.IsNullOrEmpty(locale
))
62 queryString
= string.Format("Locale={0}", UrlEncode(locale
));
64 return RenderScriptBlockToSource("/MonoRail/Files/ValidateConfig") + Environment
.NewLine
+
65 RenderScriptBlockToSource("/MonoRail/Files/ValidateCore") + Environment
.NewLine
+
66 RenderScriptBlockToSource("/MonoRail/Files/ValidateValidators") + Environment
.NewLine
+
67 RenderScriptBlockToSource("/MonoRail/Files/ValidateLang", queryString
) + Environment
.NewLine
;
71 /// Configure the submit and validation options.
73 public void SetSubmitOptions(IDictionary parameters
)
75 _submitOptions
["confirm"] = parameters
["confirm"];
76 _submitOptions
["disable"] = parameters
["disable"];
77 _submitOptions
["groupError"] = parameters
["groupError"];
78 _submitOptions
["errorMode"] = parameters
["errorMode"];
82 /// Configure the submit and validation options.
84 /// <param name="confirm"><b>True</b> for submit confirmation. Otherwise, <b>false</b>.</param>
85 /// <param name="disable"><b>True</b> for submit buttons disabling.</param>
86 /// <param name="groupError"><b>True</b> for error grouping.</param>
87 /// <param name="errorMode"><see cref="int"/> representing the error mode.</param>
88 public void SetSubmitOptions(bool confirm
, bool disable
, bool groupError
, int errorMode
)
90 _submitOptions
["confirm"] = confirm
;
91 _submitOptions
["disable"] = disable
;
92 _submitOptions
["groupError"] = groupError
;
93 _submitOptions
["errorMode"] = errorMode
;
97 /// Returns the form validation function.
99 /// <returns></returns>
100 public String
GetValidationTriggerFunction()
102 return GetValidationTriggerFunction("this");
106 /// Returns the form validation function.
108 /// <param name="formElement">Javascript expression that return the desired form.</param>
109 /// <returns></returns>
110 public String
GetValidationTriggerFunction(String formElement
)
112 return String
.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
114 _submitOptions
["confirm"].ToString().ToLower(),
115 _submitOptions
["disable"].ToString().ToLower(),
116 _submitOptions
["disable"].ToString().ToLower(),
117 _submitOptions
["groupError"].ToString().ToLower(),
118 _submitOptions
["errorMode"]);
122 /// Returns the form validation function where you can override the options:
125 /// The options that can be overriden:
126 /// confirm (bool), disable (bool), groupError (bool), errorMode (int)
128 /// <param name="formElement">Javascript expression that return the desired form.</param>
129 /// <param name="options">Custom options</param>
130 /// <returns></returns>
131 public String
GetValidationTriggerFunction(String formElement
, IDictionary options
)
135 options
= new Hashtable(_submitOptions
);
139 MergeOptions(options
, _submitOptions
);
142 return String
.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
144 options
["confirm"].ToString().ToLower(),
145 options
["disable"].ToString().ToLower(),
146 options
["disable"].ToString().ToLower(),
147 options
["groupError"].ToString().ToLower(),
148 options
["errorMode"]);