Merged branch back to the trunk. Build is passing with no changes.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / ValidationHelper.cs
blob578902504411afcc812577bbc822281e7483bd49
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;
20 /// <summary>
21 /// Helper that provides client-side validation.
22 /// </summary>
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;
29 /// <summary>
30 /// Constructor.
31 /// </summary>
32 public ValidationHelper()
34 _submitOptions = new Hashtable();
36 _submitOptions["confirm"] = false;
37 _submitOptions["disable"] = false;
38 _submitOptions["groupError"] = false;
39 _submitOptions["errorMode"] = 0;
42 /// <summary>
43 /// Automatic Script installer.
44 /// </summary>
45 /// <returns></returns>
46 public String InstallScripts()
48 return InstallScripts(string.Empty);
51 /// <summary>
52 /// Installs the scripts.
53 /// </summary>
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;
69 /// <summary>
70 /// Configure the submit and validation options.
71 /// </summary>
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"];
80 /// <summary>
81 /// Configure the submit and validation options.
82 /// </summary>
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;
95 /// <summary>
96 /// Returns the form validation function.
97 /// </summary>
98 /// <returns></returns>
99 public String GetValidationTriggerFunction()
101 return GetValidationTriggerFunction("this");
104 /// <summary>
105 /// Returns the form validation function.
106 /// </summary>
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} );",
112 formElement,
113 _submitOptions["confirm"].ToString().ToLower(),
114 _submitOptions["disable"].ToString().ToLower(),
115 _submitOptions["disable"].ToString().ToLower(),
116 _submitOptions["groupError"].ToString().ToLower(),
117 _submitOptions["errorMode"]);
120 /// <summary>
121 /// Returns the form validation function where you can override the options:
122 /// </summary>
123 /// <remarks>
124 /// The options that can be overriden:
125 /// confirm (bool), disable (bool), groupError (bool), errorMode (int)
126 /// </remarks>
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)
132 if (options == null)
134 options = new Hashtable(_submitOptions);
136 else
138 MergeOptions(options, _submitOptions);
141 return String.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
142 formElement,
143 options["confirm"].ToString().ToLower(),
144 options["disable"].ToString().ToLower(),
145 options["disable"].ToString().ToLower(),
146 options["groupError"].ToString().ToLower(),
147 options["errorMode"]);