dotnet / dotnet/aspnetcore

DefaultHtmlGenerator could add min and max HTML attributes based on RangeAttribute

Open
#44,031 1 comment 2 reactions 0 assignees View on GitHub
area-mvc enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I add the `RangeAttribute` to specify a min and max value for my number input.

```
[Range(1, 10)]
public int Quantity { get; set; }
```

As expected, I get the `data-val`,` data-val-range`, `data-val-range-min`, and `data-val-range-max` attributes on my input. The client-side validation works correctly, but it seems like reasonable behavior to also set the `min` and `max` attributes. That way the browser can enforce the range so the user can't enter a bad value to begin with.

There is precedent for this with `MaxLengthAttribute` and `StringLengthAttribute`.

We can see [here](https://github.com/dotnet/aspnetcore/blob/7fac52dea79a90143715201d2b1f62289d1ef608/src/Mvc/Mvc.ViewFeatures/src/DefaultHtmlGenerator.cs#L1438) that the `DefaultHtmlGenerator.AddMaxLengthAttribute` method adds the `maxlength` HTML attribute when either `MaxLengthAttribute` or `StringLengthAttribute` is present.

### Describe the solution you'd like

`DefaultHtmlGenerator` could have an `AddMinAndMaxAttributes` method that adds the `min` and `max` HTML attributes if the `RangeAttribute` is present.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.