/// <summary> /// Determines whether the specified value of the object is valid. /// </summary> /// <param name="value">The value of the object to validate.</param> /// <param name="validationContext">The validation context.</param> /// <returns>true if the specified value is valid; otherwise, false.</returns> protected override ValidationResult IsValid(object value, ValidationContext validationContext) { string nines = new string('9', Precision); string decimalNines = nines.Insert(Precision - scale, "."); double minMaxValue = double.Parse(decimalNines); RangeAttribute range = new RangeAttribute(-minMaxValue, minMaxValue); if (range.IsValid(value)) { return ValidationResult.Success; } else { return new ValidationResult(range.ErrorMessage); } }
public void testAddBookingWrongDistance() { var rangeCharge = new System.ComponentModel.DataAnnotations.RangeAttribute(0, 100); var rangeDistance = new System.ComponentModel.DataAnnotations.RangeAttribute(0, 1000); Booking booking = new Booking { currentCharge = 10, requiredDistance = 1500, plugType = Plug.CCSCombo2Plug, start = new DateTime(2020, 9, 7, 13, 0, 0), end = new DateTime(2020, 9, 7, 20, 0, 0) }; Assert.IsTrue(rangeCharge.IsValid(booking.currentCharge)); Assert.IsFalse(rangeDistance.IsValid(booking.requiredDistance)); }
public DateTime? getJoinedDate() { var yearField = Request["JoinedDateYear"]; var monthField = Request["JoinedDateMonth"]; var required = new RequiredAttribute(); if (string.IsNullOrEmpty(yearField)) { ModelState.AddModelError("JoinedDate", required.FormatErrorMessage(WebResources.JoiningDateYear)); } else if (string.IsNullOrEmpty(monthField)) { ModelState.AddModelError("JoinedDate", required.FormatErrorMessage(WebResources.JoiningDateMonth)); } else { var dateRange = new RangeAttribute( typeof(DateTime), MinDate.ToShortDateString(), DateTime.Now.ToShortDateString() ); DateTime? joinedDate = null; try { joinedDate = new DateTime(Convert.ToInt32(yearField), Convert.ToInt32(monthField), 1); } catch (FormatException ex) { ModelState.AddModelError("JoinedDate", ex); } catch (ArgumentOutOfRangeException) { ModelState.AddModelError("JoinedDate", dateRange.FormatErrorMessage(WebResources.JoiningDate)); } catch (ArgumentException ex) { ModelState.AddModelError("JoinedDate", ex); } if (joinedDate != null) { var now = DateTime.Now; if (!dateRange.IsValid(joinedDate.Value)) { ModelState.AddModelError("JoinedDate", dateRange.FormatErrorMessage(WebResources.JoiningDate)); } else { return joinedDate; } } } this.ViewData["JoinedDateYear"] = yearField; this.ViewData["JoinedDateYear"] = monthField; return null; }
public void testAddBooking() { var range = new System.ComponentModel.DataAnnotations.RangeAttribute(0, 100); var dateValidation = new DateAttribute(); // Alle Attribute richtig Booking b1 = new Booking { currentCharge = 30, requiredDistance = 150, connectorType = ConnectorType.CCSCombo2Plug, start = new DateTime(2020, 7, 8, 8, 0, 0), end = new DateTime(2020, 7, 8, 10, 0, 0) }; Assert.IsTrue(range.IsValid(b1.currentCharge)); Assert.IsTrue(b1.requiredDistance > 0 && b1.requiredDistance <= 1000); Assert.IsTrue(dateValidation.IsValid(b1.start)); Assert.IsTrue(dateValidation.IsValid(b1.end)); Assert.IsTrue(b1.end > b1.start); // currentCharge ungültig Booking b2 = new Booking { currentCharge = -30, requiredDistance = 150, connectorType = ConnectorType.CCSCombo2Plug, start = new DateTime(2020, 7, 8, 8, 0, 0), end = new DateTime(2020, 7, 8, 10, 0, 0) }; Assert.IsFalse(range.IsValid(b2.currentCharge)); Assert.IsTrue(b2.requiredDistance > 0 && b2.requiredDistance <= 1000); Assert.IsTrue(dateValidation.IsValid(b2.start)); Assert.IsTrue(dateValidation.IsValid(b2.end)); Assert.IsTrue(b2.end > b2.start); // requiredDistance ungültig Booking b3 = new Booking { currentCharge = 30, requiredDistance = -150, connectorType = ConnectorType.CCSCombo2Plug, start = new DateTime(2020, 7, 8, 8, 0, 0), end = new DateTime(2020, 7, 8, 10, 0, 0) }; Assert.IsTrue(range.IsValid(b3.currentCharge)); Assert.IsFalse(b3.requiredDistance > 0 && b3.requiredDistance <= 1000); Assert.IsTrue(dateValidation.IsValid(b3.start)); Assert.IsTrue(dateValidation.IsValid(b3.end)); Assert.IsTrue(b3.end > b3.start); // start ungültig Booking b4 = new Booking { currentCharge = 30, requiredDistance = 150, connectorType = ConnectorType.CCSCombo2Plug, start = new DateTime(2020, 5, 5, 8, 0, 0), end = new DateTime(2020, 7, 8, 10, 0, 0) }; Assert.IsTrue(range.IsValid(b4.currentCharge)); Assert.IsTrue(b4.requiredDistance > 0 && b4.requiredDistance <= 1000); Assert.IsFalse(dateValidation.IsValid(b4.start)); Assert.IsTrue(dateValidation.IsValid(b4.end)); Assert.IsTrue(b4.end > b4.start); // end ungültig Booking b5 = new Booking { currentCharge = 30, requiredDistance = 150, connectorType = ConnectorType.CCSCombo2Plug, start = new DateTime(2020, 7, 8, 8, 0, 0), end = new DateTime(2020, 7, 7, 10, 0, 0) }; Assert.IsTrue(range.IsValid(b5.currentCharge)); Assert.IsTrue(b5.requiredDistance > 0 && b3.requiredDistance <= 1000); Assert.IsTrue(dateValidation.IsValid(b5.start)); Assert.IsTrue(dateValidation.IsValid(b5.end)); Assert.IsFalse(b5.end > b5.start); }