validationattribute

GetClientValidationRules is never called in MVC application

穿精又带淫゛_ 提交于 2021-01-22 19:51:45
问题 I have a custom ValidationAttribute that implements IClientValidatable. But the GetClientValidationRules is never called to actually output the validation rules to the client side. There is nothing special about the attribute but for some reason it is never called. I've tried registering an adapter in Application_Start() but that also doesnt work. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class CustomAttribute : ValidationAttribute, IClientValidatable { public

ASP.Net MVC Custom client side validation not firing

我怕爱的太早我们不能终老 提交于 2020-01-16 03:51:13
问题 i have the below jquery unobtrusive code which is not firing. $.validator.unobtrusive.adapters.add('customvalidation', ['productname'], function (options) { options.rules['customvalidation'] = { productname: options.params.productname }; }); $.validator.addMethod("customvalidation", function (value, element, param) { alert(param.productname); return false; }); but the above code suppose to show alert i guess when pressing button to submit my form. here is my full code Model and view model

How to serialize a model with all validation attributes from the individual properties?

你。 提交于 2020-01-01 09:56:11
问题 Context: creating a jsonP service with mvc controller methods which provides a definition of formfields including all validation rules. My problem is that I do not know how to serialize the validation attributes. I prefer the validation attributes in the same format as they are serialized by Razor when using unobtrusive validation in regular Mvc views. For serializing to json I use NewtonSoft.Json (4.0.2). Example of model: public class Profile{ [Required(ErrorMessage="This field is required.

How to localize standard error messages of validation attributes in ASP.NET Core

廉价感情. 提交于 2019-12-25 01:47:25
问题 How to localize standard error messages of validation attributes in ASP.NET Core (v2.2)? For Example, [Required] attribute has this error message " The xxx field is required. "; [EmailAddress] has " The xxx field is not a valid e-mail address. "; [Compare] has " 'xxx' and 'yyy' do not match. " and so on. In our project we use not English language and I want to find a way how to translate standard error messages without writing them directly in every attribute of every data-model class 回答1:

Asp.Net MVC 3.0 Model Localization With RegularExpression Attribute

老子叫甜甜 提交于 2019-12-24 17:08:19
问题 I've written a custom error message localization logic in my custom DataAnnotationsModelMetadataProvider class. It's working just fine with build-in StringLengthAttribute or RequiredAttribute validation error messages. But i am having trouble with my custom derived RegularExpressionAttribute classes. The logic i am using is something like below : public class AccountNameFormatAttribute : RegularExpressionAttribute { public AccountNameFormatAttribute() : base(Linnet.Core.Shared.RegExPatterns

Asp.Net MVC 3.0 Model Localization With RegularExpression Attribute

半腔热情 提交于 2019-12-24 17:04:15
问题 I've written a custom error message localization logic in my custom DataAnnotationsModelMetadataProvider class. It's working just fine with build-in StringLengthAttribute or RequiredAttribute validation error messages. But i am having trouble with my custom derived RegularExpressionAttribute classes. The logic i am using is something like below : public class AccountNameFormatAttribute : RegularExpressionAttribute { public AccountNameFormatAttribute() : base(Linnet.Core.Shared.RegExPatterns

C# Unable to validate property using a custom validate attribute

南楼画角 提交于 2019-12-24 11:35:43
问题 I have a validation class: public sealed class ValidationSet : ValidationAttribute { private List<string> _set = new List<string>(); public ValidationSet(params string[] setOfValues) { _set = setOfValues.ToList(); } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (!(_set.Select(s => s.ToLower()).Contains(value.ToString().ToLower()))) { throw new Exception("not in the set"); } return ValidationResult.Success; } } and here is how I use it:

Custom validation based on other value

佐手、 提交于 2019-12-24 02:52:01
问题 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 { public int Id { get; set; } public int IDRestaurant{ get; set; } [CustomPlaceValidator] public int Nbpeople { get; set; } [CustomDateValidator] public DateTime Date { get; set; } } As well as a Resto class, which has an ID, a name, phone number and

Pass variable data to ValidationAttribute

对着背影说爱祢 提交于 2019-12-24 00:36:23
问题 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 current code has no such variable requirement logic. The implementation for the new password property was like so in model class called ChangePasswordData: ///summary> /// Gets and sets the new Password. /// </summary> [Display(Order = 3, Name =

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

落花浮王杯 提交于 2019-12-22 18:45:24
问题 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 request is only ever fired once, so the situation is 1 request with model binding firing twice. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class MinimumAgeAttribute :