Discussion: MVC - breaking changes to IClientModelValidator
This is the issue for discussion and questions related to the announcement here: aspnet/Announcements#179
Summary
In RC2 we made a breaking change to the definition of IClientModelValidator to simplify the number of concepts involved and improve performance.
IClientModelValidator instances get to customize the attributes written to HTML input fields by both IHtmlHelper and the <input ... > TagHelper. These attributes are then used by a client-side script to enforce client-side validation.
If you're using the built-in validation (including System.ComponentModel.DataAnnotations attributes) there should be no impact on your application. If you are authoring custom IClientModelValidator support, read on for details.
Changes
The updated interface definition is:
public interface IClientModelValidator
{
void AddValidation(ClientModelValidationContext context);
}
IClientModelValidator instances are cached indefinitely. When AddValidation is called, the validator should add any HTML attributes to context.Attributes that will provide information to client validation. The other properties on context.Attributes provide access to the ActionContext (information about the request/action) as well as ModelMetadata (information about the model being rendered).
The previous code used to return a set of ClientValidationRule instances which were translated by previous versions of MVC into either HTML attributes or a blob of JSON that was embedded into the page. We now only provide direct support for setting HTML attributes.
Example
The [Remote(...)] attribute is a built in IClientModelValidator which passes data used by unobtrusive validation to validate a field using AJAX.
Here's a simplified version of the AddValidation code:
public virtual void AddValidation(ClientModelValidationContext context)
{
MergeAttribute(context.Attributes, "data-val", "true");
var errorMessage = GetErrorMessage(...);
MergeAttribute(context.Attributes, "data-val-remote", errorMessage);
var url = GetUrl(...);
MergeAttribute(context.Attributes, "data-val-remote-url", url);
....
}
private bool MergeAttribute(IDictionary<string, string> attributes, string key, string value)
{
if (attributes.ContainsKey(key))
{
return false;
}
attributes.Add(key, value);
return true;
}
and the
<input ... >TagHelper
Also used in the <select ...> and <textarea ...> tag helpers.
By some reason added attribute values don't like non-latin letters. This implementation:
public void AddValidation(ClientModelValidationContext context)
{
string message = "Привет!"; // message in Russian throws on render
// string messge = "Test"; is rendered fine
context.Attributes.Add("data-val-myval", message);
}
throws on trying to render:
An unhandled exception occurred while processing the request.
NotImplementedException: The method or operation is not implemented. Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent.EmptyOrWhiteSpaceWriter.Write(Char value) System.Text.Encodings.Web.TextEncoder.Write(TextWriter output, Char* input, Int32 inputLength) System.Text.Encodings.Web.TextEncoder.EncodeCore(TextWriter output, Char* value, Int32 valueLength) System.Text.Encodings.Web.TextEncoder.Encode(TextWriter output, String value, Int32 startIndex, Int32 characterCount) Microsoft.AspNetCore.Mvc.Rendering.TagBuilder.AppendAttributes(TextWriter writer, HtmlEncoder encoder) Microsoft.AspNetCore.Mvc.Rendering.TagBuilder.WriteTo(TextWriter writer, HtmlEncoder encoder) Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewBuffer.WriteTo(TextWriter writer, HtmlEncoder encoder) Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent.IsEmptyOrWhiteSpaceCore(Object entry, EmptyOrWhiteSpaceWriter writer) Microsoft.AspNetCore.Razor.TagHelpers.DefaultTagHelperContent.get_IsEmptyOrWhiteSpace() Microsoft.AspNetCore.Mvc.TagHelpers.LabelTagHelper.
d__15.MoveNext()
@dubtar could you please open an issue in the Razor repo to track that problem separately?
@dougbu Sure thing! aspnet/razor#760
We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have.