forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAnnotationsModelValidator.cs
More file actions
93 lines (77 loc) · 3.82 KB
/
DataAnnotationsModelValidator.cs
File metadata and controls
93 lines (77 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace System.Web.Mvc
{
public class DataAnnotationsModelValidator : ModelValidator
{
public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
: base(metadata, context)
{
if (attribute == null)
{
throw new ArgumentNullException("attribute");
}
Attribute = attribute;
}
protected internal ValidationAttribute Attribute { get; private set; }
protected internal string ErrorMessage
{
get { return Attribute.FormatErrorMessage(Metadata.GetDisplayName()); }
}
public override bool IsRequired
{
get { return Attribute is RequiredAttribute; }
}
internal static ModelValidator Create(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
{
return new DataAnnotationsModelValidator(metadata, context, attribute);
}
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
IEnumerable<ModelClientValidationRule> results = base.GetClientValidationRules();
IClientValidatable clientValidatable = Attribute as IClientValidatable;
if (clientValidatable != null)
{
results = results.Concat(clientValidatable.GetClientValidationRules(Metadata, ControllerContext));
}
return results;
}
public override IEnumerable<ModelValidationResult> Validate(object container)
{
// Per the WCF RIA Services team, instance can never be null (if you have
// no parent, you pass yourself for the "instance" parameter).
string memberName = Metadata.PropertyName ?? Metadata.ModelType.Name;
ValidationContext context = new ValidationContext(container ?? Metadata.Model)
{
DisplayName = Metadata.GetDisplayName(),
MemberName = memberName
};
ValidationResult result = Attribute.GetValidationResult(Metadata.Model, context);
if (result != ValidationResult.Success)
{
// ModelValidationResult.MemberName is used by invoking validators (such as ModelValidator) to
// construct the ModelKey for ModelStateDictionary. When validating at type level we want to append the
// returned MemberNames if specified (e.g. person.Address.FirstName). For property validation, the
// ModelKey can be constructed using the ModelMetadata and we should ignore MemberName (we don't want
// (person.Name.Name). However the invoking validator does not have a way to distinguish between these two
// cases. Consequently we'll only set MemberName if this validation returns a MemberName that is different
// from the property being validated.
string errorMemberName = result.MemberNames.FirstOrDefault();
if (String.Equals(errorMemberName, memberName, StringComparison.Ordinal))
{
errorMemberName = null;
}
var validationResult = new ModelValidationResult
{
Message = result.ErrorMessage,
MemberName = errorMemberName
};
return new ModelValidationResult[] { validationResult };
}
return Enumerable.Empty<ModelValidationResult>();
}
}
}