New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ValidationSummary.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Globalization;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 #if NET_2_0
39         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 #endif
41         public class ValidationSummary : WebControl {
42                 #region Public Constructors
43                 public ValidationSummary() : base(HtmlTextWriterTag.Div) {
44                         this.ForeColor = Color.Red;
45                 }
46                 #endregion      // Public Constructors
47
48                 #region Public Instance Properties
49 #if ONLY_1_1
50                 [Bindable(true)]
51 #endif          
52                 [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
53                 [WebSysDescription ("")]
54                 [WebCategory ("Appearance")]
55                 public ValidationSummaryDisplayMode DisplayMode {
56                         get {
57                                 object obj;
58
59                                 obj = ViewState["DisplayMode"];
60                                 if (obj != null) {
61                                         return (ValidationSummaryDisplayMode)obj;
62                                 }
63                                 return ValidationSummaryDisplayMode.BulletList;
64                         }
65
66                         set {
67                                 ViewState["DisplayMode"] = value;
68                         }
69                 }
70
71                 [DefaultValue(true)]
72 #if NET_2_0
73                 [Themeable (false)]
74 #endif          
75                 [WebSysDescription ("")]
76                 [WebCategory ("Behavior")]
77                 public bool EnableClientScript {
78                         get {
79                                 return ViewState.GetBool("EnableClientScript", true);
80                         }
81
82                         set {
83                                 ViewState["EnableClientScript"] = value;
84                         }
85                 }
86
87                 [DefaultValue(typeof (Color), "Red")]
88                 public override System.Drawing.Color ForeColor {
89                         get {
90                                 return base.ForeColor;
91                         }
92
93                         set {
94                                 base.ForeColor = value;
95                         }
96                 }
97
98 #if ONLY_1_1
99                 [Bindable(true)]
100 #endif          
101                 [DefaultValue("")]
102 #if NET_2_0
103                 [Localizable (true)]
104 #endif          
105                 [WebSysDescription ("")]
106                 [WebCategory ("Appearance")]
107                 public string HeaderText {
108                         get {
109                                 return ViewState.GetString("HeaderText", string.Empty);
110                         }
111
112                         set {
113                                 ViewState["HeaderText"] = value;
114                         }
115                 }
116
117 #if ONLY_1_1
118                 [Bindable(true)]
119 #endif          
120                 [DefaultValue(false)]
121                 [WebSysDescription ("")]
122                 [WebCategory ("Behavior")]
123                 public bool ShowMessageBox {
124                         get {
125                                 return ViewState.GetBool("ShowMessageBox", false);
126                         }
127
128                         set {
129                                 ViewState["ShowMessageBox"] = value;
130                         }
131                 }
132
133 #if ONLY_1_1
134                 [Bindable(true)]
135 #endif          
136                 [DefaultValue(true)]
137                 [WebSysDescription ("")]
138                 [WebCategory ("Behavior")]
139                 public bool ShowSummary {
140                         get {
141                                 return ViewState.GetBool("ShowSummary", true);
142                         }
143
144                         set {
145                                 ViewState["ShowSummary"] = value;
146                         }
147                 }
148
149 #if NET_2_0
150                 [DefaultValue ("")]
151                 [Themeable (false)]
152                 public virtual string ValidationGroup
153                 {
154                         get {
155                                 return ViewState.GetString("ValidationGroup", string.Empty);
156                         }
157                         set {
158                                 ViewState["ValidationGroup"] = value;
159                         }
160                 }
161 #endif          
162                 
163                 #endregion      // Public Instance Properties
164
165                 #region Public Instance Methods
166                 [MonoTODO ()]
167                 // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute 
168                 // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
169                 protected override void AddAttributesToRender(HtmlTextWriter writer) {
170                         base.AddAttributesToRender (writer);
171
172 #if NET_2_0
173                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
174 #else
175                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
176 #endif
177                                 /* force an ID here if we weren't assigned one */
178                                 if (ID == null)
179                                         writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
180 #if NET_2_0
181                                 if (ValidationGroup != String.Empty)
182                                         writer.AddAttribute ("validationgroup", ValidationGroup);
183 #endif
184                                 if (HeaderText != "")
185                                         writer.AddAttribute ("headertext", HeaderText);
186
187                                 if (ShowMessageBox)
188                                         writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
189
190                                 if (ShowSummary == false)
191                                         writer.AddAttribute ("showsummary", ShowSummary.ToString());
192
193                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
194                                         writer.AddAttribute ("displaymode", DisplayMode.ToString());
195
196                                 if (!has_errors)
197                                         writer.AddStyleAttribute ("display", "none");
198                         }
199                 }
200
201 #if NET_2_0
202                 protected internal
203 #else           
204                 protected
205 #endif          
206                 override void OnPreRender(EventArgs e) {
207                         base.OnPreRender (e);
208
209                         pre_render_called = true;
210                 }
211
212 #if NET_2_0
213                 protected internal
214 #else           
215                 protected
216 #endif          
217                 override void Render(HtmlTextWriter writer) {
218 #if NET_2_0
219                         if (!Enabled)
220                                 return;
221 #endif
222                         ValidatorCollection     validators;
223                         ArrayList               errors;
224
225                         // First, figure out if there's even data to deal with
226 #if NET_2_0
227                         validators = Page.GetValidators (ValidationGroup);
228 #else
229                         validators = Page.Validators;
230 #endif
231                         if (validators.Count == 0) {
232                                 return;
233                         }
234
235                         // We have validators
236                         errors = new ArrayList(validators.Count);
237                         for (int i = 0; i < validators.Count; i++) {
238                                 if (!validators[i].IsValid) {
239                                         errors.Add(validators[i].ErrorMessage);
240                                 }
241                         }
242
243                         has_errors = errors.Count > 0;
244
245 #if NET_2_0
246                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
247 #else
248                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
249 #endif
250                                 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
251                                                                             String.Format ("document.getElementById ('{0}')", ClientID));
252                         }
253
254                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
255                                 base.RenderBeginTag(writer);
256
257                         if (ShowSummary && has_errors) {
258                                 switch(DisplayMode) {
259                                         case ValidationSummaryDisplayMode.BulletList: {
260                                                 if (HeaderText.Length > 0) {
261                                                         writer.Write(HeaderText);
262                                                 }
263
264                                                 writer.Write("<ul>");
265                                                 for (int i = 0; i < errors.Count; i++) {
266                                                         writer.Write("<li>");
267                                                         writer.Write(errors[i]);
268                                                         writer.Write("</li>");
269                                                 }
270                                                 writer.Write("</ul>");
271                                                 break;
272                                         }
273
274                                         case ValidationSummaryDisplayMode.List: {
275                                                 if (HeaderText.Length > 0) {
276                                                         writer.Write(HeaderText);
277 #if NET_2_0
278                                                         writer.Write("<br />");
279 #else
280                                                         writer.Write("<br>");
281 #endif
282                                                 }
283
284                                                 for (int i = 0; i < errors.Count; i++) {
285                                                         writer.Write(errors[i]);
286 #if NET_2_0
287                                                         writer.Write("<br />");
288 #else
289                                                         writer.Write("<br>");
290 #endif
291                                                 }
292                                                 break;
293                                         }
294
295                                         case ValidationSummaryDisplayMode.SingleParagraph: {
296                                                 if (HeaderText.Length > 0) {
297                                                         writer.Write(HeaderText);
298                                                         writer.Write(" ");
299                                                 }
300
301                                                 for (int i = 0; i < errors.Count; i++) {
302                                                         writer.Write(errors[i]);
303                                                         writer.Write(" ");
304                                                 }
305 #if NET_2_0
306                                                 writer.Write("<br />");
307 #else
308                                                 writer.Write("<br>");
309 #endif
310
311                                                 break;
312                                         }
313                                 }
314                         }
315
316                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
317                                 base.RenderEndTag(writer);
318                 }
319                 #endregion      // Public Instance Methods
320
321                 bool pre_render_called;
322                 bool has_errors;
323         }
324 }