I'm working on a Web API 2 project. besides the requirement that some properties are required, some only can have specific values.
One option is that I could try to save the model to the database (EF6) and create some logic while saving, but I think it is better to validate if the correct value is set before I make a call to the database. Does data annotations provide an attribute like Range but then for specific string values like in the example below? Or do I have to write my own validator attribute?
public class Person {
public int PersonID { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
[StringRange("M","F")]
public string Gender { get; set; }
}
In the above example, when a post is done to the controller, the only values to accept are "M" or "F".
In case anyone stumbles upon this thread in the future, I took it a little further and added a public string array property accepting the allowable values in the validation filter. This will allow you to provide a collection of valid strings in the attribute decorator.
This way, you now have a generic reusable attribute you can use anytime to limit a model string property to a predefined set of values. Since it's a validation attribute, you can decorate it with a more informative error message as well.
Example Usage:
public class Person {
[StringRange(AllowableValues = new[] { "M", "F" }, ErrorMessage = "Gender must be either 'M' or 'F'.")]
public string Gender { get;set; }
}
String Attribute:
public class StringRangeAttribute : ValidationAttribute
{
public string[] AllowableValues { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (AllowableValues?.Contains(value?.ToString()) == true)
{
return ValidationResult.Success;
}
var msg = $"Please enter one of the allowable values: {string.Join(", ", (AllowableValues ?? new string[] { "No allowable values found" }))}.";
return new ValidationResult(msg);
}
}
To validate the Gender property I've created a custom validation attribute by creating a new class (attribute):
using System;
using System.Collections.Generic;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MyProject.Models.Validation
{
public class StringRangeAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if(value.ToString() == "M" || value.ToString() == "F")
{
return ValidationResult.Success;
}
return new ValidationResult("Please enter a correct value");
}
}
}
you can use reqular expression like this:
[RegularExpression("M|F", ErrorMessage = "The Gender must be either 'M' or 'F' only.")]
public string Gender { get; set; }
but in my api it will show error message when i passed data
so you can add
[StringLength(1, MinimumLength = 1, ErrorMessage = "The Gender must be 1 characters.")]
final code:
[StringLength(1, MinimumLength = 1, ErrorMessage = "The Gender must be 1 characters.")]
[RegularExpression("M|F", ErrorMessage = "The Gender must be either 'M' or 'F' only.")]
public string Gender { get; set; }
[Required]
[RegularExpression("value1|value2|value3|...", ErrorMessage = "YourMessageHere")]
public string Gender { get; set; }
Related
Using .Net and Newtonsoft Json, how can I hide a model property on serialization, however, be able to populate that property and utilize the value passed.
E.g.
[JsonIgnore]
public Int16 Value { get; set; }
This hides the property on output, however, I cannot set the value on model POST. How can I hide the property on output but allow the property to be set on a POST or PUT request?
POST Example:
{
"Name": "King James Version",
"Abbreviation" : "kjv",
"Publisher": "Public Domain",
"Copyright": "This is the copyright",
"Notes": "This is the notes.",
"TextDirection" : "LTR"
}
PUT example:
{
"ID" : 1,
"Name": "King James Version",
"Abbreviation" : "kjv",
"Publisher": "Public Domain",
"Copyright": "This is the copyright",
"Notes": "This is the notes.",
"TextDirection" : "LTR"
}
Business Logic:
ID should not be passed in POST request and will be ignored if passed.
Abbreviation is required for POST request and will be validated against database using custom Validation Filter attribute.
Abbreviation cannot be passed on PUT request because that field/property cannot be updated.
ID must be passed in the PUT request to identity in the custom validator that it is a PUT request and not a POST request.
Model:
namespace Library.Models.Bible
{
public class BibleModel : IPopulatable<BibleModel, DataTable>, IPopulatable<BibleModel, DataRow>
{
public Int32? ID { get; set; }
[MinLength(4, ErrorMessage = "Bible name must be between 4-100 characters")]
[MaxLength(100, ErrorMessage = "Bible name must be between 4-100 characters")]
public String Name { get; set; }
[MinLength(3, ErrorMessage = "Bible abbreviation must be between 3-9 characters")]
[MaxLength(9, ErrorMessage = "Bible abbreviation must be between 3-9 characters")]
[ValidateBibleAbbreviationExists]
public String Abbreviation { get; set; }
[MaxLength(250, ErrorMessage = "Publisher may not exceed 250 characters")]
public String Publisher { get; set; }
[MaxLength(3000, ErrorMessage = "Copyright may not exceed 3000 characters")]
public String Copyright { get; set; }
[MaxLength(3000, ErrorMessage = "Notes may not exceed 3000 characters")]
public String Notes { get; set; }
[EnumDataType(typeof(Enums.eTxtDir), ErrorMessage = "Text direction does not exist. Allowed values: LTR, RTL")]
public String TextDirection { get; set; }
[JsonIgnore]
public Int16 Active { get; set; } = 1;
public BibleModel Populate(DataTable dT)
{
if (dT != null && dT.Rows.Count > 0)
return Populate(dT.Rows[0]);
return null;
}
public BibleModel Populate(DataRow ro)
{
if(ro != null)
{
this.ID = Convert.ToInt32(ro["Bible_ID"]);
this.Name = ro["Name"].ToString();
this.Abbreviation = ro["Abbr"].ToString();
this.Publisher = ro["Publisher"].ToString();
this.Copyright = ro["Copyright"].ToString();
this.Notes = ro["Notes"].ToString();
this.TextDirection = ro["TxtDir"].ToString();
return this;
}
return null;
}
}
There are several possibilities (https://www.newtonsoft.com/json/help/html/ConditionalProperties.htm)
1) Write a Custom Contract Resolver
class MyContractResolver: DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.ShouldSerialize = String.Compare(property.PropertyName, "Value") != 0;
return property;
}
}
2) Add a ShouldSerialize... method to your class
class MyClass {
public Int16 Value {get;set;}
public bool ShouldSerializeValue() {return false;}
}
Is there any way of using data annotations to compare two form field (eg. to confirm an email address) are the same, before allowing the form to be posted?
eg. can the regular expression data annotation use the match function to reference another property in a ViewModel?
Use the CompareAttribute
public string EmailAddress {get; set;}
[Compare(nameof(EmailAddress), ErrorMessage = "Emails mismatch")]
public string VerifiedEmailAddress { get; set; }
As one possibe option self-validation:
Implement an interface IValidatableObject with method Validate, where you can put your validation code.
public class TestModel : IValidatableObject
{
public string Email{ get; set; }
public string ConfirmEmail { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Email != ConfirmEmail)
{
yield return new ValidationResult("Emails mismatch", new [] { "ConfirmEmail" });
}
}
}
Please notice: this is only server-side validation.
I have a Model with 4 properties which are of type string. I know you can validate the length of a single property by using the StringLength annotation. However I want to validate the length of the 4 properties combined.
What is the MVC way to do this with data annotation?
I'm asking this because I'm new to MVC and want to do it the correct way before making my own solution.
You could write a custom validation attribute:
public class CombinedMinLengthAttribute: ValidationAttribute
{
public CombinedMinLengthAttribute(int minLength, params string[] propertyNames)
{
this.PropertyNames = propertyNames;
this.MinLength = minLength;
}
public string[] PropertyNames { get; private set; }
public int MinLength { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
var values = properties.Select(p => p.GetValue(validationContext.ObjectInstance, null)).OfType<string>();
var totalLength = values.Sum(x => x.Length) + Convert.ToString(value).Length;
if (totalLength < this.MinLength)
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
return null;
}
}
and then you might have a view model and decorate one of its properties with it:
public class MyViewModel
{
[CombinedMinLength(20, "Bar", "Baz", ErrorMessage = "The combined minimum length of the Foo, Bar and Baz properties should be longer than 20")]
public string Foo { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
Self validated model
Your model should implement an interface IValidatableObject. Put your validation code in Validate method:
public class MyModel : IValidatableObject
{
public string Title { get; set; }
public string Description { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Title == null)
yield return new ValidationResult("*", new [] { nameof(Title) });
if (Description == null)
yield return new ValidationResult("*", new [] { nameof(Description) });
}
}
Please notice: this is a server-side validation. It doesn't work on client-side. You validation will be performed only after form submission.
ExpressiveAnnotations gives you such a possibility:
[Required]
[AssertThat("Length(FieldA) + Length(FieldB) + Length(FieldC) + Length(FieldD) > 50")]
public string FieldA { get; set; }
To improve Darin's answer, it can be bit shorter:
public class UniqueFileName : ValidationAttribute
{
private readonly NewsService _newsService = new NewsService();
public override bool IsValid(object value)
{
if (value == null) { return false; }
var file = (HttpPostedFile) value;
return _newsService.IsFileNameUnique(file.FileName);
}
}
Model:
[UniqueFileName(ErrorMessage = "This file name is not unique.")]
Do note that an error message is required, otherwise the error will be empty.
Background:
Model validations are required for ensuring that the received data we receive is valid and correct so that we can do the further processing with this data. We can validate a model in an action method. The built-in validation attributes are Compare, Range, RegularExpression, Required, StringLength. However we may have scenarios wherein we required validation attributes other than the built-in ones.
Custom Validation Attributes
public class EmployeeModel
{
[Required]
[UniqueEmailAddress]
public string EmailAddress {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public int OrganizationId {get;set;}
}
To create a custom validation attribute, you will have to derive this class from ValidationAttribute.
public class UniqueEmailAddress : ValidationAttribute
{
private IEmployeeRepository _employeeRepository;
[Inject]
public IEmployeeRepository EmployeeRepository
{
get { return _employeeRepository; }
set
{
_employeeRepository = value;
}
}
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var model = (EmployeeModel)validationContext.ObjectInstance;
if(model.Field1 == null){
return new ValidationResult("Field1 is null");
}
if(model.Field2 == null){
return new ValidationResult("Field2 is null");
}
if(model.Field3 == null){
return new ValidationResult("Field3 is null");
}
return ValidationResult.Success;
}
}
Hope this helps. Cheers !
References
Code Project - Custom Validation Attribute in ASP.NET MVC3
Haacked - ASP.NET MVC 2 Custom Validation
A bit late to answer, but for who is searching.
You can easily do this by using an extra property with the data annotation:
public string foo { get; set; }
public string bar { get; set; }
[MinLength(20, ErrorMessage = "too short")]
public string foobar
{
get
{
return foo + bar;
}
}
That's all that is too it really. If you really want to display in a specific place the validation error as well, you can add this in your view:
#Html.ValidationMessage("foobar", "your combined text is too short")
doing this in the view can come in handy if you want to do localization.
Hope this helps!
In my multi country site, I have a form for address creation. I want customize my validation rules by country (For example : FR ZipCode length 8, USA : 10...), and I hesitate between many solution : one ViewModel by Country, Parameterized annotation, DataFilter... Any idea ?
public class Address
{
[Required]
public string Name { get; set; }
[StringLength(lengthByCountry)]
public string ZipCode { get; set; }
}
You can use the CustomValidationAttribute to write your own validator that does country-specific validation of ZIP codes depending on the country.
If the country and zip code need to be specified within the same submission, then I'd suggest making your model implement IValidatableObject, which allows you to validate based on combinations of values.
public class Address : IValidatableObject
{
[Required]
public string Name { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
switch (Country)
{
case "France":
if (ZipCode.Length < 8)
results.Add(
new ValidationResult("French zip codes must be at least 8 characters", new List<string> { "ZipCode" })
);
break;
case "U.S.":
if (ZipCode.Length < 10)
results.Add(
new ValidationResult("American zip codes must be at least 10 characters", new List<string> { "ZipCode" })
);
break;
// Etc.
}
return results;
}
}
I'm trying to write my own Custom Validation attribute but I'm having some problems.
The attribute I'm trying to write is that when a user logs in, the password will be compared against the confirmation password.
namespace Data.Attributes
{
public class ComparePassword : ValidationAttribute
{
public string PasswordToCompareWith { get; set; }
public override bool IsValid(object value)
{
if (PasswordToCompareWith == (string)value)
{
return true;
}
return false;
}
}
Now my problem is when i'm trying to set the attribute like this in the model file:
[Required]
[ComparePassword(PasswordToCompareWith=ConfirmPassword)]
public string Password { get; set; }
[Required]
public string ConfirmPassword { get; set; }
}
I get the following error:
Error 1 An object reference is required for the non-static field, method,
or property 'Project.Data.Models.GebruikerRegistreerModel.ConfirmPassword.get'
It seems that VS is not accepting the confirmpassword in the PasswordToCompareWith=ConfirmPassword part.
What am I doing wrong?
According to this link http://devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1 there is an special validation attribute now in MVC3:
public class RegisterModel
{
// skipped
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation do not match.")]
public string ConfirmPassword { get; set; }
}
CompareAttribute is a new, very useful validator that is not actually
part of
System.ComponentModel.DataAnnotations,
but has been added to the
System.Web.Mvc DLL by the team. Whilst
not particularly well named (the only
comparison it makes is to check for
equality, so perhaps EqualTo would be
more obvious), it is easy to see from
the usage that this validator checks
that the value of one property equals
the value of another property. You can
see from the code, that the attribute
takes in a string property which is
the name of the other property that
you are comparing. The classic usage
of this type of validator is what we
are using it for here: password
confirmation.
Sorry to disappoint you but handling such a simple case like yours using Data Annotations could be a pain. You may take a look at this post.
FoolProof http://foolproof.codeplex.com/ seems to be the best solution.
public class SignUpViewModel
{
[Required]
public string Password { get; set; }
[EqualTo("Password", ErrorMessage="Passwords do not match.")]
public string RetypePassword { get; set; }
}
It is better than suggested PropertiesMustMatchAttribute as it adds the validation error for the "RetypePassword' instead of the global model level as PropertiesMustMatchAttribute does.
I don't know why this is made out to be such a big deal, just do this:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
public class ComparePassword: ValidationAttribute
{
public ComparePassword()
: base("Passwords must match.") { }
protected override ValidationResult IsValid (object value, ValidationContext validationContext)
{
if (value == null) return new ValidationResult("A password is required.");
// Make sure you change YourRegistrationModel to whatever the actual name is
if ((validationContext.ObjectType.Name != "YourRegistrationModel")
return new ValidationResult("This attribute is being used incorrectly.");
if (((YourRegistrationModel)validationContext.ObjectInstance).ConfirmPassword != value.ToString())
return new ValidationResult("Passwords must match.");
return ValidationResult.Success;
}
}
Now all you need to do is add [ComparePassword] to your password property, nothing to pass... simple and fairly clean
You can't pass a reference type to an attribute unless you do some rather lame reflection code.
In this situation, I would think creating a custom model binder would be a better idea and then checking the Password and ComparePassword at that point.
you need a STATIC method in your case:
EXAMPLE:
public static ValidationResult ValidateFrequency( double frequency, ValidationContext context )
{
if( context == null )
{
return ( ValidationResult.Success );
}
}
just as an example:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
namespace GDNET.Web.Mvc.Validation
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class ValidatePasswordLengthAttribute : ValidationAttribute, IClientValidatable
{
private const string defaultErrorMessage = "'{0}' must be at least {1} characters long.";
private readonly int minRequiredPasswordLength = Membership.Provider.MinRequiredPasswordLength;
public ValidatePasswordLengthAttribute()
: base(defaultErrorMessage)
{
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, minRequiredPasswordLength);
}
public override bool IsValid(object value)
{
string valueAsString = value as string;
return (valueAsString != null && valueAsString.Length >= minRequiredPasswordLength);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new[]
{
new ModelClientValidationStringLengthRule(FormatErrorMessage(metadata.GetDisplayName()), minRequiredPasswordLength, int.MaxValue)
};
}
}
}
source: https://code.google.com/p/gdnetprojects/source/browse/trunk/Experiments/Common/GDNET.Web.Mvc/Validation/ValidatePasswordLengthAttribute.cs?r=69