DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
224 stars 79 forks source link

Render required attribute by [Required] attribute in model and with fluent method call (Bootstrap 3, Mvc5) #370

Closed csharpparadise closed 9 years ago

csharpparadise commented 9 years ago

Hi. Is it possible to render a required attribute in a textbox or so with a model property annotated with a [Required] attribute?

Code sample is (model): public class Person { [Required] public string Username { get; set; }

[Required]
[MinLength(8)]
public string Password { get; set; }

}

UI: @using (var f = Html.Bootstrap().Begin(new Form())) { @f.FormGroup().TextBoxFor(x => x.Username).Placeholder("Enter email") @f.FormGroup().PasswordFor(x => x.Password).HtmlAttributes(new {required="required"}).Placeholder("Enter super password") @f.FormGroup().CheckBox("hist") @Html.Bootstrap().SubmitButton().Text("Sign in") }

In the snippet, i have added a HtmlAttributes call for adding the attribute. But i want to add this attribute by model or if not annotated in the model with a fluent call e.g. ".Required()".

Thx.

DmitryEfimenko commented 9 years ago

This is not in the specs for MVC validation, and therefore I'd rather avoid making the changes that are for some custom solution and would affect all users of BMVC. What is the underlying reason for this? Perhaps, what you want to achieve can be done differently.

csharpparadise commented 9 years ago

I need to add the HTML5 specific attributes, e.g. required. This is possible by a custom html attribute. So you are right, while this is not part of Bootstrap. Thanks.