New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ModelStateDictionary.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections;\r
16     using System.Collections.Generic;\r
17     using System.Linq;\r
18 \r
19     [Serializable]\r
20     public class ModelStateDictionary : IDictionary<string, ModelState> {\r
21 \r
22         private readonly Dictionary<string, ModelState> _innerDictionary = new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);\r
23 \r
24         public ModelStateDictionary() {\r
25         }\r
26 \r
27         public ModelStateDictionary(ModelStateDictionary dictionary) {\r
28             if (dictionary == null) {\r
29                 throw new ArgumentNullException("dictionary");\r
30             }\r
31 \r
32             foreach (var entry in dictionary) {\r
33                 _innerDictionary.Add(entry.Key, entry.Value);\r
34             }\r
35         }\r
36 \r
37         public int Count {\r
38             get {\r
39                 return _innerDictionary.Count;\r
40             }\r
41         }\r
42 \r
43         public bool IsReadOnly {\r
44             get {\r
45                 return ((IDictionary<string, ModelState>)_innerDictionary).IsReadOnly;\r
46             }\r
47         }\r
48 \r
49         public bool IsValid {\r
50             get {\r
51                 return Values.All(modelState => modelState.Errors.Count == 0);\r
52             }\r
53         }\r
54 \r
55         public ICollection<string> Keys {\r
56             get {\r
57                 return _innerDictionary.Keys;\r
58             }\r
59         }\r
60 \r
61         public ModelState this[string key] {\r
62             get {\r
63                 ModelState value;\r
64                 _innerDictionary.TryGetValue(key, out value);\r
65                 return value;\r
66             }\r
67             set {\r
68                 _innerDictionary[key] = value;\r
69             }\r
70         }\r
71 \r
72         public ICollection<ModelState> Values {\r
73             get {\r
74                 return _innerDictionary.Values;\r
75             }\r
76         }\r
77 \r
78         public void Add(KeyValuePair<string, ModelState> item) {\r
79             ((IDictionary<string, ModelState>)_innerDictionary).Add(item);\r
80         }\r
81 \r
82         public void Add(string key, ModelState value) {\r
83             _innerDictionary.Add(key, value);\r
84         }\r
85 \r
86         public void AddModelError(string key, Exception exception) {\r
87             GetModelStateForKey(key).Errors.Add(exception);\r
88         }\r
89 \r
90         public void AddModelError(string key, string errorMessage) {\r
91             GetModelStateForKey(key).Errors.Add(errorMessage);\r
92         }\r
93 \r
94         public void Clear() {\r
95             _innerDictionary.Clear();\r
96         }\r
97 \r
98         public bool Contains(KeyValuePair<string, ModelState> item) {\r
99             return ((IDictionary<string, ModelState>)_innerDictionary).Contains(item);\r
100         }\r
101 \r
102         public bool ContainsKey(string key) {\r
103             return _innerDictionary.ContainsKey(key);\r
104         }\r
105 \r
106         public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex) {\r
107             ((IDictionary<string, ModelState>)_innerDictionary).CopyTo(array, arrayIndex);\r
108         }\r
109 \r
110         public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator() {\r
111             return _innerDictionary.GetEnumerator();\r
112         }\r
113 \r
114         private ModelState GetModelStateForKey(string key) {\r
115             if (key == null) {\r
116                 throw new ArgumentNullException("key");\r
117             }\r
118 \r
119             ModelState modelState;\r
120             if (!TryGetValue(key, out modelState)) {\r
121                 modelState = new ModelState();\r
122                 this[key] = modelState;\r
123             }\r
124 \r
125             return modelState;\r
126         }\r
127 \r
128         public bool IsValidField(string key) {\r
129             if (key == null) {\r
130                 throw new ArgumentNullException("key");\r
131             }\r
132 \r
133             // if the key is not found in the dictionary, we just say that it's valid (since there are no errors)\r
134             return DictionaryHelpers.FindKeysWithPrefix(this, key).All(entry => entry.Value.Errors.Count == 0);\r
135         }\r
136 \r
137         public void Merge(ModelStateDictionary dictionary) {\r
138             if (dictionary == null) {\r
139                 return;\r
140             }\r
141 \r
142             foreach (var entry in dictionary) {\r
143                 this[entry.Key] = entry.Value;\r
144             }\r
145         }\r
146 \r
147         public bool Remove(KeyValuePair<string, ModelState> item) {\r
148             return ((IDictionary<string, ModelState>)_innerDictionary).Remove(item);\r
149         }\r
150 \r
151         public bool Remove(string key) {\r
152             return _innerDictionary.Remove(key);\r
153         }\r
154 \r
155         public void SetModelValue(string key, ValueProviderResult value) {\r
156             GetModelStateForKey(key).Value = value;\r
157         }\r
158 \r
159         public bool TryGetValue(string key, out ModelState value) {\r
160             return _innerDictionary.TryGetValue(key, out value);\r
161         }\r
162 \r
163         #region IEnumerable Members\r
164         IEnumerator IEnumerable.GetEnumerator() {\r
165             return ((IEnumerable)_innerDictionary).GetEnumerator();\r
166         }\r
167         #endregion\r
168 \r
169     }\r
170 }\r