Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / ValidationHelper.cs
blobee4857290eb0ff6c47cae5021aedabf23934bf16
1 // Copyright 2004-2008 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;
30 /// <summary>
31 /// Initializes a new instance of the <see cref="ValidationHelper"/> class.
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;
42 /// <summary>
43 /// Initializes a new instance of the <see cref="ValidationHelper"/> class.
44 /// setting the Controller, Context and ControllerContext.
45 /// </summary>
46 /// <param name="engineContext">The engine context.</param>
47 public ValidationHelper(IEngineContext engineContext)
48 : base(engineContext)
50 _submitOptions = new Hashtable();
52 _submitOptions["confirm"] = false;
53 _submitOptions["disable"] = false;
54 _submitOptions["groupError"] = false;
55 _submitOptions["errorMode"] = 0;
58 /// <summary>
59 /// Automatic Script installer.
60 /// </summary>
61 /// <returns></returns>
62 public String InstallScripts()
64 return InstallScripts(string.Empty);
67 /// <summary>
68 /// Installs the scripts.
69 /// </summary>
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;
85 /// <summary>
86 /// Configure the submit and validation options.
87 /// </summary>
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"];
96 /// <summary>
97 /// Configure the submit and validation options.
98 /// </summary>
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;
111 /// <summary>
112 /// Returns the form validation function.
113 /// </summary>
114 /// <returns></returns>
115 public String GetValidationTriggerFunction()
117 return GetValidationTriggerFunction("this");
120 /// <summary>
121 /// Returns the form validation function.
122 /// </summary>
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} );",
128 formElement,
129 _submitOptions["confirm"].ToString().ToLower(),
130 _submitOptions["disable"].ToString().ToLower(),
131 _submitOptions["disable"].ToString().ToLower(),
132 _submitOptions["groupError"].ToString().ToLower(),
133 _submitOptions["errorMode"]);
136 /// <summary>
137 /// Returns the form validation function where you can override the options:
138 /// </summary>
139 /// <remarks>
140 /// The options that can be overriden:
141 /// confirm (bool), disable (bool), groupError (bool), errorMode (int)
142 /// </remarks>
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)
148 if (options == null)
150 options = new Hashtable(_submitOptions);
152 else
154 MergeOptions(options, _submitOptions);
157 return String.Format("return validateForm( {0}, {1}, {2}, {3}, {4}, {5} );",
158 formElement,
159 options["confirm"].ToString().ToLower(),
160 options["disable"].ToString().ToLower(),
161 options["disable"].ToString().ToLower(),
162 options["groupError"].ToString().ToLower(),
163 options["errorMode"]);