2721: Fixing the error message for empty html field name
This commit is contained in:
parent
6170ac1924
commit
a6a7903b84
|
|
@ -762,6 +762,22 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
|
||||||
return string.Format(CultureInfo.CurrentCulture, GetString("RemoteAttribute_RemoteValidationFailed"), p0);
|
return string.Format(CultureInfo.CurrentCulture, GetString("RemoteAttribute_RemoteValidationFailed"), p0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of an HTML field cannot be null or empty. Instead use methods {0}.{1} or {2}.{3} with a non-empty {4} argument value.
|
||||||
|
/// </summary>
|
||||||
|
internal static string HtmlGenerator_FieldNameCannotBeNullOrEmpty
|
||||||
|
{
|
||||||
|
get { return GetString("HtmlGenerator_FieldNameCannotBeNullOrEmpty"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of an HTML field cannot be null or empty. Instead use methods {0}.{1} or {2}.{3} with a non-empty {4} argument value.
|
||||||
|
/// </summary>
|
||||||
|
internal static string FormatHtmlGenerator_FieldNameCannotBeNullOrEmpty(object p0, object p1, object p2, object p3, object p4)
|
||||||
|
{
|
||||||
|
return string.Format(CultureInfo.CurrentCulture, GetString("HtmlGenerator_FieldNameCannotBeNullOrEmpty"), p0, p1, p2, p3, p4);
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetString(string name, params string[] formatterNames)
|
private static string GetString(string name, params string[] formatterNames)
|
||||||
{
|
{
|
||||||
var value = _resourceManager.GetString(name);
|
var value = _resourceManager.GetString(name);
|
||||||
|
|
|
||||||
|
|
@ -413,8 +413,15 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
||||||
if (string.IsNullOrEmpty(fullName))
|
if (string.IsNullOrEmpty(fullName))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(expression));
|
throw new ArgumentException(
|
||||||
}
|
Resources.FormatHtmlGenerator_FieldNameCannotBeNullOrEmpty(
|
||||||
|
typeof(IHtmlHelper).FullName,
|
||||||
|
nameof(IHtmlHelper.Editor),
|
||||||
|
typeof(IHtmlHelper<>).FullName,
|
||||||
|
nameof(IHtmlHelper<object>.EditorFor),
|
||||||
|
"htmlFieldName"),
|
||||||
|
nameof(expression));
|
||||||
|
}
|
||||||
|
|
||||||
// If we got a null selectList, try to use ViewData to get the list of items.
|
// If we got a null selectList, try to use ViewData to get the list of items.
|
||||||
if (selectList == null)
|
if (selectList == null)
|
||||||
|
|
@ -483,7 +490,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
||||||
if (string.IsNullOrEmpty(fullName))
|
if (string.IsNullOrEmpty(fullName))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(expression));
|
throw new ArgumentException(
|
||||||
|
Resources.FormatHtmlGenerator_FieldNameCannotBeNullOrEmpty(
|
||||||
|
typeof(IHtmlHelper).FullName,
|
||||||
|
nameof(IHtmlHelper.Editor),
|
||||||
|
typeof(IHtmlHelper<>).FullName,
|
||||||
|
nameof(IHtmlHelper<object>.EditorFor),
|
||||||
|
"htmlFieldName"),
|
||||||
|
nameof(expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
ModelState modelState;
|
ModelState modelState;
|
||||||
|
|
@ -563,7 +577,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
||||||
if (string.IsNullOrEmpty(fullName))
|
if (string.IsNullOrEmpty(fullName))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(expression));
|
throw new ArgumentException(
|
||||||
|
Resources.FormatHtmlGenerator_FieldNameCannotBeNullOrEmpty(
|
||||||
|
typeof(IHtmlHelper).FullName,
|
||||||
|
nameof(IHtmlHelper.Editor),
|
||||||
|
typeof(IHtmlHelper<>).FullName,
|
||||||
|
nameof(IHtmlHelper<object>.EditorFor),
|
||||||
|
"htmlFieldName"),
|
||||||
|
nameof(expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
var formContext = viewContext.ClientValidationEnabled ? viewContext.FormContext : null;
|
var formContext = viewContext.ClientValidationEnabled ? viewContext.FormContext : null;
|
||||||
|
|
@ -738,7 +759,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
||||||
if (string.IsNullOrEmpty(fullName))
|
if (string.IsNullOrEmpty(fullName))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(expression));
|
throw new ArgumentException(
|
||||||
|
Resources.FormatHtmlGenerator_FieldNameCannotBeNullOrEmpty(
|
||||||
|
typeof(IHtmlHelper).FullName,
|
||||||
|
nameof(IHtmlHelper.Editor),
|
||||||
|
typeof(IHtmlHelper<>).FullName,
|
||||||
|
nameof(IHtmlHelper<object>.EditorFor),
|
||||||
|
"htmlFieldName"),
|
||||||
|
nameof(expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = allowMultiple ? typeof(string[]) : typeof(string);
|
var type = allowMultiple ? typeof(string[]) : typeof(string);
|
||||||
|
|
@ -969,7 +997,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
var fullName = GetFullHtmlFieldName(viewContext, expression);
|
||||||
if (string.IsNullOrEmpty(fullName))
|
if (string.IsNullOrEmpty(fullName))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(expression));
|
throw new ArgumentException(
|
||||||
|
Resources.FormatHtmlGenerator_FieldNameCannotBeNullOrEmpty(
|
||||||
|
typeof(IHtmlHelper).FullName,
|
||||||
|
nameof(IHtmlHelper.Editor),
|
||||||
|
typeof(IHtmlHelper<>).FullName,
|
||||||
|
nameof(IHtmlHelper<object>.EditorFor),
|
||||||
|
"htmlFieldName"),
|
||||||
|
nameof(expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
var tagBuilder = new TagBuilder("input", _htmlEncoder);
|
var tagBuilder = new TagBuilder("input", _htmlEncoder);
|
||||||
|
|
|
||||||
|
|
@ -259,4 +259,7 @@
|
||||||
<data name="RemoteAttribute_RemoteValidationFailed" xml:space="preserve">
|
<data name="RemoteAttribute_RemoteValidationFailed" xml:space="preserve">
|
||||||
<value>'{0}' is invalid.</value>
|
<value>'{0}' is invalid.</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="HtmlGenerator_FieldNameCannotBeNullOrEmpty" xml:space="preserve">
|
||||||
|
<value>The name of an HTML field cannot be null or empty. Instead use methods {0}.{1} or {2}.{3} with a non-empty {4} argument value.</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
|
|
@ -60,6 +60,116 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
Assert.Null(result);
|
Assert.Null(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetCurrentValues_WithNullExpression_Throws()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var metadataProvider = new TestModelMetadataProvider();
|
||||||
|
var htmlGenerator = GetGenerator(metadataProvider);
|
||||||
|
var viewContext = GetViewContext<Model>(model: null, metadataProvider: metadataProvider);
|
||||||
|
var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model: null);
|
||||||
|
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
|
// Act and assert
|
||||||
|
var ex = Assert.Throws<ArgumentException>(
|
||||||
|
"expression",
|
||||||
|
() => htmlGenerator.GetCurrentValues(
|
||||||
|
viewContext,
|
||||||
|
modelExplorer,
|
||||||
|
expression: null,
|
||||||
|
allowMultiple: true));
|
||||||
|
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateSelect_WithNullExpression_Throws()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var metadataProvider = new TestModelMetadataProvider();
|
||||||
|
var htmlGenerator = GetGenerator(metadataProvider);
|
||||||
|
var viewContext = GetViewContext<Model>(model: null, metadataProvider: metadataProvider);
|
||||||
|
var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model: null);
|
||||||
|
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
|
// Act and assert
|
||||||
|
var ex = Assert.Throws<ArgumentException>(
|
||||||
|
"expression",
|
||||||
|
() => htmlGenerator.GenerateSelect(
|
||||||
|
viewContext,
|
||||||
|
modelExplorer,
|
||||||
|
"label",
|
||||||
|
null,
|
||||||
|
new List<SelectListItem>(),
|
||||||
|
true,
|
||||||
|
null));
|
||||||
|
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateTextArea_WithNullExpression_Throws()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var metadataProvider = new TestModelMetadataProvider();
|
||||||
|
var htmlGenerator = GetGenerator(metadataProvider);
|
||||||
|
var viewContext = GetViewContext<Model>(model: null, metadataProvider: metadataProvider);
|
||||||
|
var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model: null);
|
||||||
|
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
|
// Act and assert
|
||||||
|
var ex = Assert.Throws<ArgumentException>(
|
||||||
|
"expression",
|
||||||
|
() => htmlGenerator.GenerateTextArea(
|
||||||
|
viewContext,
|
||||||
|
modelExplorer,
|
||||||
|
null,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
null));
|
||||||
|
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GenerateValidationMessage_WithNullExpression_Throws()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var metadataProvider = new TestModelMetadataProvider();
|
||||||
|
var htmlGenerator = GetGenerator(metadataProvider);
|
||||||
|
var viewContext = GetViewContext<Model>(model: null, metadataProvider: metadataProvider);
|
||||||
|
var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model: null);
|
||||||
|
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
|
// Act and assert
|
||||||
|
var ex = Assert.Throws<ArgumentException>(
|
||||||
|
"expression",
|
||||||
|
() => htmlGenerator.GenerateValidationMessage(
|
||||||
|
viewContext,
|
||||||
|
null,
|
||||||
|
"Message",
|
||||||
|
"tag",
|
||||||
|
null));
|
||||||
|
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(false)]
|
[InlineData(false)]
|
||||||
[InlineData(true)]
|
[InlineData(true)]
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,25 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
Assert.Equal(expected, ex.Message);
|
Assert.Equal(expected, ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CheckBoxWithNullExpressionThrows()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
|
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetTestModelViewData());
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var ex = Assert.Throws<ArgumentException>(
|
||||||
|
"expression",
|
||||||
|
() => helper.CheckBox(null, isChecked: true, htmlAttributes: null));
|
||||||
|
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CheckBoxCheckedWithOnlyName_GeneratesExpectedValue()
|
public void CheckBoxCheckedWithOnlyName_GeneratesExpectedValue()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -403,10 +403,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
{
|
{
|
||||||
{ "class", "some-class"}
|
{ "class", "some-class"}
|
||||||
};
|
};
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
// Act and Assert
|
// Act and Assert
|
||||||
ExceptionAssert.ThrowsArgumentNullOrEmpty(() => helper.Hidden(string.Empty, string.Empty, attributes),
|
ExceptionAssert.ThrowsArgument(
|
||||||
"expression");
|
() => helper.Hidden(string.Empty, string.Empty, attributes),
|
||||||
|
"expression",
|
||||||
|
expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -128,10 +128,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues());
|
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues());
|
||||||
var name = string.Empty;
|
var name = string.Empty;
|
||||||
var value = string.Empty;
|
var value = string.Empty;
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
|
||||||
// Act and Assert
|
// Act and Assert
|
||||||
ExceptionAssert.ThrowsArgumentNullOrEmpty(() => helper.Password(name, value, htmlAttributes: null),
|
ExceptionAssert.ThrowsArgument(
|
||||||
"expression");
|
() => helper.Password(name, value, htmlAttributes: null),
|
||||||
|
"expression",
|
||||||
|
expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -362,6 +362,27 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
Assert.Equal(savedSelected, selectList.Select(item => item.Selected));
|
Assert.Equal(savedSelected, selectList.Select(item => item.Selected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(DropDownListDataSet))]
|
||||||
|
public void DropDownList_WithNullExpression_Throws(
|
||||||
|
IEnumerable<SelectListItem> selectList,
|
||||||
|
string expectedHtml,
|
||||||
|
string ignoredHtml)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expected = "The name of an HTML field cannot be null or empty. Instead use methods " +
|
||||||
|
"Microsoft.AspNet.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNet.Mvc.Rendering." +
|
||||||
|
"IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value." +
|
||||||
|
Environment.NewLine + "Parameter name: expression";
|
||||||
|
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
var ex = Assert.Throws<ArgumentException>(
|
||||||
|
"expression",
|
||||||
|
() => helper.DropDownList(null, selectList: null, optionLabel: null, htmlAttributes: null));
|
||||||
|
Assert.Equal(expected, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[MemberData(nameof(DropDownListDataSet))]
|
[MemberData(nameof(DropDownListDataSet))]
|
||||||
public void DropDownList_WithModelValue_GeneratesExpectedValue(
|
public void DropDownList_WithModelValue_GeneratesExpectedValue(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue