5 Replies - 237 Views - Last Post: 08 October 2019 - 07:44 AM Rate Topic: -----

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,277
  • Joined: 12-December 12

data annotation to compare dates

Posted 07 October 2019 - 03:56 AM

ASP.NET MVC

I have two date inputs using TextBoxFor, StartDate and ExpiryDate.

                    <td>@Html.TextBoxFor(m => m.LeasesList[i].StartDate, new { @class = "editDetail form-control input-sm", @type = "date", 
                       data_associated_date_before = "LeasesList_" + i + "__ExpiryDate", 
                       @Value = Model.LeasesList[i].StartDate != null ? Convert.ToDateTime(Model.LeasesList[i].StartDate).ToString("yyyy-MM-dd") : "" })
                    <td>@Html.TextBoxFor(m => m.LeasesList[i].ExpiryDate, new { @class = "editDetail form-control input-sm", @type = "date",
                       data_associated_date_after = "LeasesList_" + i + "__StartDate", 
                       @Value = Model.LeasesList[i].ExpiryDate != null ? Convert.ToDateTime(Model.LeasesList[i].ExpiryDate).ToString("yyyy-MM-dd") : "" })


I have implemented some JS validation, based on their data attributes, to validate that the start is less than the expiry date, and vice versa. Sample code below.

Is there an existing feature, or perhaps extension, for data annotation(s) that I am missing that would do this without my JS? Could you offer some initial guidance as to how I might implement this (custom) validation via data annotations? Particularly given that it is between two fields.

Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: data annotation to compare dates

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,277
  • Joined: 12-December 12

Re: data annotation to compare dates

Posted 07 October 2019 - 04:03 AM

There is a CompareAttribute apparently that I'll have a look into. Edit: Looks like this is just to compare to see if two values are equal.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15363
  • View blog
  • Posts: 61,617
  • Joined: 12-June 08

Re: data annotation to compare dates

Posted 07 October 2019 - 07:24 AM

You would need to build a custom attribute.

A little weird but work well once done.

https://docs.microso...=aspnetcore-3.0
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,277
  • Joined: 12-December 12

Re: data annotation to compare dates

Posted 07 October 2019 - 11:51 PM

Thank you.

I believe that sets up server-side validation; I think it then requires quite a bit more work to tie it up with client-side/jQuery validation.

I'll probably need to discuss this with the team about how we wish to approach it. Ta.
Was This Post Helpful? 1
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15363
  • View blog
  • Posts: 61,617
  • Joined: 12-June 08

Re: data annotation to compare dates

Posted 08 October 2019 - 07:07 AM

I would also scroll down to the 'jQuery Unobtrusive Validation' info. Works pretty well.. I think I implemented it on a page or something.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,277
  • Joined: 12-December 12

Re: data annotation to compare dates

Posted 08 October 2019 - 07:44 AM

Yes, as I recall the unobtrusive validation is how server-side is carried across to client-side validation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1