0

I'm working on a bigger project and we have many views and almost all of them have a SelectList or more, whose value is a GUID. The viewmodel works fine and server side validation as well, the problem is that the HTML select element does not get any data-val attributes, we are using Html.DropDownListFor. It works fine when the value is short, string etc but not GUID.

Is there a way to get data-val attributes without adding an ValidationAttribute to all GUID properties in the viewmodels? Because there are a loot of them.


What worked for me in the end:

I got on the right track with Stephen Muecke's answer: we are using our own RequiredAttribute:

public class LocalizedRequiredAttribute : RequiredAttribute
{
    public LocalizedRequiredAttribute()
    {
        ErrorMessageResourceName = "Required";
        ErrorMessageResourceType = typeof (Resources.ErrorMessages);
    }
}

But this does not add any client side validation attributes like the basic [Required] does, but it was easy to fix. Just add this code to your Application_Start():

        DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof (LocalizedRequiredAttribute),
            typeof (RequiredAttributeAdapter));

And now you will get data-val=true data-val-required="message". Solution found here: https://stackoverflow.com/a/12573540/1225758

Community
  • 1
  • 1
SpaceOgre
  • 107
  • 1
  • 12
  • What data-val attributes are you expecting? –  Nov 24 '14 at 12:06
  • Ah good point, should have included that in the question: `data-val="true"` at least but since numbers also get `data-val-number="message here"` I would guess something like `data-val-guid="message here"` as well. – SpaceOgre Nov 24 '14 at 12:53

3 Answers3

0

Firstly, I would want to know what the problem is with having multiple GUID data annotations on your model properties?

Secondly I would say that it is far clearer and readable to other developers working on the project to have explicit validation going on with data annotations on each property than it is to have some "non-standard" validation voodoo going on.

You could probably achieve this with action filters (look for any properties of type Guid) but I think this will make the use/specification of your models less expressive of their intentions and simply confuse.

Jammer
  • 9,969
  • 11
  • 68
  • 115
  • It is not a problem to have multiple annotations really, but there are well over 100 view models in the project who would need to be updated. That is why a "global" solution would be good, since updating that many files manually would more than likely lead to some being missed. – SpaceOgre Nov 25 '14 at 08:31
0

Guids have dashes in content and may occur problems. You may try to use .ToString('N') at the end of the Guids where they are being generated to remove dashes. Or you may write a jquery hack to add escape characters before dashes on client side. Or even more; try to implement your own guid validation approach as here: How to test valid UUID/GUID?

Or even even more, you can try to implement your own guid annotation attribute as here: Validation of Guid

Community
  • 1
  • 1
Görkem Öğüt
  • 1,761
  • 3
  • 18
  • 29
  • Thanks for your comment. I think my question needs to be improved. I don't want the client side to check if the Guid in it self is valid, since it is a dropdown created on the server with only valid Guid's in it. What I need is for the client side to hide the required text when a value is selected. This works out of the box for strings, number etc – SpaceOgre Nov 25 '14 at 08:28
0

No data-val-* attributes are rendered because there are no jquery.validate.unobtrusive adaptors for GUID. The only one you could get out of the box is data-val-required (and the associated data-val) if you were to make the property nullable and add the [Required] attribute.

If you want some client validation you could use a [RegularExpression] attribute (not tested but I think ^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$ should work).

However it seems unnecessary since you are using @Html.DropDownListFor() and (I assume) your building a SelectList on the controller which would contain only GUID's for the SelectListItem.Value property (why would you render an option which is not valid - other than perhaps a "--Please select--" label option for use with a [Required] attribute?).

  • I have tried `Guid?` and `[Required]` but I still don't get any attributes from it. And you are correct, I don't need client side validation for the Guid values. I need the `data-val-*` attributes for the client side to hide the required text when a value is selected. – SpaceOgre Nov 25 '14 at 08:22
  • Works fine for me and renders ` –  Nov 25 '14 at 08:30
  • What do you mean _need the data-val-* attributes for the client side to hide the required text_? What 'text`? and whats it got to do with data attributes? –  Nov 25 '14 at 08:42
  • I forgot we created our own RequiredAttribute, and that is not giving any html attributes. If I change back to the basic `[Required]` then it works. So I need to find a way to do it with our own Required. Will mark this answer as the correct one. – SpaceOgre Nov 25 '14 at 08:58