model-validation

Array must contain 1 element

老子叫甜甜 提交于 2019-11-28 11:53:14
I have the following class: public class CreateJob { [Required] public int JobTypeId { get; set; } public string RequestedBy { get; set; } public JobTask[] TaskDescriptions { get; set; } } I'd like to have a data annotation above TaskDescriptions so that the array must contain at least one element? Much like [Required] . Is this possible? I've seen a custom validation attribute used for this before, like this: (I've given sample with a list but could be adapted for array or you could use list) public class MustHaveOneElementAttribute : ValidationAttribute { public override bool IsValid(object

Model state validation in unit tests

社会主义新天地 提交于 2019-11-28 06:48:34
I am writing a unit test for a controller like this: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } the model looks like: public class LoginModel { [Required] public string Username { set; get; } [Required] public string Password { set; get; } } Then I have unit test like this one: [TestMethod] public void TestLogin_InvalidModel() { AccountController controller = CreateAccountController(); ... var response = controller.PostLogin(new LoginModel() { }); Assert.AreEqual(HttpStatusCode.BadRequest,

Model Validation / ASP.NET MVC 3 - Conditional Required Attribute

僤鯓⒐⒋嵵緔 提交于 2019-11-27 18:02:09
I'm having trouble with my ASP.NET MVC 3 application. I have 2 propertiesin my model whereby I only want 1 of them required in my view based on whichever one is empty. So for example, if I enter a phone number then email is no longer required and vice versa, but if I leave both empty, then either 1 should be required, below is my model: [Display(Name = "Contact Phone Number:")] [MaxLength(150)] public string ContactPhoneNumber { get; set; } [Display(Name = "Contact Email Address:")] [MaxLength(100)] public string ContactEmailAddress { get; set; } Would I need to create a custom attribute to

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

北城余情 提交于 2019-11-27 07:08:02
问题 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

Array must contain 1 element

十年热恋 提交于 2019-11-27 06:34:35
问题 I have the following class: public class CreateJob { [Required] public int JobTypeId { get; set; } public string RequestedBy { get; set; } public JobTask[] TaskDescriptions { get; set; } } I'd like to have a data annotation above TaskDescriptions so that the array must contain at least one element? Much like [Required] . Is this possible? 回答1: I've seen a custom validation attribute used for this before, like this: (I've given sample with a list but could be adapted for array or you could use

MaxLength Attribute not generating client-side validation attributes

*爱你&永不变心* 提交于 2019-11-27 03:47:59
I have a curious problem with ASP.NET MVC3 client-side validation. I have the following class: public class Instrument : BaseObject { public int Id { get; set; } [Required(ErrorMessage = "Name is required.")] [MaxLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")] public string Name { get; set; } } From my view: <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> And here's the generated HTML I get for the textbox for this field: <input class="text-box single-line" data-val="true" data-val-required="Name is

Model state validation in unit tests

人走茶凉 提交于 2019-11-27 01:30:29
问题 I am writing a unit test for a controller like this: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } the model looks like: public class LoginModel { [Required] public string Username { set; get; } [Required] public string Password { set; get; } } Then I have unit test like this one: [TestMethod] public void TestLogin_InvalidModel() { AccountController controller = CreateAccountController(); ... var

MVC model validation for date

风流意气都作罢 提交于 2019-11-26 23:10:23
Is there any default validation for MVC 5 where I can set min and max value of date? In my model i want date validation public class MyClass { [Required(ErrorMessage="Start date and time cannot be empty")] //validate:Must be greater than current date [DataType(DataType.DateTime)] public DateTime StartDateTime { get; set; } [Required(ErrorMessage="End date and time cannot be empty")] //validate:must be greater than StartDate [DataType(DataType.DateTime)] public DateTime EndDateTime { get; set; } } Ps: According to this Asp.Net Website , there is a problem in using the Range validator for

ViewModel validation for a List

那年仲夏 提交于 2019-11-26 21:29:49
I have the following viewmodel definition public class AccessRequestViewModel { public Request Request { get; private set; } public SelectList Buildings { get; private set; } public List<Person> Persons { get; private set; } } So in my application there must be at least 1 person for an access request. What approach might you use to validate? I don't want this validation to happen in my controller which would be simple to do. Is the only choice a custom validation attribute? Edit: Currently performing this validation with FluentValidation (nice library!) RuleFor(vm => vm.Persons) .Must((vm,

MaxLength Attribute not generating client-side validation attributes

我的梦境 提交于 2019-11-26 12:40:26
问题 I have a curious problem with ASP.NET MVC3 client-side validation. I have the following class: public class Instrument : BaseObject { public int Id { get; set; } [Required(ErrorMessage = \"Name is required.\")] [MaxLength(40, ErrorMessage = \"Name cannot be longer than 40 characters.\")] public string Name { get; set; } } From my view: <div class=\"editor-field\"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> And here\'s the generated HTML I get