1 // Copyright 2004-2008 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
;
31 /// Initializes a new instance of the <see cref="ValidationHelper"/> class.
33 public ValidationHelper()
35 _submitOptions
= new Hashtable();
37 _submitOptions
["confirm"] = false;
38 _submitOptions
["disable"] = false;
39 _submitOptions
["groupError"] = false;
40 _submitOptions
["errorMode"] = 0;
43 /// Initializes a new instance of the <see cref="ValidationHelper"/> class.
44 /// setting the Controller, Context and ControllerContext.
46 /// <param name="engineContext">The engine context.</param>
47 public ValidationHelper(IEngineContext engineContext
)
50 _submitOptions
= new Hashtable();
52 _submitOptions
["confirm"] = false;
53 _submitOptions
["disable"] = false;
54 _submitOptions
["groupError"] = false;
55 _submitOptions
["errorMode"] = 0;
59 /// Automatic Script installer.
61 /// <returns></returns>
62 public String
InstallScripts()
64 return InstallScripts(string.Empty
);
68 /// Installs the scripts.
70 /// <param name="locale">The locale.</param>
71 /// <returns></returns>
72 public string InstallScripts(string locale
)
74 string queryString
= null;
75 if (!string.IsNullOrEmpty(locale
))
77 queryString
= string.Format("Locale={0}", UrlEncode(locale
));
79 return RenderScriptBlockToSource("/MonoRail/Files/ValidateConfig") + Environment
.NewLine
+
80 RenderScriptBlockToSource("/MonoRail/Files/ValidateCore") + Environment
.NewLine
+
81 RenderScriptBlockToSource("/MonoRail/Files/ValidateValidators") + Environment
.NewLine
+
82 RenderScriptBlockToSource("/MonoRail/Files/ValidateLang", queryString
) + Environment
.NewLine
;
86 /// Configure the submit and validation options.
88 public void SetSubmitOptions(IDictionary parameters
)
90 _submitOptions
["confirm"] = parameters
["confirm"];
91 _submitOptions
["disable"] = parameters
["disable"];
92 _submitOptions
["groupError"] = parameters
["groupError"];
93 _submitOptions
["errorMode"] = parameters
["errorMode"];
97 /// Configure the submit and validation options.
99 /// <param name="confirm"><b>True</b> for submit confirmation. Otherwise, <b>false</b>.</param>
100 /// <param name="disable"><b>True</b> for submit buttons disabling.</param>
101 /// <param name="groupError"><b>True</b> for error grouping.</param>
102 /// <param name="errorMode"><see cref="int"/> representing the error mode.</param>
103 public void SetSubmitOptions(bool confirm
, bool disable
, bool groupError
, int errorMode
)
105 _submitOptions
["confirm"] = confirm
;
106 _submitOptions
["disable"] = disable
;
107 _submitOptions
["groupError"] = groupError
;
108 _submitOptions
["errorMode"] = errorMode
;
112 /// Returns the form validation function.
114 /// <returns></returns>
115 public String
GetValidationTriggerFunction()
117 return GetValidationTriggerFunction("this");
121 /// Returns the form validation function.
123 /// <param name="formElement">Javascript expression that return the desired form.</param>
124 /// <returns></returns>
125 public String
GetValidationTriggerFunction(String formElement
)
127 return String
.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
129 _submitOptions
["confirm"].ToString().ToLower(),
130 _submitOptions
["disable"].ToString().ToLower(),
131 _submitOptions
["disable"].ToString().ToLower(),
132 _submitOptions
["groupError"].ToString().ToLower(),
133 _submitOptions
["errorMode"]);
137 /// Returns the form validation function where you can override the options:
140 /// The options that can be overriden:
141 /// confirm (bool), disable (bool), groupError (bool), errorMode (int)
143 /// <param name="formElement">Javascript expression that return the desired form.</param>
144 /// <param name="options">Custom options</param>
145 /// <returns></returns>
146 public String
GetValidationTriggerFunction(String formElement
, IDictionary options
)
150 options
= new Hashtable(_submitOptions
);
154 MergeOptions(options
, _submitOptions
);
157 return String
.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
159 options
["confirm"].ToString().ToLower(),
160 options
["disable"].ToString().ToLower(),
161 options
["disable"].ToString().ToLower(),
162 options
["groupError"].ToString().ToLower(),
163 options
["errorMode"]);