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

[Solved] MVC3 Client Validation

5 Answers 73 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Adam
Top achievements
Rank 1
Adam asked on 07 Jul 2011, 02:30 PM
I have seen this question asked several times on the forms but none of them seem to have answers.  Do the Telerik controls support the rendering of the data-val* attributes for validations or do they have to be manually added? 

From my own experience it appears that the controls don't but I am unsure if I am missing a setting or if this is a "work in progress" for a future release.  Specifically I am looking at the NumericTextBox controls.  I noticed that they don't output any of the data-val* attributes and as such don't honor the default Required validations or any custom validations.  We are currently using the 2011.1.414 build.

5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 07 Jul 2011, 02:37 PM
Hi Adam,

 The build which you are using fully supports unobtrusive validation and renders those attributes. Make sure UnobtrusiveValidation is enabled in your web.config though.

Greetings,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Adam
Top achievements
Rank 1
answered on 07 Jul 2011, 06:48 PM
I do have UnobtrusiveJavaScriptEnabled turned on in my web config but it is not outputting the validation attributes.  After building a debug version of the Telerik.Web.Mvc assembly I found that when it was getting into the ViewComponentExtensions.GetUnobtrusiveValidationAttributes() method that the final call to "htmlHelper.GetUnobtrusiveValidationAttributes(instance.Name, metadata)" is returning a 0 element dictionary.  I even tried creating a clean MVC3 application and added a numeric field to the RegisterModel and used a NumericTextBox and got the same results.  In my application I am using the model in an EditorTemplate and in the clean application it was just on the straight view.

I did find that if I changed my implementation from:
@(Html.Telerik().CurrencyTextBoxFor(model => model.LoanAmount).Spinners(false).DecimalDigits(0).EmptyMessage("$"))
To:
@(Html.Telerik().CurrencyTextBoxFor(model => model.LoanAmount).Spinners(false).DecimalDigits(0).EmptyMessage("$").InputHtmlAttributes(Html.GetUnobtrusiveValidationAttributes("LoanAmount")))

Then the attributes appeared just fine.  Even when I was debugging in ViewComponentExtensions.GetUnobtrusiveValidationAttributes() I tried variations on the name that was passed into the method and wasn't able to get any of them to come back with data.

EDIT: I attached my sample project.  If you run it and go to /Account/Register and view source or press the register button you can see that the first Age field doesn't get marked with the Required message but the second one does.
0
Georgi Krustev
Telerik team
answered on 08 Jul 2011, 09:09 AM
Hello Adam,

 
Thank you for the test project.

This actually is a known issue, which happens when the Model is null.

This code:

return View();
returns ViewResult where RegisterModel is null.
HenceViewComponentExtensions.GetUnobtrusiveValidationAttributes() method is not able to get correct data validation attributes.

In order to overcome this issue I will suggest you return empty RegisterModel:
return View(new RegisterModel());

Kind regards,
Georgi Krustev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Adam
Top achievements
Rank 1
answered on 08 Jul 2011, 12:57 PM
I can understand how that could be an issue.  I have updated the sample to better match what we are doing in our application.  We are setting the model to an actual value and not returning null.  However, as we have the model that is being used wrapped inside another one that is where the problem is occurring.
0
Accepted
Georgi Krustev
Telerik team
answered on 11 Jul 2011, 10:16 AM
Hello Adam,

 
Thank you for the updated test project.

The NumericTextBox, as other UI components which support unobtrusive validation, uses Html.ViewContext.ViewData rather then Html.ViewData, which causes the aforementioned issue.

We will try to address this issue for the SP1  Q2 2011 release of Telerik Extensions for ASP.NET MVC.
For now, you can continue to use the InputHtmlAttributes method to apply unobtrusive validation attributes.

As a token of gratitude for your involvement I have updated your Telerik points.

Regards,
Georgi Krustev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
General Discussions
Asked by
Adam
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Adam
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or