New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Html 5 Placeholder and Required attribute support to editor templates #4
Conversation
The duplication of the attribute code definitely violates DRY, but I wanted to make sure the EditorTemplates supported copy/paste deployment if people only wanted to use a couple of them and not the whole project. |
Looks good, but I don't think that the required attribute should have the value "required", I went with true, even though both work. In my patch I added the required and the maxlength attributes, and have the static attribute written a bit more concise. |
From the spec:
http://www.w3.org/TR/html5/common-microsyntaxes.html#boolean-attribute You may want to dig in to the HTML 5 spec a bit more. For example placeholder and required are not actually valid on the date/time/month inputs and stuff like that, you can't just put that on every input. In terms of the EditorFor the goal is that you wouldn't actually pass any HTML attributes to it, its meant as an abstraction to the view template where you don't know how it will be rendered. By allowing the passing of HTML attributes you would be coupling the editor implementation to the calling view implementation which isn't really how its intended to work. |
I like the addition of maxlength, could also apply min/max/pattern attributes where applicable. We should look in to those as well. |
Again you are right. The value of required should be name, and placeholder and required should not be on some templates. The purpose of passing HTML parameters was in the case of the "autofocus" attribute, which people using the templates should be able to specify themselves, how else would "autofocus" be set. For the maxlength property I first tried it with the MaxLength annotation, but because MVC doesn't use it, this was not possible, and as StringLength can only be used for a max length, im not sure how to min length would be done, but min/max/pattern should be looked into. |
Ah yeah in the
|
What I did in my project for autofocus is this: if (ViewData["autofocus"] != null && ViewData["autofocus"].ToString().ToLower().Equals("autofocus")) { values.Add("autofocus", "autofocus"); } But this only helps with the autofocus property, which is enough in my case, but not if other attributes are needed. I was just looking into the DataAnnotationExtentions project, which might help with some of the attributes, and can be made a dependency in the NuGet package. |
We can just do readonly with: if (ViewData.ModelMetadata.IsReadOnly) { values.Add("readonly", "readonly"); } Just like with required. |
I have added min and max. Sorry for putting everything together, min/max should of course not be specified for textbox. @{ Dictionary<string, object> values = new Dictionary<string, object>() { { "class", "text-box single-line" }, { "type", "text" } }; |
Added pattern. @{ Dictionary<string, object> values = new Dictionary<string, object>() { { "class", "text-box single-line" }, { "type", "text" } }; |
Now the inputs will render with these attributes when they are specified in metadata:
Note the additional attributes.
placeholder
pulls from theModelMetadata.Watermark
,required
pulls fromModelMetadata.IsRequired