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
2 answers

Data annotations, unobtrusive validation. Min age, max age

What do I want to achieve: To show a seperate validation message for failing minimum age check and one for maximum age check To store the minimum and maximum age in one area as integers. Not in js/ validator... only in the model. which I hope to…
1
vote
0 answers

RegularExpressionAttribute fails validating right data

I have a regular expression that works great when I try it: System.Text.RegularExpressions.Regex.IsMatch("universal",@"^[A-Za-z0-9 ._’&-/s]{0,100}$") true System.Text.RegularExpressions.Regex.IsMatch("universal £$%$£%",@"^[A-Za-z0-9…
vtortola
  • 34,709
  • 29
  • 161
  • 263
1
vote
0 answers

Custom validation attributes for blazor, not validating

FIXED Instead of using ValidationResult? IsValid(object?, ValidationContext) i just used bool IsValid(object), and that seems to be working fine. I've created a custom validation attribute for email, but it does not validate as it should. Atm, i…
WhySoShy
  • 21
  • 3
1
vote
1 answer

Web API attribute validation does not return all errors

I have an ASP.NET Core 6 Web API project with all controllers decorated with the [ApiController] annotation. I use validation annotations like [Required], [MaxLength] etc. to validate properties of DTOs received in incoming requests. Some of my DTOs…
Andrew
  • 1,139
  • 1
  • 12
  • 27
1
vote
1 answer

Validation Context is always NULL

I have implemented a custom validation attribute to check column uniqueness. I want to check if the provided value already exists in the database or not. Here is my code: [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited =…
1
vote
1 answer

Why should Custom Validation Attributes be sealed in C#

I'm trying to create a custom C# Validation Attribute(without sealed keyword) for Data Annotation. public class MyValidatorAttribute : ValidationAttribute { } But Microsoft page here says "Make the class not inheritable." why? Do I overlooked…
191180rk
  • 735
  • 2
  • 12
  • 37
1
vote
1 answer

Custom Validation Attribute: Comparing one property to another property's inner property

I have a class StarActivityModel, and I want to validate that the inputted value for StarChange, is less than the Client's property of StarCount. To do this I have attempted to create a Custom Validation Attribute but am having trouble getting the…
1
vote
1 answer

Change Response Status Code from ValidationAttribute

In an aspnet core web api project I have the logic if(!_entityService.Exists(entityId)) return NotFound($"{entityId} not found."); Scattered across many endpoints/controllers. I would like to refactor this cross-cutting concern to an…
Ned Grady
  • 33
  • 3
1
vote
2 answers

Add Custom ValidationAttribute to Swagger Documentation

I have a custom validation attribute Lets say I have a HelloWorld class that implements ValidationAttribute. I then apply this attribute to a field within my API. [HelloWorld] public string FirstName { get; set; } When I generate the Swagger UI, I…
1
vote
0 answers

ASP MVC ICollection Length Validation - Preventing Form Submission

This question is somewhat of a follow-up to this question about making a custom validation for the length of an ICollection. I created a custom DataAnnotation class according to the post I referenced, which looks like this: /// /// Require…
1
vote
0 answers

Client-Side Validation of Child Properties in MVC Core 5

In my ASP.NET MVC 5 (C#) application, I have a class called Name: public class Name { public virtual string? First { get; set; } public virtual string? Middle { get; set; } public virtual string? Last { get; set; } public virtual…
dhughes
  • 295
  • 5
  • 10
1
vote
1 answer

How can I cleanly share a Custom Validator (ValidationAttribute) for a data model, a ViewModel, and a DTO in ASP.NET

I'm doing a ASP.NET MVC course. I'm building a REST Web API using ASP.NET WebAPI 2. The application also contains standard MVC 5 views. I'm using DTOs (Data Transfer Objects) to decouple the API from the data model. I've made a custom…
1
vote
1 answer

Adding custom JsonConverter to Web API impacts string value passed to custom Validation Attribute

I have an ASP.NET Web API that accepts json requests. The default JsonMediaTypeFormatter is used. configuration.Formatters.Add(new JsonMediaTypeFormatter()); On the model I use a custom ValidationAttribute to ensure a string value provided matches…
DeMaki
  • 371
  • 4
  • 15
1
vote
1 answer

How to format error message from a custom validator

I have created a custom validator this way: public class IntArrayRequiredAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if ((!(value is int[] array)…
1
vote
1 answer

Passing data from custom validation attribute to page/view

Let's say I have a custom validation attribute: public class CustomAttribute : ValidationAttribute { public override string FormatErrorMessage(string name) { return string.Format(CultureInfo.CurrentCulture, ErrorMessageString,…
LeonidasFett
  • 3,052
  • 4
  • 46
  • 76