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

DatePicker validates format, but not invalid date

6 Answers 1418 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Robert
Top achievements
Rank 1
Robert asked on 14 Mar 2012, 06:16 PM

Using the DatePicker in MVC 3, I have implemented the necessary data model validation provider to have MVC 3 render the data-val-date attribute so I can use unobtrusive validation.

I know this is working a) because I can see the attribute in the page source and b) if I enter text in the datepicker text box and tab, I get the expected "*Invalid date/format." error.

However, if I enter a completely bogus date, say "32/11/2012" and tab, I get no validation error at all. When the form posts to the server, I get server-side validation that the date is invalid. This isn't a huge deal for a straight-up form, at least the bad date is flagged.

The bigger problem is on a grid-popup form that also has a datepicker - the popup exhibits the same behavior, however the bad date is not flagged!!! It is just set to NULL and posted that way to the server. Model object properties in question are below. Screenshot example of missing validation included.

Screenshot 1: Straight-up form with client side validation working when entering text, not working when entering invalid date.
Screenshot 2: Pop-up with client side validation working when entering text
Screenshot 3: Pop-up with client side validation not working completely when entering an invalid date - it flags it as invalid by changing the TB border color to red, but doesn't show error message and doesn't prevent user form Insert!

What am I missing about this?

[Display(Name = "EOL Date")]
[UIHint("Date")]
[DataType(DataType.Date, ErrorMessage = "*Invalid date/format.")]
public DateTime? EolDate { get; set; }
 
[Display(Name = "Last Order Date")]
[UIHint("Date")]
[DataType(DataType.Date, ErrorMessage = "*Invalid date/format.")]
public DateTime? LastOrderDate { get; set; }

6 Answers, 1 is accepted

Sort by
0
Luciano Kaesemodel
Top achievements
Rank 1
answered on 22 May 2012, 06:49 PM
I have the same problem, have you found any solution to this problem?
0
Robert
Top achievements
Rank 1
answered on 22 May 2012, 09:56 PM
Not as of yet, an no one from Telerik has replied.
0
Atanas Korchev
Telerik team
answered on 23 May 2012, 08:54 AM
Hi,

 The DatePicker visually indicates that the date is invalid. It is the ASP.NET MVC validator which does not detect that the date is invalid. As far as I know the validator is a simple regular expression which clearly would not detect invalid dates. You can perhaps create a custom validator which will validate the date as well. Here is a stackoverflow question which seems related: http://stackoverflow.com/questions/6250821/date-validation-using-asp-net-mvc-3-0 

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Luciano Kaesemodel
Top achievements
Rank 1
answered on 12 Jun 2012, 11:13 PM
Atanas,

Yes, the Datepicker visually indicates that the date is invalid, but the problem is that when the submit button of the form is clicked.
Instead of displaying the default message ("The value XXX is not valid for XXX"), the form is submitted with the value of the DateTime being null.

I've attached a sample project to show that. Try put 32/32/2012 in the regular form and also in the grid popup form. It shows clearly the difference.

The problem could be solved if the grid popup form could display the default error message instead of sending null value.
0
Daniel
Telerik team
answered on 15 Jun 2012, 03:40 PM
Hello Luciano,

The message is not displayed in the project you provided because the attribute isn't added to the input element and so the jQuery validation isn't triggered when submitting an invalid date. The Grid uses the built-in validation to check if the values are valid and since the specified date cannot be converted, the invalid date won't be send to the server.
In order to unable the jQuery validation for dates you could:
  • Specify the DataType of the property.
  • Register a DataTypeAttributeAdapter.
  • Use the InputHtmlAttributes method in the editor template to set the validation attributes to the DatePicker with the GetUnobtrusiveValidationAttributes HTML helper. This step is required because the DatePicker is rendered from the EditorFor helper and the ModelMetadata is different in that context.
I attached the project modified to use the client-side date validation. I hope it helps.

Regards,
Daniel
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
Vijay
Top achievements
Rank 1
answered on 13 Dec 2012, 10:52 AM
Hi,
I am trying DateValidation.zip exaple provided by you...
How can I pass HtmlAttributes to
@Html.EditorFor(m => m.Date) (in Index.cshtml)?

Somthing like
@Html.EditorFor(m => m.Date, new { data-val2="false" })

so, i would get
<input class="t-input" data-val2="false" .......id="Date" name="Date" type="text />

Please some one help me here.
Thanks
Vijay
Tags
Date/Time Pickers
Asked by
Robert
Top achievements
Rank 1
Answers by
Luciano Kaesemodel
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Atanas Korchev
Telerik team
Daniel
Telerik team
Vijay
Top achievements
Rank 1
Share this question
or