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

View model reset/broken with IValidatableObject

4 Answers 676 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Bil
Top achievements
Rank 1
Bil asked on 26 Feb 2019, 02:46 PM

Hi guys,

I've got a view model that implements IValidatableObject for some DateTime fields that are used with the DateTimePicker. If the validation fails in the Validate method for the DateTime fields they return the error but after fixing the issue (for example DateTime cannot be in the past) the view model and control continues to use a empty/default DateTime value (1-1-1901 12:00 AM) and there's no way to get the model back to a valid state. 

The attached image is the form at start. The two DateTime fields are set to the current time. One you submit the form the Validate method kicks in and recognizes the date in the past and throws back the error message. The second image shows the error message in the form. The third image shows the corrected value to be a time in the future (using the time picker). The fourth image shows the form after the POST where the time gets reset back to 1/1/1901 12:00 AM and throws the error it can't be in the past. No matter how many times you change this field it keeps throwing this error.

Here's the Validate method:

1.public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
2.{
3.        if (OutageStart < DateTime.Now)
4.        {
5.            yield return new ValidationResult("Outage Date / Time cannot appear in the past", new[] { "OutageStart" });
6.        }
7.}

4 Answers, 1 is accepted

Sort by
0
Bil
Top achievements
Rank 1
answered on 26 Feb 2019, 02:53 PM

Just some more code. The form is also using jquery validate.

Here's the form with the controls:

01.@using (Html.BeginForm("Add", "Outage"))
02.{
03.        @Html.HiddenFor(x => x.Id)
04.        @Html.AntiForgeryToken()
05. 
06.        <div class="form-group  row">
07.            @Html.LabelFor(x => x.OutagePoint, new { @class = "col-sm-4 col-form-label text-right col-form-label-background" })
08.            <div class="col-sm-4">
09.                @Html.TextBoxFor(x => x.OutagePoint, new { @class = "form-control" })
10.                <div class="help-block text-danger">@Html.ValidationMessageFor(x => x.OutagePoint)</div>
11.            </div>
12.        </div>
13. 
14.        <div class="form-group  row">
15.            @Html.LabelFor(x => x.OutageStart, new { @class = "col-sm-4 col-form-label text-right col-form-label-background" })
16.            <div class="col-sm-4">
17.                @(Html.Kendo().DateTimePickerFor(m => m.OutageStart))
18.                <div class="help-block text-danger">@Html.ValidationMessageFor(x => x.OutageStart)</div>
19.            </div>
20.        </div>
21.}

 

The controller POST method

1.[HttpPost]
2.[ValidateAntiForgeryToken]
3.public async Task<IActionResult> Add(OutageViewModel model)
4.{
5.    if (!ModelState.IsValid) return View(model);
6.    return RedirectToAction("Index");
7.}

 

And the view model

01.public class OutageViewModel : IValidatableObject
02.    {
03.        public int Id { get; set; }
04. 
05.        [Display(Name = "Outage Point")]
06.    [Required]
07.        public string OutagePoint { get; set; }
08. 
09.        [Display(Name = "Outage Date / Time")]
10.        [Required]
11.        public DateTime OutageStart { get; set; }
12.}

Thanks!

0
Angel Petrov
Telerik team
answered on 01 Mar 2019, 12:18 PM
Hello,

I am not sure what exactly is causing this behavior. Is the form being reset? Can you please send us a small sample in which this problem can be observed?

Regards,
Angel Petrov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Rob
Top achievements
Rank 1
answered on 25 Mar 2019, 11:01 PM

I think I've en encountered exactly the same problem.  I have a model with two dates on.  The model implements IValidatable object.  The validation prevents the first date being in the past and the second date being before the first date.  

public class SalesOrder : IValidatableObject
{
    [Required]
    [Display(Name = "Earliest delivery date")]
    [UIHint("DateEditor")]
    public DateTime EarliestDeliveryDate { get; set; }
    [Required]
    [Display(Name = "Latest delivery date")]
    [UIHint("DateEditor")]
    public DateTime LatestDeliveryDate { get; set; }
 
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (EarliestDeliveryDate.Date <= DateTime.Today)
        {
            yield return new ValidationResult("Earliest delivery date must be in the future", new[] { nameof(EarliestDeliveryDate) });
        }
        if (LatestDeliveryDate.Date < EarliestDeliveryDate.Date)
        {
            yield return new ValidationResult("Latest delivery date must not be before earliest delivery date", new[] { nameof(LatestDeliveryDate) });
        }
    }
}

The editor looks like:

@model DateTime
 
@Html.Kendo().DatePickerFor(m => m)

 

When the form submits and there is a validation error, one of the dates seems to get reset to DateTime.MinValue.  It's not always the date that failed validation either.

 

 

 

0
Angel Petrov
Telerik team
answered on 28 Mar 2019, 07:13 PM
Hi,

Can you please open a formal support ticket and attach a project in which this problem can be observed? Once we can replicate it locally we should be able to provide a more precise answer.

Regards,
Angel Petrov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Date/Time Pickers
Asked by
Bil
Top achievements
Rank 1
Answers by
Bil
Top achievements
Rank 1
Angel Petrov
Telerik team
Rob
Top achievements
Rank 1
Share this question
or