November Happy Hour will be moved to Thursday December 5th.

Compare Datetime properties

Vote:
 

Hi

Is this possible to achive?  In Episerver CMS 6R2 in my Pagetype I got two properties of type DateTime (Start ad Stop).

To prevent the users to enter a later date as Start compared to Stop.  

Would it be possible to do a compare of those when the user enters the vaules and display this error to the user (like in the manner when a property is required). My intention was to hook something in the pagesave event, but is it possible to send back some nice response to user after the datecheck from that event or can this be done in some other way?

#123295
Jul 01, 2015 9:33
Vote:
 

Hi,

In EPiServer 6 you could  hook to a DataFactory events or prepare a custom property. In EPiServer 7/8 you there is EPiServer.Validation.IValidate<T> interface.

#123312
Jul 01, 2015 13:08
Vote:
 

Hi Grzegorz yes this I know, but how do after you done your comparison in Datafactory events send a response back to the user interface displaying for the user that the userinput is wrong.

"Your stopdate should be later than startdate".

/C

#123313
Jul 01, 2015 13:13
Vote:
 

Did you check the CancelReason property? Maybe it could be used.

private void Instance_SavingContent(object sender, ContentEventArgs e)
{
e.CancelAction = true;
e.CancelReason = "Your stopdate should be later than startdate";
}

#123314
Jul 01, 2015 13:18
Vote:
 

Nice I will look into this, I'll get back.

#123315
Jul 01, 2015 13:21
Vote:
 

I would recommend to create a custom validator instead. It's easier and more convenient.

Here's an example for validating start and stop dates for a calendar event (CalendarEventPage), given that there is two properties called EventStartDate and EventStopDate in the content type.

public class CalendarEventPageValidator : IValidate<CalendarEventPage>
{
    public IEnumerable<ValidationError> Validate(CalendarEventPage instance)
    {
        if (instance.EventStartDate == null || instance.EventStopDate == null)
        {
            yield return new ValidationError
            {
                ErrorMessage = "An event start and stop date must be specified",
                PropertyName = "EventStartDate",
                RelatedProperties = new string[] { "EventStartDate", "EventStopDate" },
                Severity = ValidationErrorSeverity.Error,
                ValidationType = ValidationErrorType.StorageValidation
            };
        }
        else if (instance.EventStartDate > instance.EventStopDate)
        {
            yield return new ValidationError
            {
                ErrorMessage = "The event start date must occur before the stop date",
                PropertyName = "EventStartDate",
                RelatedProperties = new string[] { "EventStartDate", "EventStopDate" },
                Severity = ValidationErrorSeverity.Error,
                ValidationType = ValidationErrorType.StorageValidation
            };
        }

        yield break;
    }
}

Just add this class and compile. Done.

#123332
Jul 01, 2015 16:46
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.