1

I have a inputbox (text) that contains a datetime value, for some reason this input box has validation when I don't even require it.

My Model and ViewModel has no validation on the properites at all.

In my Razor view page I do have some input fields that have:

  @Html.ValidationMessageFor(vm => vm.User.Age)

On this input I am trying to enter some string and it gives me a client-side validation warning:

 @Html.TextBoxFor(vm => vm.User.BannedDate)

Below is the HTML that is rendered below the input box:

<input data-val="true" data-val-date="The field BannedDate must be a date." id="User_BannedDate" name="User.BannedDate" type="text" value="27/02/2014 12:00:00 AM" class="input-validation-error">

I am using Bootstrap, I have jquery also. Could it be detecting things automatically?

Update

Don't have a globalization tag in web.config

In my Controller's OnActionExecuting I am setting:

System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-CA");
loyalflow
  • 14,275
  • 27
  • 107
  • 168
  • It's normal that non nullable DateTime properties on your view model will enforce validation. A DateTime property on your view model will require that the user enters a value respecting the client browser CultureInfo format. What exactly did you expect? – Darin Dimitrov Feb 18 '14 at 21:23
  • @DarinDimitrov The field is nullable in my Model. – loyalflow Feb 18 '14 at 21:27
  • Is the user entering some value in the corresponding input field? If he does, what format does he use? Does this format respect the culture format of dates he is using? For example if the client browser is configured to use `bg-BG` culture info (Bulgarian) then it is expected that he enters the DateTime as `dd.MM.yyyy`. – Darin Dimitrov Feb 18 '14 at 21:28
  • My culture is set to dd/MM/yyyy and it breaks if you put 29/02/2014. – loyalflow Feb 18 '14 at 21:29
  • What do you mean by your culture is set to `dd/MM/yyyy`? How is your culture set? What's the value of the `globalization` tag in your web.config? How is the client browser preferred language configured? For example if the client browser is using `fr-FR` culture then `29/02/2014` is a perfectly valid date but if your server is using `en-US` then this is a completely invalid format simply because there are only 12 months in a year. See the difference between `dd/MM/yyyy` and `MM/dd/yyyy` format? – Darin Dimitrov Feb 18 '14 at 21:30
  • @DarinDimitrov see my updated answer, appreciate your help! – loyalflow Feb 18 '14 at 21:34
  • You are setting the server side culture to `en-CA` but you cannot control the culture of the client browser which is used by jQuery client side validation. For example if I set my browser to a different culture then client side validation might pass but hardcoding your server side culture might be incompatible. – Darin Dimitrov Feb 18 '14 at 21:35
  • So I have to set the validation's date format somehow at a global level then? – loyalflow Feb 18 '14 at 21:36
  • There's no such notion as global level. – Darin Dimitrov Feb 18 '14 at 21:37
  • Ideally you should be using `auto` culture in order to allow the client to define the desired culture or enforce the culture format and write a custom model binder as I illustrated in this answer http://stackoverflow.com/a/7836093/29407. – Darin Dimitrov Feb 18 '14 at 21:38
  • By global level I mean if I can do this one time in my master.js file or something and not on a per page basis. – loyalflow Feb 18 '14 at 21:38
  • I don't understand what you mean by per page basis. There's the culture of the client browser and the culture of the server. Using `auto` in your globalization element means that the server will automatically use the same culture as the client. Unfortunately you seem to have hardcoded the culture on the server to `en-CA` which obviously is causing troubles if the client is using a different culture. – Darin Dimitrov Feb 18 '14 at 21:39
  • @DarinDimitrov - I recommend an edit of this question, the issue is one of culture and "why can't I force users to enter a data a certain way" and less of "why is this validation being output", which I interpreted as the original question. – Tommy Feb 18 '14 at 21:41
  • @DarinDimitrov What is best practise? Not to force the culture on the server-side? We are currently forcing it to 2 languages based on the user's settings. – loyalflow Feb 18 '14 at 21:58

1 Answers1

2

The framework is automatically generating these because it will try and bind anything you put in that text field to the backing property. Since BannedDate is a date and cannot accept values like Tomorrow, the framework is ensuring a valid date is submitted from the client. Looking at your output, it is not requiring those fields, only if something is entered into that field, it shall be a date.

As for the Age property, if you are not using a nullable int, you are seeing one of the following:

1) Automatically populates as a 0

2) If you are using a nullable int, it may be the same as BannedDate, in that you have to enter an integer and not 32 and a half! as an acceptable value.

If these automatic validations were not in place or if javascript was turned off on the client, you would receive a ModelState error during binding letting you know the value was not an acceptable value for those particular fields.

Tommy
  • 39,592
  • 10
  • 90
  • 121