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
2
votes
1 answer

Cannot resolve dependency in a custom ValidationAttribute, IServiceProvider is set to null on the ValidatationContontext parameter

An ASP.NET Core 2.1 MVC app, using Autofac following their documentation on setup (https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html). I am trying to resolve a dependency in a custom ValidationAttribute. The returned value from…
2
votes
0 answers

ASP.NET MVC 3 Validating ComplexModel.EntityCollection.Property1 for only the first X items in the collection with Custom ValidationAttribute

My model is described below. I'd like to only require a subset of Reference fields for the first 2 items in the Application.References collection. I'd like this to occur on both client side and server side validation. I have a working method that…
2
votes
1 answer

Testing custom validation of property depenent on other property returns ArgumentNullException

I'm trying to write an XUnit test to test my custom validator. The validator checks the value of other property which indicates if the validated property should be null or have a value. However the test returns ArgumentNullException when I'm using…
2
votes
1 answer

Custom ValidationAttribute ErrorMessage not working as expected

I have built a simple date check to validate if the date from the form is smaller then the current date. ValidationAttribute public class DateIsValidAttribute : ValidationAttribute { private const string DefaultErrorMessage = "Datumet är…
AllramEst
  • 1,319
  • 4
  • 23
  • 47
2
votes
1 answer

Custom Validation in MVC - Match New Email To Confirmed Email

I am trying to create a new custom validation in which I can compare two values within the same class. in this case its checking that when people change their email they have to enter it twice and those two values match. (Would do the same for…
Steve
  • 2,971
  • 7
  • 38
  • 63
2
votes
1 answer

Is it possible to bind A RequiredAttribute to a function?

public partial class TaskScheduling { public Guid EvaluationTemplateId { get; set; } //[Required(ErrorMessage = "TaskScheduling.OwnerUser")] [Required(ErrorMessage = ResolveErrorMessage())] public string SenderName { get; set; } …
Fran Zardoya
  • 165
  • 1
  • 1
  • 6
2
votes
1 answer

JQuery custom validation attribute MVC core

I try to add a custom attribute to validate required field and trim value for white space. So here is my custom attribute : [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class CustomRequired :…
2
votes
3 answers

Custom validation based on other value

I make a Booking form for restaurant, which asks for the name of the restaurant, the date of the meal and the number of person. I have a booking class, which has an ID, an ID of the restaurant, a date and a number of people : public class Booking { …
Matthieu Veron
  • 290
  • 6
  • 16
2
votes
1 answer

Pass variable data to ValidationAttribute

I have a requirement to apply different min length values in a change password function to a new password being created based on a user's roles. If a user has no administrative roles min length is 12, if they have admin roles min length is 16. The…
2
votes
2 answers

ASP.NET WEB API 2 - ModelBinding Firing twice per request

I have a custom validation attribute, that when I make a request to the server via a POST, is firing the IsValid method on the attribute twice. Its resulting in the error message returned to be duplicated. I've checked using Fiddler that the…
Derek
  • 8,300
  • 12
  • 56
  • 88
2
votes
1 answer

wcf generated classes and validation application block attributes

I'm new to the validation application block and trying to use it with wcf... I have a wcf service that has data objects with validation rules defined with attributes, using the validation application block . On my client side (WPF), I have a service…
2
votes
0 answers

MVC 4 Validation for Recursive relationship using ValidationAttribute

I have an MVC project that has several recursive relationships in different models. See sample model structure below. For example: tileid and parentid (a tile can be related to many tiles) regionid and parentid (a region can be related to many…
2
votes
1 answer

CustomTypeDescriptor with MVC Validation - How to get property value with property.GetValue(component)?

I've created custom TypeDescriptionProvider for one of my MVC models. I use it for dynamic assignment of ValidationAttribute. I use value of one property to decide what attributes to add to other properties. In web service, where I use…
2
votes
1 answer

Can't acces the ValidationParameters on .NET MVC client-side validation

I am coding a custom validation to my .NET MVC 4 application. This is the first validation that uses a parameter, and I'm finding some trouble to get this. This is my C# code: ValidationAttribute: namespace Validations { …
2
votes
1 answer

Change Validation For a Property in ASP.NET MVC 3 by Condition

This is my Model: [RegularExpression(@"^08[589][0-9]{8}$", ErrorMessage = "Invalid Number!")] public string Phone { get; set; } [ForeignKey] public long PhoneType { get; set; } // 1-CellPhone , 2-Phone So I think to change RegularExpression…
Saeid
  • 13,224
  • 32
  • 107
  • 173