Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / DataAnnotationsModelValidator.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.Generic;\r
16     using System.ComponentModel.DataAnnotations;\r
17 \r
18     public class DataAnnotationsModelValidator : ModelValidator {\r
19         public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)\r
20             : base(metadata, context) {\r
21 \r
22             if (attribute == null) {\r
23                 throw new ArgumentNullException("attribute");\r
24             }\r
25 \r
26             Attribute = attribute;\r
27         }\r
28 \r
29         protected internal ValidationAttribute Attribute { get; private set; }\r
30 \r
31         protected internal string ErrorMessage {\r
32             get {\r
33                 return Attribute.FormatErrorMessage(Metadata.GetDisplayName());\r
34             }\r
35         }\r
36 \r
37         public override bool IsRequired {\r
38             get {\r
39                 return Attribute is RequiredAttribute;\r
40             }\r
41         }\r
42 \r
43         internal static ModelValidator Create(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute) {\r
44             return new DataAnnotationsModelValidator(metadata, context, attribute);\r
45         }\r
46 \r
47         public override IEnumerable<ModelValidationResult> Validate(object container) {\r
48             if (!Attribute.IsValid(Metadata.Model)) {\r
49                 yield return new ModelValidationResult {\r
50                     Message = ErrorMessage\r
51                 };\r
52             }\r
53         }\r
54     }\r
55 }\r