1 // Copyright 2004-2007 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 Castle
.MonoRail
.TestSupport
;
23 using NUnit
.Framework
;
26 public class FormValidationTestCase
: BaseControllerTest
28 private FormHelper helper
;
29 private ModelWithValidation model
;
34 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
36 Thread
.CurrentThread
.CurrentCulture
= en
;
37 Thread
.CurrentThread
.CurrentUICulture
= en
;
39 helper
= new FormHelper();
40 model
= new ModelWithValidation();
42 HomeController controller
= new HomeController();
43 PrepareController(controller
, "", "Home", "Index");
45 controller
.PropertyBag
.Add("model", model
);
47 helper
.SetController(controller
);
51 public void ValidationIsGeneratedForModel()
53 helper
.FormTag(DictHelper
.Create("noaction=true"));
55 Assert
.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
56 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
57 "title=\"This is a required field.\" />", helper
.TextField("model.nonemptyfield"));
59 Assert
.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
60 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
61 "title=\"Email doesnt look right.\" />", helper
.TextField("model.emailfield"));
63 // Attribute order cannot be guaranted, so this test may fail ocasionally
64 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
65 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
66 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("model.nonemptyemailfield"));
72 public void UsingScopes()
74 helper
.FormTag(DictHelper
.Create("noaction=true"));
77 Assert
.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
78 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
79 "title=\"This is a required field.\" />", helper
.TextField("nonemptyfield"));
81 Assert
.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
82 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
83 "title=\"Email doesnt look right.\" />", helper
.TextField("emailfield"));
85 // Attribute order cannot be guaranted, so this test may fail ocasionally
86 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
87 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
88 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("nonemptyemailfield"));
95 public void ValidationForSelects()
97 helper
.FormTag(DictHelper
.Create("noaction=true"));
99 Assert
.AreEqual("<select id=\"model_city\" " +
100 "name=\"model.city\" class=\"validate-selection\" " +
101 "title=\"This is a required field.\" >\r\n" +
102 "<option value=\"0\">---</option>\r\n" +
103 "<option value=\"Sao Paulo\">Sao Paulo</option>\r\n" +
104 "<option value=\"Sao Carlos\">Sao Carlos</option>\r\n" +
106 helper
.Select("model.city",
107 new string[] { "Sao Paulo", "Sao Carlos" }
, DictHelper
.Create("firstoption=---")));
113 public void ValidationAreInherited()
115 helper
.FormTag(DictHelper
.Create("noaction=true"));
117 Assert
.AreEqual("<select id=\"model_city_id\" " +
118 "name=\"model.city.id\" class=\"validate-selection\" " +
119 "title=\"This is a required field.\" >\r\n" +
120 "<option value=\"0\">---</option>\r\n" +
121 "<option value=\"1\">1</option>\r\n" +
122 "<option value=\"2\">2</option>\r\n" +
124 helper
.Select("model.city.id",
125 new string[] { "1", "2" }
, DictHelper
.Create("firstoption=---")));
131 public class ModelWithValidation
133 private string nonEmptyField
;
134 private string emailField
;
135 private string nonEmptyEmailField
;
137 private Country country
;
140 public Country Country
142 get { return country; }
143 set { country = value; }
150 set { city = value; }
154 public string NonEmptyField
156 get { return nonEmptyField; }
157 set { nonEmptyField = value; }
160 [ValidateEmail("Email doesnt look right")]
161 public string EmailField
163 get { return emailField; }
164 set { emailField = value; }
167 [ValidateNonEmpty
, ValidateEmail
]
168 public string NonEmptyEmailField
170 get { return nonEmptyEmailField; }
171 set { nonEmptyEmailField = value; }