1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.ComponentModel.DataAnnotations;
\r
18 public class DataAnnotationsModelValidator : ModelValidator {
\r
19 public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
\r
20 : base(metadata, context) {
\r
22 if (attribute == null) {
\r
23 throw new ArgumentNullException("attribute");
\r
26 Attribute = attribute;
\r
29 protected internal ValidationAttribute Attribute { get; private set; }
\r
31 protected internal string ErrorMessage {
\r
33 return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
\r
37 public override bool IsRequired {
\r
39 return Attribute is RequiredAttribute;
\r
43 internal static ModelValidator Create(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute) {
\r
44 return new DataAnnotationsModelValidator(metadata, context, attribute);
\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