开发者

How to add Custom logic for validation using asp.net mvc data annotations?

I have read this before about asp.net mvc validations but does not mention what i want to do,So have this view model -

public class MyViewModel
{
        [StringLength(200, MinimumLength = 2, ErrorMessage = "Invalid Name")]
        public string Name { ge开发者_JS百科t; set; }
        [Required(ErrorMessage = "*")]
        public DateTime StartDate { get; set; }
        [Required(ErrorMessage = "*")]
        public DateTime EndDate { get; set; }

}

I have setup the validation and it works..but now i want to add a condition like StartDate should always be greater than the End Date..how can i add such a custom logic/validation? Instead of checking it on controller explicitly and redirecting...can asp.net mvc validation accommodate something like this?


This is what I ended up doing i am also checking if date selected is not today..in case someone wants to do something similiar -

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class EndDateValidationAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "End date cannot be prior to start date";

        public EndDateValidationAttribute(string startDate, string endDate)
            : base(_defaultErrorMessage)
        {
            StartDateStr = startDate;
            EndDateStr = endDate;
            ErrorMessage = _defaultErrorMessage;
        }

        public string StartDateStr { get; private set; }
        public string EndDateStr { get; private set; }

        public DateTime StartDate { get; private set; }
        public DateTime EndDate { get; private set; }

        public override bool IsValid(object value)
        {
            // This is not a required field validator, so if the value equals null return true.  
            if (value == null) return true;

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object startDate = properties.Find(StartDateStr, true /* ignoreCase */).GetValue(value);
            object endDate = properties.Find(EndDateStr, true /* ignoreCase */).GetValue(value);

            StartDate = (DateTime)startDate;
            EndDate = (DateTime)endDate;

            if (StartDate > EndDate) return false;
            else if (Convert.ToDateTime(startDate) == DateTime.Today.Date)
            {
                return false;

            }
            return true;
        }
    } 

Here is how you can use it -

[EndDateValidationAttribute("StartDate", "EndDate", ErrorMessage = "Start date should be after today's date and before end date!")]
    public class CustomeDate
    {
        [DisplayName("StartDate")]
        [Required(ErrorMessage = "*")]
        public DateTime StartDate { get; set; }
        [DisplayName("EndDate")]
        [Required(ErrorMessage = "*")]
        public DateTime EndDate { get; set; }
    }


I'm not sure about this but it may be worth looking at the default asp.net MVC account controller and the PropertiesMustMatchAttribute for an example of custom validation.

Having looked at the code briefly, it should be doable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜