/// <summary> Checks the input fields for for valid contents. </summary> /// <returns> <see langword="true"/> if all fields are filled out according to the requirements. </returns> protected override bool EvaluateIsValid() { Control control = NamingContainer.FindControl(ControlToValidate); BocDateTimeValue dateTimeValueControl = control as BocDateTimeValue; if (dateTimeValueControl == null) { throw new InvalidOperationException("BocDateTimeValueValidator may only be applied to controls of type BocDateTimeValue"); } var validationErrorForRequired = EvaluateIsRequiredValid(dateTimeValueControl); if (validationErrorForRequired != ValidationError.None) { Error = validationErrorForRequired; return(false); } var validationErrorForComplete = EvaluateIsCompleteValid(dateTimeValueControl); if (validationErrorForComplete != ValidationError.None) { Error = validationErrorForComplete; return(false); } bool isValidDate = EvaluateIsValidDate(dateTimeValueControl); bool isValidTime = EvaluateIsValidTime(dateTimeValueControl); if (!isValidDate && !isValidTime) { Error = ValidationError.InvalidDateAndTime; } else if (!isValidDate) { Error = ValidationError.InvalidDate; } else if (!isValidTime) { Error = ValidationError.InvalidTime; } return(isValidDate && isValidTime); }
private ValidationError EvaluateIsRequiredValid(BocDateTimeValue control) { if (!control.IsRequired) { return(ValidationError.None); } bool isDateOrTimeRequired = control.ActualValueType == BocDateTimeValueType.Undefined; bool isDateRequired = control.ActualValueType == BocDateTimeValueType.DateTime || control.ActualValueType == BocDateTimeValueType.Date; bool isTimeRequired = control.ActualValueType == BocDateTimeValueType.DateTime; bool hasDate = !string.IsNullOrWhiteSpace(control.DateString); bool hasTime = !string.IsNullOrWhiteSpace(control.TimeString); var isDateMissing = isDateRequired && !hasDate; var isTimeMissing = isTimeRequired && !hasTime; if (isDateOrTimeRequired && !(hasDate || hasTime)) { return(ValidationError.MissingDateOrTime); } if (isDateMissing && isTimeMissing) { return(ValidationError.MissingDateAndTime); } if (isDateMissing) { return(ValidationError.MissingDate); } if (isTimeMissing) { return(ValidationError.MissingTime); } return(ValidationError.None); }
private ValidationError EvaluateIsCompleteValid(BocDateTimeValue control) { bool isDateRequired = control.ActualValueType == BocDateTimeValueType.DateTime || control.ActualValueType == BocDateTimeValueType.Date; if (!isDateRequired) { return(ValidationError.None); } bool hasDate = !string.IsNullOrWhiteSpace(control.DateString); bool hasTime = !string.IsNullOrWhiteSpace(control.TimeString); if (hasTime && !hasDate) { return(ValidationError.MissingDate); } return(ValidationError.None); }
/// <summary> Validates time values in the current culture. </summary> /// <remarks> Does not detect an included date of 01.01.0001. </remarks> private bool EvaluateIsValidTime(BocDateTimeValue control) { bool isValidTimeRequired = control.ActualValueType == BocDateTimeValueType.Undefined || control.ActualValueType == BocDateTimeValueType.DateTime; if (!isValidTimeRequired) { return(true); } string timeValue = control.TimeString; if (string.IsNullOrWhiteSpace(timeValue)) { return(true); } try { // Is a valid time value? If not, FormatException will be thrown and caught DateTime dateTime = DateTime.Parse( timeValue, Thread.CurrentThread.CurrentCulture, DateTimeStyles.NoCurrentDateDefault); // If only a time was entered, the date will default to 01.01.0001 if (dateTime.Year != 1 || dateTime.Month != 1 || dateTime.Day != 1) { return(false); } } catch (FormatException) { return(false); } return(true); }
/// <summary> /// Validates date values in the current culture. /// </summary> /// <remarks> /// Cannot detect a 0:0 time component included in the date string. /// Since a time-offset of 0:0 will not falsify the result, this is acceptable. /// Prevented by setting the MaxLength attribute of the input field. /// </remarks> private bool EvaluateIsValidDate(BocDateTimeValue control) { bool isValidDateRequired = control.ActualValueType == BocDateTimeValueType.Undefined || control.ActualValueType == BocDateTimeValueType.DateTime || control.ActualValueType == BocDateTimeValueType.Date; if (!isValidDateRequired) { return(true); } string dateValue = control.DateString; if (string.IsNullOrWhiteSpace(dateValue)) { return(true); } try { // Is a valid date/time value? If not, FormatException will be thrown and caught DateTime dateTime = DateTime.Parse(dateValue); // Has a time component? if (dateTime.TimeOfDay != TimeSpan.Zero) { return(false); } } catch (FormatException) { return(false); } catch (IndexOutOfRangeException) { return(false); } try { // If there is only a time value in the date field, // it will be detected if dateTimeToday and dateTimeFirstDay differ // Empty date defaults to 01.01.0001 DateTime dateTimeFirstDay = DateTime.Parse( dateValue, Thread.CurrentThread.CurrentCulture); // Empty date defaults to today DateTime dateTimeToday = DateTime.Parse( dateValue, Thread.CurrentThread.CurrentCulture, DateTimeStyles.NoCurrentDateDefault); // That's actually a time instead of a date if (dateTimeToday.Date != dateTimeFirstDay.Date) { return(false); } } catch (FormatException) { // This exception will most likely never happen. If it does, the value is still a valid date // or the execution would not have reached this point. } catch (IndexOutOfRangeException) { return(false); } return(true); }