Skip to content
This repository has been archived by the owner on Jan 11, 2018. It is now read-only.

Populate the label 'for' with the control id #41

Merged
merged 1 commit into from Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions FluentBootstrap.Mvc/Forms/FormControlOverride.cs
Expand Up @@ -13,9 +13,6 @@ internal class FormControlOverride<TModel> : ComponentOverride<FormControl>
{
protected override void OnStart(TextWriter writer)
{
Component.Prepare(writer);

// Add the validation data
string name = Component.GetAttribute("name");
if (!string.IsNullOrWhiteSpace(name))
{
Expand All @@ -28,7 +25,13 @@ protected override void OnStart(TextWriter writer)
}
tagBuilder.GenerateId(name);
Component.MergeAttribute("id", tagBuilder.Attributes["id"]);
}

Component.Prepare(writer);

// Add the validation data
if (!string.IsNullOrWhiteSpace(name))
{
// Set the validation class
ModelState modelState;
MvcBootstrapConfig<TModel> config = (MvcBootstrapConfig<TModel>)Config;
Expand Down
7 changes: 3 additions & 4 deletions FluentBootstrap.Mvc/Forms/MvcFormExtensions.cs
Expand Up @@ -389,7 +389,7 @@ public static ValidationSummary<TModel> IncludePropertyErrors<TModel>(this Valid
this ComponentBuilder<MvcBootstrapConfig<TModel>, TFormControl> builder, Expression<Func<TModel, TValue>> expression, Action<ControlLabel> labelAction = null)
where TFormControl : FormControl
{
ControlLabel controlLabel = GetControlLabel(builder.GetHelper(), expression).For(builder.GetComponent().GetAttribute("name")).GetComponent();
ControlLabel controlLabel = GetControlLabel(builder.GetHelper(), expression).For(TagBuilder.CreateSanitizedId(builder.GetComponent().GetAttribute("name"))).GetComponent();
if (labelAction != null)
{
labelAction(controlLabel);
Expand All @@ -406,14 +406,13 @@ public static ValidationSummary<TModel> IncludePropertyErrors<TModel>(this Valid
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.GetConfig().HtmlHelper.ViewData);
string name = GetControlName(helper, expressionText);
string label = GetControlLabel(metadata, expressionText);
return new MvcBootstrapHelper<TModel>(helper.GetConfig().HtmlHelper).ControlLabel(label).For(name);
return new MvcBootstrapHelper<TModel>(helper.GetConfig().HtmlHelper).ControlLabel(label).For(TagBuilder.CreateSanitizedId(name));
}

private static string GetControlName<TComponent, TModel>(BootstrapHelper<MvcBootstrapConfig<TModel>, TComponent> helper, string expressionText)
where TComponent : Component
{
return TagBuilder.CreateSanitizedId(
helper.GetConfig().HtmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText));
return helper.GetConfig().HtmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
}

private static string GetControlLabel(ModelMetadata metadata, string expressionText)
Expand Down
7 changes: 7 additions & 0 deletions FluentBootstrap.Tests.Web/Models/MvcTests/ViewModel.cs
Expand Up @@ -14,5 +14,12 @@ public class ViewModel
public string PropC { get; set; }
public Dictionary<int, string> PropCOptions { get; set; }
public bool PropD { get; set; }
public ChildModel Child { get; set; }
}

public class ChildModel
{
[Display(Name = "Child Property A")]
public string ChildPropA { get; set; }
}
}
9 changes: 9 additions & 0 deletions FluentBootstrap.Tests.Web/Views/MvcTests/MvcForms.cshtml
Expand Up @@ -139,4 +139,13 @@
@form.SelectFor(x => x.PropC).AddOptions(Model.PropCOptions.Select(y => new KeyValuePair<string, string>(y.Key.ToString(), y.Value)))
}
}

@Html.Bootstrap().Heading1("Input For With Dotted Child")
using (Html.Bootstrap().Div().SetId("test-input-for-dotted").Begin())
{
using (var form = Html.Bootstrap().Form().HideValidationSummary().Begin())
{
@form.InputFor(x => x.Child.ChildPropA)
}
}
}
12 changes: 12 additions & 0 deletions FluentBootstrap.Tests/MvcFormsFixture.cs
Expand Up @@ -189,6 +189,18 @@ public void SelectForWithAddOptionsProducesCorrectHtml()
<option value=""Three"">3
</select>
</div>
</form>");
}

[Test]
public void InputForDottedProducesCorrectHtml()
{
TestHelper.AssertMvcHtml<ASP._Views_MvcTests_MvcForms_cshtml>("test-input-for-dotted",
@"<form role=""form"" method=""post"">
<div class=""form-group"">
<label for=""Child_ChildPropA"" class=""control-label"">Child Property A</label>
<input type=""text"" name=""Child.ChildPropA"" id=""Child_ChildPropA"" class=""form-control"">
</div>
</form>");
}
}
Expand Down
6 changes: 5 additions & 1 deletion FluentBootstrap.Tests/TestHelper.cs
Expand Up @@ -82,7 +82,11 @@ public static void AssertMvcHtml<TView>(string containerId, string expected, boo
{ 2, "Two"},
{ 3, "Three"}
},
PropD = true
PropD = true,
Child = new ChildModel()
{
ChildPropA = "ChildA"
}
};
HtmlDocument doc = Render<TView, ViewModel>(model);
expected = expected.Replace("\r\n", "\n");
Expand Down
8 changes: 4 additions & 4 deletions FluentBootstrap/Forms/FormControl.cs
Expand Up @@ -81,11 +81,11 @@ public void Prepare(TextWriter writer)
// Add the label to the form group or write it
if (_label != null)
{
// Set the label's for attribute to the input name
string name = Attributes.GetValue("name");
if (!string.IsNullOrWhiteSpace(name))
// Set the label's for attribute to the input id
string id = Attributes.GetValue("id");
if (!string.IsNullOrWhiteSpace(id))
{
_label.MergeAttribute("for", name);
_label.MergeAttribute("for", id);
}

// Add or write the label
Expand Down