Questions tagged [validationattribute]

Used for classes Validation Attribute Classes in .NET within the System.ComponentModel.DataAnnotations namespace

The ValidationAttribute class provides validation on class properties. It can be overridden to provide custom validation logic.

When implemented with IClientValidatable and also adds client side validation logic.

The following classes are derived from Validation Attribute:

180 questions
74
votes
7 answers

RequiredIf Conditional Validation Attribute

I was looking for some advice on the best way to go about implementing a validation attribute that does the following. Model public class MyInputModel { [Required] public int Id {get;set;} public string MyProperty1 {get;set;} …
zSynopsis
  • 4,854
  • 21
  • 69
  • 106
60
votes
3 answers

Custom validation attribute that compares the value of my property with another property's value in my model class

I want to create a custom validation attribute, in which I want to compare the value of my property with another property's value in my model class. For example I have in my model class: ... public string SourceCity { get; set; } public string…
TheForbidden
  • 1,533
  • 4
  • 22
  • 30
35
votes
8 answers

Phone Number Validation MVC

I am trying to use a regular expression to validate a phone number and return an error when an invalid number or phone number is submitted. MVC Code:
  1. Phone Number:
  2. Leonardo Wildt
    • 2,529
    • 5
    • 27
    • 56
21
votes
1 answer

Validating Enum Values within C# MVC. Partial validation occurs - How to change validation behaviour?

I've been representing an enum within my razor view as a hidden field, which is posted back to an action result. I've noticed that when it binds the string value provided within the HTML, it automatically validates the value for the enum. ///…
Luke
  • 22,826
  • 31
  • 110
  • 193
16
votes
4 answers

DataAnnotations "NotRequired" attribute

I've a model kind of complicated. I have my UserViewModel which has several properties and two of them are HomePhone and WorkPhone. Both of type PhoneViewModel. In PhoneViewModel I have CountryCode, AreaCode and Number all strings. I want to make…
Diego
  • 16,436
  • 26
  • 84
  • 136
15
votes
3 answers

Best approach for complex model/submodel validation (MVC)

Problem I know there is a lot of ways of doing Model validation within MVC, and there is quite a lot of documentation regarding this topic. However, I'm not quite sure what's the best approach for validating properties of the Model which are "Sub…
12
votes
1 answer

Testing ValidationAttribute that overrides IsValid

I'm having a bit of trouble getting my head around testing my custom validation attribute. As the method signature is protected when I invoke the IsValid method in my unit test, I can't pass in a Mock object, it's calling the base…
ediblecode
  • 11,701
  • 19
  • 68
  • 116
10
votes
1 answer

Why does Validator.TryValidateObject does not validate class if I have validation attribute in a property?

I created a custom ValidationAttribute that targets a class. This validates correctly whenever I try to call the Validator.TryValidateObject. But when I have other ValidationAttribute in the properties inside my class, the validation results does…
Lance
  • 2,774
  • 4
  • 37
  • 57
9
votes
2 answers

Get Data Annotations attributes from model

I want to create custom client-side validator, but I want define validation rules via Data Annotations attributes at business logic layer. How can I access model validation attributes in runtime? I want to write 'generator', which will convert this…
8
votes
0 answers

Unit Testing a custom TagHelper where HTML conditionally rendered based on the presence of a ValidationAttribute on the model Property

I have a custom TagHelper which extends the OOTB InputTagHelper. I am conditionally adding attributes to it based on the presence of a custom ValidationAttribute on the model property associated with it. The code for this in the TagHelper .Process…
8
votes
1 answer

How can I have a custom ValidationAttribute rendered as a 'data-val-xx' attribute on the client-side?

Given a ViewModel that looks like this: public class Login { [Required] public string Username { get; set; } [Required, CustomValidator] public string Password { get; set; } } And a View like this (Razor syntax…
Jonathan
  • 32,202
  • 38
  • 137
  • 208
7
votes
3 answers

ASP.NET MVC ValidationAttribute Get Other Property Display Name

I have created a custom CompareLessThan validation attribute by copying the ASP.NET MVC 3 CompareAttribute and instead of checking for equality, I check to see that one property is less than another. If there is a client side error, the message '{0}…
7
votes
2 answers

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property…
7
votes
5 answers

Require one field or another

Basically what I'm trying to figure out is how to require at least one of two fields to be filled out in a View. In my View, I have two text fields called ISBN and ISBN13. It doesn't matter which one the user fills in as long as one of them gets…
Christopher Johnson
  • 635
  • 2
  • 7
  • 21
7
votes
2 answers

Prevent inheriting validation attributes in model

I'm using a base contact model which other custom contact models classes inherit. public class BaseContactModel { [Required(ErrorMessage = "Firstname is required")] public virtual string FirstName { get; set; } } The base contact model uses…
Phil Cooper
  • 3,083
  • 39
  • 63
1
2 3
11 12