This is a migrated thread and some comments may be shown as answers.

ValidationMessageFor won't display in Template Pop-up Window

1 Answer 83 Views
Window
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 07 Dec 2012, 04:48 PM
I can tell it's returning the right message, but it never displays the message.  What am I missing?

DateValidator:
using System;
using System.ComponentModel.DataAnnotations;
 
namespace PosPayAndBankRec.Models.Validators
{
    public class Date
    {
        public enum ValidationType
        {
            RangeValidation,
            Compare
        }
  
        public class DateValidationAttribute : ValidationAttribute
        {
            private ValidationType _validationType;
            private DateTime? _fromDate;
            private DateTime _toDate;
            private string _defaultErrorMessage;
            private string _propertyNameToCompare;
 
            public DateValidationAttribute(string message, string compareWith = "")
            {
                _propertyNameToCompare = compareWith;
                _defaultErrorMessage = message;
            }
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                var baseProperyInfo = validationContext.ObjectType.GetProperty(_propertyNameToCompare);
                DateTime startDate;
                bool NotValid = false;
 
                if (baseProperyInfo.GetValue(validationContext.ObjectInstance, null) is DateTime)
                {
                    startDate = (DateTime)baseProperyInfo.GetValue(validationContext.ObjectInstance, null);
 
                    if (value != null)
                    {
                        DateTime thisDate = (DateTime)value;
 
                        if (thisDate <= startDate)
                        {
                            NotValid = true;
                        }
                    }
                    else if (value == null)
                    {
                        //From date as a value but not the to date
                        NotValid = false;
                    }
                }
                else
                {
                    //To  date not a date and we have a from date
                    if (value != null)
                    {
                        NotValid = true;
                    }
                }
 
                //return validation result
                if (NotValid == false)
                {
                    return null;
                }
                else if (NotValid == true)
                {
                    string message = string.Format(_defaultErrorMessage, validationContext.DisplayName);//, displayAttr.Name);
                    return new ValidationResult(message);
                }
                return null;
            }
        }
    }
}


TreasuryModel:
[Date.DateValidation("Deactivation Date should be greater than the Activation Date.", compareWith: "brActivationDate")]
public Nullable<System.DateTime> brDeactivationDate { get; set; }


TreasuryPopupTemplate:
                <td>
                    @Html.EditorFor(model => model.brActivationDate)
                    @Html.ValidationMessageFor(model => model.brActivationDate)
            </td>
            <td>
                    @Html.EditorFor(model => model.brDeactivationDate)
                    @Html.ValidationMessageFor(model => model.brDeactivationDate)
            </td>

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 11 Dec 2012, 04:40 PM
Hello Alex,

The information you provided is not enough to tell you what exactly is going wrong.
I would like to ask you few questions.

 What is your exact setup - do you set this fragment you shared as content of the Window - if so, how is it with Ajax?
Does the validation work when you are not using Kendo.Window? Does your validation support client side validation?
Could you send us a runnable project?

Kind Regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Window
Asked by
Alex
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or