Zann Anderson
Zann Anderson

Reputation: 4907

Orchard client-side validation - how SHOULD it look/work?

I'm attempting to use client-side validation in an admin page in Orchard. I've been successful at making it work using the techniques discussed in this question, but after doing some digging in the Orchard source and online, it seems to me that commenting out these lines

// Register localized data annotations    
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());

is subverting some built-in Orchard functionality which allows for localized error strings. At this point, either having these lines in our out of OrchardStarter.cs is the only difference between validation working and not working for me.

What I'm hoping for is some guidance on this, maybe from the Orchard team. If these lines have to be out in order for validation to work, why are they there in the first place? If they are there for a good reason, what am I (and others) doing wrong in our attempts to get client-side validation working? I'm happy to post code samples if needs be, although it's a pretty standard ViewModel with data annotations. Thanks.

Upvotes: 2

Views: 804

Answers (1)

JimmiTh
JimmiTh

Reputation: 7449

The lines are there to replace the DataAnnotationsModelValidatorProvider (DAMVP) with Orchard's own implementation, which allows localizing the validation messages the Orchard way. The way it does this is by replacing e.g. [Required] with [LocalizedRequired] before passing control on to the DAMVP. Note that DAMVP does get to do its job - but only after Orchard has "messed" with the attributes.

The problem is that DAMVP uses the type of the Attribute to apply client validation attributes. And now it won't find e.g. RequiredAttribute, because it's been replaced by LocalizedRequiredAttribute. So it won't know what - if any - client validation attributes it should add.

So, out-commenting the lines will make you lose Orchard's localization. Leaving them in will make you lose client validation.

One workaround that might work (haven't looked enough through Orchard's code, and haven't the means to test at the moment) would be to make DAMVP aware of Orchard's Localized attributes and what to do with them.

DAMVP has a static RegisterAdapter() method for the purpose of adding new client rules for attributes. It takes the type of the attribute and the type of the Client-side adapter (the class that takes care of adding client attributes) to use.

So, something like the following might work:

In OrchardStarter.cs:

// Leave the LocalizedModelValidatorProvider lines uncommented/intact

// These are the four attributes Orchard replaces. Register the standard
// client adapters for them:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRegularExpressionAttribute), 
    typeof(RegularExpressionAttributeAdapter)
);

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

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedRangeAttribute), 
    typeof(RangeAttributeAdapter)
);

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(LocalizedStringLengthAttribute), 
    typeof(StringLengthAttributeAdapter)
);

As for the official word, it would seem this hasn't worked since localized validation was introduced in 1.3, and the impact is considered low: http://orchard.codeplex.com/workitem/18269

So, at the moment, it appears the official answer to the question title is, "it shouldn't".

Upvotes: 2

Related Questions