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
1
vote
1 answer

How to notify users for custom validation based on multiple properties

I'm trying to build a custom message validation that involves two or more attribute. Here is a simplified version of my DTO: public class FooDTO { public int Id { get; set; } public int Name { get; set; } //more attributes... public…
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
1
vote
2 answers

Is it possible to access properties from a different model in a ValidationAttribute

I have a web api with some nested models that are being bound like so: public class Class1 { //This data is needed in Class2 and Class3 validation public bool ImportantInformation {get; set;} public Class2 A {get; set;} public Class3 B {get;…
Benjamin U.
  • 580
  • 1
  • 3
  • 16
1
vote
1 answer

Whats is the best way to check URL document link existence?

I need to check the validity of the URL to different files without opening the link document/image. I used the below code in ASP.net 4.7.2 in validation attribute. The current code is as follows : (working fine with jpg) public override bool…
PCG
  • 2,049
  • 5
  • 24
  • 42
1
vote
0 answers

C# RequireNonDefaultAttribute strange behaviour: Equals vs == inside IsValid()

Long story short: I made a RequireNonDefaultAttribute (I mainly needed it for guids in my DTOs) [AttributeUsage(AttributeTargets.Property)] public class RequireNonDefaultAttribute : ValidationAttribute { public…
john
  • 11
  • 1
1
vote
0 answers

Can't get TryValidateModel() to work

I have a signup action that attempts checks if the model state is valid, attempts a sign in, if fail revalidates the model. I am unable to revalidate the model using TryValidateModel. I have modified some functions to force the model to be…
1
vote
1 answer

MVC3 ValidationAttribute break

I upgraded my project from MVC2 to MVC3 and all custom ValidationAttributes broke. At some point in the model binder phase, it throws this exception: Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name:…
1
vote
2 answers

Get the validation attribute which is setting ModelState's IsValid to false

Is there any way to know which validation attribute caused ModelState.IsValid == false. Class A public class A { [Required(ErrorMessage = "Required")] [DataFormat(DataType.Date, "Must be a Date (DD/MM/YEAR)")] [NoFutureDate(ErrorMessage…
phougatv
  • 881
  • 2
  • 12
  • 29
1
vote
2 answers

How to remove an inherited ValidationAttribute?

I have a controller which take a model as parameter. This model is a specialized version of a generic model. I want my specialized model to hide a required base field to make it not required. Here is what I did : Controller public class…
gobes
  • 507
  • 7
  • 25
1
vote
1 answer

Custom ValidationAttribute: How to check for duplicate value, ignoring the object being edited

I'm trying to write a custom ValidationAttribute that verifies no records already exist with the same value. The problem is that if the user is editing an existing record, then my code will find a record that matches the same value. It will find the…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1
vote
1 answer

Custom email address attribute on client side

Evening all! I've created the below validation attribute to check a list of emails are valid. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)] public class EmailAddressListAttribute :…
Zhorian
  • 802
  • 1
  • 9
  • 15
1
vote
1 answer

Check from razor view if derived System.ComponentModel.DataAnnotations.ValidationAttribute exists

I'm implementing Html.EditorForModel() so that it's Bootstrap friendly. I have editor templates for all the data types, and one, Object.cshtml, that wraps each editor template with
etc. The issue is that when I have…
MikeT
  • 2,530
  • 25
  • 36
1
vote
3 answers

C# how to determine value wrapped within object is null or default?

I am currently writing on a custom required validation attribute, and I want to revert with the error when the value is null or Default. With Default I mean "" for String, 0 for int, 0.0 for double, null for object. And to achieve that, I am calling…
Mukesh Bhojwani
  • 2,014
  • 4
  • 20
  • 35
1
vote
1 answer

How to retrieve formcollection value or route data inside custom validation attribute in ASP.NET MVC

I am developing an Asp.NET MVC5 project. In my project, I am creating a custom validation attribute. But I am having problem with retrieving posted form data from custom attribute. I am not retrieving property value of ViewModel. I need to retrieve…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
1
vote
0 answers

How to create an custom remote validation attribute with dynamic additiional fields in ASP.NET MVC

I am developing an ASP.NET MVC5 project. In my project I am creating remote validation. For that I just created an custom validation attribute to support server-side validation. My remote validator working fine when remote action has to catch only…
1
vote
0 answers

Custom Validation Attribute - IsValid() parameter is null

I'm using Simon Ince's RequiredIf custom validation attribute implementation. For most situations it has been working, however, I have come into a situation that doesn't work and I can't seem to figure out why. Here is the relevant portion of the…
Dar
  • 383
  • 1
  • 5
  • 16