- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / ValidationHelper.cs
blobd8a2631a2937bc14a393e9503c88733ec795257e
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
21 /// <summary>
22 /// Helper that provides client-side validation.
23 /// </summary>
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;
30 /// <summary>
31 /// Constructor.
32 /// </summary>
33 public ValidationHelper()
35 _submitOptions = new Hashtable();
37 _submitOptions["confirm"] = false;
38 _submitOptions["disable"] = false;
39 _submitOptions["groupError"] = false;
40 _submitOptions["errorMode"] = 0;
43 /// <summary>
44 /// Automatic Script installer.
45 /// </summary>
46 /// <returns></returns>
47 public String InstallScripts()
49 return InstallScripts(string.Empty);
52 /// <summary>
53 /// Installs the scripts.
54 /// </summary>
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;
70 /// <summary>
71 /// Configure the submit and validation options.
72 /// </summary>
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"];
81 /// <summary>
82 /// Configure the submit and validation options.
83 /// </summary>
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;
96 /// <summary>
97 /// Returns the form validation function.
98 /// </summary>
99 /// <returns></returns>
100 public String GetValidationTriggerFunction()
102 return GetValidationTriggerFunction("this");
105 /// <summary>
106 /// Returns the form validation function.
107 /// </summary>
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} );",
113 formElement,
114 _submitOptions["confirm"].ToString().ToLower(),
115 _submitOptions["disable"].ToString().ToLower(),
116 _submitOptions["disable"].ToString().ToLower(),
117 _submitOptions["groupError"].ToString().ToLower(),
118 _submitOptions["errorMode"]);
121 /// <summary>
122 /// Returns the form validation function where you can override the options:
123 /// </summary>
124 /// <remarks>
125 /// The options that can be overriden:
126 /// confirm (bool), disable (bool), groupError (bool), errorMode (int)
127 /// </remarks>
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)
133 if (options == null)
135 options = new Hashtable(_submitOptions);
137 else
139 MergeOptions(options, _submitOptions);
142 return String.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
143 formElement,
144 options["confirm"].ToString().ToLower(),
145 options["disable"].ToString().ToLower(),
146 options["disable"].ToString().ToLower(),
147 options["groupError"].ToString().ToLower(),
148 options["errorMode"]);