Validate range of dates with Regula

123 Views Asked by At

Is there any way to use @Range to validate a range of dates in Regula? (ditto @Min and @Max)

Or do I need to use @Custom?

@Range(min=

and

@Range(max=

do not seem to accept anything of the type Date - only numbers or strings.

2

There are 2 best solutions below

4
Vivin Paliath On BEST ANSWER

Unfortunately @Range only accepts numbers. I think you can do something like this though:

<input type="hidden" 
       name="date" 
       id="date" 
       data-constraints="
           @Future(date='2000/1/1', format='YMD') 
           @Past(date='2010/1/1', format='YMD')" 
/>

This ensures that the date is after 2000/1/1 and before 2010/1/1 (i.e., in between). I didn't document the date parameter because I don't think I had implemented it when I wrote the documentation. Sorry; the documentation is a bit behind because I'm working on rolling version 1.3 of Regula out, that will have a lot more goodies. I'll be getting started on updating the documentation soon!

0
sq33G On

I found that the following hack works:

I added a second, hidden input that contains the date entered in the format of Date.getTime():

<input type="hidden" data-bind="value: myDateValue.getTime()" ... />

(I'm using Kendo MVVM, but I'm sure other MVVM libraries can handle the same approach)

Then for the constraint, on server side (ASP.NET MVC in my case) I'm generating the Unix time for the min and max:

@Range(min="<%= (dateTime1 - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds %>",
       max="<%= (dateTime2 - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds %>")

EDIT

Need to use .TotalMilliseconds - and even so there's some unexplained discrepancy.