validationattribute

DataAnnotations “NotRequired” attribute

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 03:42:28
问题 I've a model kind of complicated. I have my UserViewModel which has several properties and two of them are HomePhone and WorkPhone . Both of type PhoneViewModel . In PhoneViewModel I have CountryCode , AreaCode and Number all strings. I want to make the CountryCode optional but AreaCode and Number mandatory. This works great. My problem is that in the UserViewModel WorkPhone is mandatory, and HomePhone is not. Is there anyway I can dissable Require attributs in PhoneViewModel by setting any

Get Data Annotations attributes from model

帅比萌擦擦* 提交于 2019-12-19 05:59:14
问题 I want to create custom client-side validator, but I want define validation rules via Data Annotations attributes at business logic layer. How can I access model validation attributes in runtime? I want to write 'generator', which will convert this code: public class LoginModel { [Required] [MinLength(3)] public string UserName { get; set; } [Required] public string Password { get; set; } } into this one: var loginViewModel= { UserName: ko.observable().extend({ minLength: 3, required: true })

How can I have a custom ValidationAttribute rendered as a 'data-val-xx' attribute on the client-side?

这一生的挚爱 提交于 2019-12-18 12:32:57
问题 Given a ViewModel that looks like this: public class Login { [Required] public string Username { get; set; } [Required, CustomValidator] public string Password { get; set; } } And a View like this (Razor syntax here): @Html.TextBoxFor(f => f.Password) I am getting the following markup: <input type="text" value="" data-val-required="This field is required." /> However I would like it to also include a 'data-' attribute for my custom validator. I want something like this: <input type="text"

Autofac and DI for ValidationAttribute

假如想象 提交于 2019-12-13 22:01:07
问题 I have the following validation attribute class: public class ZipCodeValidationAttribute : ValidationAttribute { private readonly IValidationRepository _repository; public override bool IsValid(object value) { var repository = _repository; return repository.IsPostalCodeValid((string) value); } } To test I am trying to use Autofac as my IOC and use property injection. I've set up the test as follows: [TestMethod] public void When_PostalCodeAttribute_Given_ValidPostalCode_Then_SystemReturnsTrue

How to create an custom remote validation attribute with dynamic additiional fields in ASP.NET MVC

本小妞迷上赌 提交于 2019-12-11 13:46:39
问题 I am developing an ASP.NET MVC5 project. In my project I am creating remote validation. For that I just created an custom validation attribute to support server-side validation. My remote validator working fine when remote action has to catch only one field. But I am having problems when I try to bind additional field for it. I mean when I validate with extra fields. Here is my custom remote validation attribute public class RemoteClientServerAttribute : RemoteAttribute { protected override

Custom remote validation attribute throwing error at server side in ASP.NET MVC

五迷三道 提交于 2019-12-08 12:18:52
问题 I am developing an ASP.NET MVC Web Application. In my project I am doing remote validation using data annotation to my view model class. I know default remote attribute does not support server validation. I can validate it again in action method. But I do not want to do that it is violating separation of concerns. So I tried to create custom server client remote validation attribute. I found a code online and I used it. But it is giving me error when server validation is occurred. This is my

Limitation of ModelState.IsValid in ASP.NET MVC 3

岁酱吖の 提交于 2019-12-07 09:04:35
问题 I always use ModelState.IsValid for check all of my model validation validated correctly in Server Side, but I think there is a limitation to use this. For example I define a Remote Validation attribute, but if I disable javascript then ModelState.IsValid don't check Remote Validation and always return true, Where is the problem? this is a limitation for ModelState.IsValid or is my fault? If necessary I can Add all my implementation. 回答1: This question has come around a few times. The answer

Limitation of ModelState.IsValid in ASP.NET MVC 3

杀马特。学长 韩版系。学妹 提交于 2019-12-05 12:33:31
I always use ModelState.IsValid for check all of my model validation validated correctly in Server Side, but I think there is a limitation to use this. For example I define a Remote Validation attribute, but if I disable javascript then ModelState.IsValid don't check Remote Validation and always return true, Where is the problem? this is a limitation for ModelState.IsValid or is my fault? If necessary I can Add all my implementation. Styxxy This question has come around a few times. The answer is: it doesn't validate on the server-side, you have to perform the validation action yourself. See

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

不问归期 提交于 2019-12-04 05:42:31
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.")] [StringLength(25, ErrorMessage="Max 25 chars.")] public string Firstname{get;set;} } Example of

DataAnnotations “NotRequired” attribute

不羁的心 提交于 2019-12-03 11:05:59
I've a model kind of complicated. I have my UserViewModel which has several properties and two of them are HomePhone and WorkPhone . Both of type PhoneViewModel . In PhoneViewModel I have CountryCode , AreaCode and Number all strings. I want to make the CountryCode optional but AreaCode and Number mandatory. This works great. My problem is that in the UserViewModel WorkPhone is mandatory, and HomePhone is not. Is there anyway I can dissable Require attributs in PhoneViewModel by setting any attributes in HomeWork property? I've tried this: [ValidateInput(false)] but it is only for classes and