lunes, 13 de julio de 2015

Validation Error Message From Data Annotation

CODE EXAMPLE:
[
AttributeUsage(AttributeTargets.Method)] [Match(Regex("dsfasd"))] [StringLength(40, MinimumLength = 1)] [MetadataType(typeof(CustomerMetadata))] [Required()] [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] [CustomAttribute(parm1, ErrorMessage = "{0} field validation failed.")] [PhoneMask("999-999-9999", ErrorMessage = "{0} value does not match the mask {1}.")] [DisplayName(@"Do Not Order")] [DefaultValue(0d)] [Range(0.00, 100000000.00, ErrorMessage = "Price must be greater than 0.00.")] [Required(AllowEmptyStrings = false, ErrorMessage = "The {0} is requiered.")] [Min(1, ErrorMessage = "{0} should be minimun of {1}.")] [Column] [DataAnnotationsExtensions.Integer(ErrorMessage = "Please enter a valid number.")] [Mapping(ColumnName = "InventoryId")]
FULL EXAMPLE:

using System.ComponentModel.DataAnnotations;


public class Movie
{
    public int ID { get; set; }
 
    [Required(ErrorMessage = "Title is required")]
    public string Title { get; set; }
 
    [Required(ErrorMessage = "Date is required")]
    [DisplayFormat(DataFormatString = "{0:d}")]    ---->   alternatively,     [DataType(DataType.Date)] 
    public DateTime ReleaseDate { get; set; }
 
    [Required(ErrorMessage = "Genre must be specified")]
    public string Genre { get; set; }
 
    [Required(ErrorMessage = "Price Required")]
    [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
    [DisplayFormat(DataFormatString = "{0:c}")]    ---->   alternatively,     [DataType(DataType.Currency)]
    public decimal Price { get; set; }
 
    [StringLength(100)]
    [Required(ErrorMessage="Required")]
    [Display(Description = "First Name")]
    [RegularExpression(@"^([a-zA-Z0-9 \.\&\'\-]+)$", ErrorMessage = "Invalid First Name")] 
    public string Rating { get; set; }
}

source code