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
.Tests
.Helpers
.Validations
17 using System
.Globalization
;
18 using System
.Threading
;
19 using Castle
.Components
.Validator
;
20 using Castle
.MonoRail
.Framework
.Helpers
;
21 using Castle
.MonoRail
.Framework
.Tests
.Controllers
;
22 using NUnit
.Framework
;
25 public class FormValidationTestCase
27 private FormHelper helper
;
28 private ModelWithValidation model
;
33 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
35 Thread
.CurrentThread
.CurrentCulture
= en
;
36 Thread
.CurrentThread
.CurrentUICulture
= en
;
38 helper
= new FormHelper();
39 model
= new ModelWithValidation();
41 HomeController controller
= new HomeController();
42 ControllerContext controllerContext
= new ControllerContext();
44 controllerContext
.PropertyBag
.Add("model", model
);
46 helper
.SetController(controller
, controllerContext
);
50 public void ValidationIsGeneratedForModel()
52 helper
.FormTag(DictHelper
.Create("noaction=true"));
54 Assert
.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
55 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
56 "title=\"This is a required field.\" />", helper
.TextField("model.nonemptyfield"));
58 Assert
.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
59 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
60 "title=\"Email doesnt look right.\" />", helper
.TextField("model.emailfield"));
62 // Attribute order cannot be guaranted, so this test may fail ocasionally
63 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
64 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
65 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("model.nonemptyemailfield"));
71 public void UsingScopes()
73 helper
.FormTag(DictHelper
.Create("noaction=true"));
76 Assert
.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
77 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
78 "title=\"This is a required field.\" />", helper
.TextField("nonemptyfield"));
80 Assert
.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
81 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
82 "title=\"Email doesnt look right.\" />", helper
.TextField("emailfield"));
84 // Attribute order cannot be guaranted, so this test may fail ocasionally
85 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
86 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
87 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("nonemptyemailfield"));
94 public void ValidationForSelects()
96 helper
.FormTag(DictHelper
.Create("noaction=true"));
98 Assert
.AreEqual("<select id=\"model_city\" " +
99 "name=\"model.city\" class=\"validate-selection\" " +
100 "title=\"This is a required field.\" >\r\n" +
101 "<option value=\"0\">---</option>\r\n" +
102 "<option value=\"Sao Paulo\">Sao Paulo</option>\r\n" +
103 "<option value=\"Sao Carlos\">Sao Carlos</option>\r\n" +
105 helper
.Select("model.city",
106 new string[] { "Sao Paulo", "Sao Carlos" }
, DictHelper
.Create("firstoption=---")));
112 public void ValidationAreInherited()
114 helper
.FormTag(DictHelper
.Create("noaction=true"));
116 Assert
.AreEqual("<select id=\"model_city_id\" " +
117 "name=\"model.city.id\" class=\"validate-selection\" " +
118 "title=\"This is a required field.\" >\r\n" +
119 "<option value=\"0\">---</option>\r\n" +
120 "<option value=\"1\">1</option>\r\n" +
121 "<option value=\"2\">2</option>\r\n" +
123 helper
.Select("model.city.id",
124 new string[] { "1", "2" }
, DictHelper
.Create("firstoption=---")));
130 public class ModelWithValidation
132 private string nonEmptyField
;
133 private string emailField
;
134 private string nonEmptyEmailField
;
136 private Country country
;
139 public Country Country
141 get { return country; }
142 set { country = value; }
149 set { city = value; }
153 public string NonEmptyField
155 get { return nonEmptyField; }
156 set { nonEmptyField = value; }
159 [ValidateEmail("Email doesnt look right")]
160 public string EmailField
162 get { return emailField; }
163 set { emailField = value; }
166 [ValidateNonEmpty
, ValidateEmail
]
167 public string NonEmptyEmailField
169 get { return nonEmptyEmailField; }
170 set { nonEmptyEmailField = value; }