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
;
21 /// Helper that provides client-side validation.
23 /// <remarks>The javascript core lib is extension of Peter Bailey's
24 /// fValidate(http://www.peterbailey.net/fValidate/).</remarks>
25 public class ValidationHelper
: AbstractHelper
27 private IDictionary _submitOptions
;
32 public ValidationHelper()
34 _submitOptions
= new Hashtable();
36 _submitOptions
["confirm"] = false;
37 _submitOptions
["disable"] = false;
38 _submitOptions
["groupError"] = false;
39 _submitOptions
["errorMode"] = 0;
43 /// Automatic Script installer.
45 /// <returns></returns>
46 public String
InstallScripts()
48 return InstallScripts(string.Empty
);
52 /// Installs the scripts.
54 /// <param name="locale">The locale.</param>
55 /// <returns></returns>
56 public string InstallScripts(string locale
)
58 string queryString
= null;
59 if (!string.IsNullOrEmpty(locale
))
61 queryString
= string.Format("Locale={0}", UrlEncode(locale
));
63 return RenderScriptBlockToSource("/MonoRail/Files/ValidateConfig") + Environment
.NewLine
+
64 RenderScriptBlockToSource("/MonoRail/Files/ValidateCore") + Environment
.NewLine
+
65 RenderScriptBlockToSource("/MonoRail/Files/ValidateValidators") + Environment
.NewLine
+
66 RenderScriptBlockToSource("/MonoRail/Files/ValidateLang", queryString
) + Environment
.NewLine
;
70 /// Configure the submit and validation options.
72 public void SetSubmitOptions(IDictionary parameters
)
74 _submitOptions
["confirm"] = parameters
["confirm"];
75 _submitOptions
["disable"] = parameters
["disable"];
76 _submitOptions
["groupError"] = parameters
["groupError"];
77 _submitOptions
["errorMode"] = parameters
["errorMode"];
81 /// Configure the submit and validation options.
83 /// <param name="confirm"><b>True</b> for submit confirmation. Otherwise, <b>false</b>.</param>
84 /// <param name="disable"><b>True</b> for submit buttons disabling.</param>
85 /// <param name="groupError"><b>True</b> for error grouping.</param>
86 /// <param name="errorMode"><see cref="int"/> representing the error mode.</param>
87 public void SetSubmitOptions(bool confirm
, bool disable
, bool groupError
, int errorMode
)
89 _submitOptions
["confirm"] = confirm
;
90 _submitOptions
["disable"] = disable
;
91 _submitOptions
["groupError"] = groupError
;
92 _submitOptions
["errorMode"] = errorMode
;
96 /// Returns the form validation function.
98 /// <returns></returns>
99 public String
GetValidationTriggerFunction()
101 return GetValidationTriggerFunction("this");
105 /// Returns the form validation function.
107 /// <param name="formElement">Javascript expression that return the desired form.</param>
108 /// <returns></returns>
109 public String
GetValidationTriggerFunction(String formElement
)
111 return String
.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
113 _submitOptions
["confirm"].ToString().ToLower(),
114 _submitOptions
["disable"].ToString().ToLower(),
115 _submitOptions
["disable"].ToString().ToLower(),
116 _submitOptions
["groupError"].ToString().ToLower(),
117 _submitOptions
["errorMode"]);
121 /// Returns the form validation function where you can override the options:
124 /// The options that can be overriden:
125 /// confirm (bool), disable (bool), groupError (bool), errorMode (int)
127 /// <param name="formElement">Javascript expression that return the desired form.</param>
128 /// <param name="options">Custom options</param>
129 /// <returns></returns>
130 public String
GetValidationTriggerFunction(String formElement
, IDictionary options
)
134 options
= new Hashtable(_submitOptions
);
138 MergeOptions(options
, _submitOptions
);
141 return String
.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
143 options
["confirm"].ToString().ToLower(),
144 options
["disable"].ToString().ToLower(),
145 options
["disable"].ToString().ToLower(),
146 options
["groupError"].ToString().ToLower(),
147 options
["errorMode"]);