validationattribute

C# Custom Attribute Required If

随声附和 提交于 2019-12-02 16:21:36
问题 I am just getting into custom attributes, and I absolutely love them. I am wondering if it is possible to create an attribute that gets applied to a property and denotes the name of another property in the same object. If would check to see if the referenced property has a value, and if so, the decorated attribute would be required. Something like this: [RequiredIfNotNull("ApprovedDate")] [DisplayName("Approved By")] [StringLength(50, ErrorMessage = "{0} must not exceed {1} characters")]

C# Custom Attribute Required If

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 13:38:20
I am just getting into custom attributes, and I absolutely love them. I am wondering if it is possible to create an attribute that gets applied to a property and denotes the name of another property in the same object. If would check to see if the referenced property has a value, and if so, the decorated attribute would be required. Something like this: [RequiredIfNotNull("ApprovedDate")] [DisplayName("Approved By")] [StringLength(50, ErrorMessage = "{0} must not exceed {1} characters")] public string ApprovedBy { get; set; } [DisplayName("Approved Date")] [DisplayFormat(DataFormatString = "{0

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

半腔热情 提交于 2019-11-30 20:32:12
We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me. As an example I have a model such as: public class HobbyModel { [Required(AllowEmptyStrings = false, ErrorMessage = "Do not allow empty strings")] [DisplayName("Hobby")] [DataType(DataType.Text)]

Custom ValidationAttribute test against whole model

孤者浪人 提交于 2019-11-30 09:55:48
问题 I know this is probably not possible but let's say I have a model with two properties. I write a ValidationAttribute for one of the properties. Can that VA look at the other property and make a decision? So; public class QuickQuote { public String state { get; set; } [MyRequiredValidator(ErrorMessage = "Error msg")] public String familyType { get; set; } So in the above example, can the validator test to see what's in the "state" property and take that into consideration when validating

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

时光总嘲笑我的痴心妄想 提交于 2019-11-30 05:05:16
问题 We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0. At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me. As an example I have a model such as: public class HobbyModel { [Required(AllowEmptyStrings =

Validating Enum Values within C# MVC. Partial validation occurs - How to change validation behaviour?

為{幸葍}努か 提交于 2019-11-30 01:28:36
问题 I've been representing an enum within my razor view as a hidden field, which is posted back to an action result. I've noticed that when it binds the string value provided within the HTML, it automatically validates the value for the enum. /// <summary> /// Quiz Types Enum /// </summary> public enum QuizType { /// <summary> /// Scored Quiz /// </summary> Scored = 0, /// <summary> /// Personality Type Quiz /// </summary> Personality = 1 } Razor: @Html.HiddenFor(x => x.QuizType) Rendered HTML:

Custom ValidationAttribute test against whole model

ぃ、小莉子 提交于 2019-11-29 18:07:10
I know this is probably not possible but let's say I have a model with two properties. I write a ValidationAttribute for one of the properties. Can that VA look at the other property and make a decision? So; public class QuickQuote { public String state { get; set; } [MyRequiredValidator(ErrorMessage = "Error msg")] public String familyType { get; set; } So in the above example, can the validator test to see what's in the "state" property and take that into consideration when validating "familyType"? I know I can probably save the object to the session but would like to avoid any saving of

Phone Number Validation MVC

此生再无相见时 提交于 2019-11-27 05:22:08
问题 I am trying to use a regular expression to validate a phone number and return an error when an invalid number or phone number is submitted. MVC Code : <ol class="row"> <li class="cell" style="width: 20%;">Phone Number:</li> <li class="cell last" style="width: 60%;"> @Html.TextBoxFor(model => model.PhoneNumber, new { @class = "textbox" }) @Html.ValidationMessageFor(model => model.PhoneNumber) </li> </ol> C# Code : [DataType(DataType.PhoneNumber)] [Display(Name = "Phone Number")] [Required

Custom validation attribute that compares the value of my property with another property&#39;s value in my model class

送分小仙女□ 提交于 2019-11-26 15:21:39
I want to create a custom validation attribute, in which I want to compare the value of my property with another property's value in my model class. For example I have in my model class: ... public string SourceCity { get; set; } public string DestinationCity { get; set; } And I want to create a custom attribute to use it like this: [Custom("SourceCity", ErrorMessage = "the source and destination should not be equal")] public string DestinationCity { get; set; } //this wil lcompare SourceCity with DestinationCity How can I get there? Here's how you could obtain the other property value: public

Custom validation attribute that compares the value of my property with another property&#39;s value in my model class

浪子不回头ぞ 提交于 2019-11-26 03:39:14
问题 I want to create a custom validation attribute, in which I want to compare the value of my property with another property\'s value in my model class. For example I have in my model class: ... public string SourceCity { get; set; } public string DestinationCity { get; set; } And I want to create a custom attribute to use it like this: [Custom(\"SourceCity\", ErrorMessage = \"the source and destination should not be equal\")] public string DestinationCity { get; set; } //this wil lcompare