model-validation

Custom Model Validator for Integer value in ASP.NET Core Web API

情到浓时终转凉″ 提交于 2019-12-02 04:01:56
问题 I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code: public class ValidateIntegerValueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { int output; var isInteger = int.TryParse(value.ToString(), out

How to discern between model binding errors and model validation errors?

血红的双手。 提交于 2019-12-01 09:31:45
I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is syntactically correct but contains unacceptable values). As an example, given an action like [HttpPut("{id}")] public async Task<IActionResult> UpdateAsync( [FromRoute] int id, [FromBody] ThingModel model) I'd like to return a 400 status code when the id parameter in the route contains a non-digit character or when no body has been specified in the

How to discern between model binding errors and model validation errors?

倖福魔咒の 提交于 2019-12-01 06:37:57
问题 I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is syntactically correct but contains unacceptable values). As an example, given an action like [HttpPut("{id}")] public async Task<IActionResult> UpdateAsync( [FromRoute] int id, [FromBody] ThingModel model) I'd like to return a 400 status code when

Server side validation with custom DataAnnotationsModelValidatorProvider

烂漫一生 提交于 2019-11-30 15:28:34
I have setup a custom provider to allow setting validation attributes from a data store instead of in static code. Works great with the client side validation in my .NET MVC 4 project, but I am unable to get the server side validation to work. CustomModelValidatorProvider .cs: public class CustomModelValidatorProvider : DataAnnotationsModelValidatorProvider { protected override IEnumerable GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { // set attributes from datastore here return base.GetValidators(metadata, context, attributes); } } In my Global

How to do Integer model validation in asp.net mvc 2

假装没事ソ 提交于 2019-11-30 13:10:24
问题 I have a registration form and the user must enter the square footage of their house. I would like this value to be only an integer. Is there a way to validate this value using attributes asp.net mvc? 回答1: yes, it is, but you will have to make a flat version of the object you are wanting to create, because the validation with attributes only runs AFTER MVC has converted your data into the model. which, when your value is an int, will fail to validate if the user did not enter an int, and you

MVC .Net Core Model Validation - The value '' is invalid. Error

拥有回忆 提交于 2019-11-30 07:52:32
问题 I am trying to use Model Validation in MVC .Net Core and can't manage to replace this default error message 'The value '' is invalid'. In theory, we can replace our own custom error message by using ErrorMessage Annotation in the Model. But I couldn't find a way to make this one work. My Model [Required(ErrorMessage = "Date Required")] [DataType(DataType.Date, ErrorMessage = "Invalid Date Format")] [Display(Name = "Appointment Date")] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",

Server side validation with custom DataAnnotationsModelValidatorProvider

偶尔善良 提交于 2019-11-29 21:16:22
问题 I have setup a custom provider to allow setting validation attributes from a data store instead of in static code. Works great with the client side validation in my .NET MVC 4 project, but I am unable to get the server side validation to work. CustomModelValidatorProvider .cs: public class CustomModelValidatorProvider : DataAnnotationsModelValidatorProvider { protected override IEnumerable GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { // set

ASP.Net MVC 2 Model Validation Regex Validator fails

馋奶兔 提交于 2019-11-29 15:37:39
I have following property in my Model Metadata class: [Required(ErrorMessage = "Spent On is required")] [RegularExpression(@"[0-1][0-9]/[0-3][0-9]/20[12][0-9]", ErrorMessage = "Please enter date in mm/dd/yyyy format")] [DataType(DataType.Date)] [DisplayName("Spent On")] public DateTime SpentOn { get; set; } But whenever I call ModelState.IsValid it always returns false because regex is not validating. I have matched the entered date (08/29/2010) against new regex using same pattern and it matches perfectly. What am I doing wrong? Darin Dimitrov That's because regex applies to strings and not

MVC .Net Core Model Validation - The value '' is invalid. Error

╄→гoц情女王★ 提交于 2019-11-29 05:39:55
I am trying to use Model Validation in MVC .Net Core and can't manage to replace this default error message 'The value '' is invalid'. In theory, we can replace our own custom error message by using ErrorMessage Annotation in the Model. But I couldn't find a way to make this one work. My Model [Required(ErrorMessage = "Date Required")] [DataType(DataType.Date, ErrorMessage = "Invalid Date Format")] [Display(Name = "Appointment Date")] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime AppointmentDate { get; set; } I put different ErrorMessage for

Custom Model Validator for Integer value in ASP.NET Core Web API

假如想象 提交于 2019-11-28 13:54:16
I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code: public class ValidateIntegerValueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { int output; var isInteger = int.TryParse(value.ToString(), out output); if (!isInteger) { return new ValidationResult("Must be a Integer number"); } } return