How to customize the default error message "The value '' is invalid" for dynamically generated form
-
I am new to asp.net core. I have a form which is generated dynamically. I want the validation error message to be "The value must be numeric" instead I get "The value 'a' is invalid" when I submit the form. Here is my View Model:
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric")]
public List Units_Service { get; set; }Here is my form code:
for (int i = 0; i < Model.Workload_Category_Name.Count; i++)
{
<div class="row">
<div class="col-md-3">
<b></u>@Html.DisplayFor(model => model.Workload_Category_Name[i])</u> </b>
</div>
<div class="col-md-4">
@Html.TextBoxFor(model => model.Units_Service[i], new { style = "width: 15%", MaskedTextBox = "9999" })
@Html.ValidationMessageFor(model => model.Units_Service[i])
</div>
</div>
}Despite the fact, I have put the custom error message in my View Model as shown above, I keep getting the default message "The value '' is invalid". Please what is the solution to this kind of scenario ?
-
I am new to asp.net core. I have a form which is generated dynamically. I want the validation error message to be "The value must be numeric" instead I get "The value 'a' is invalid" when I submit the form. Here is my View Model:
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric")]
public List Units_Service { get; set; }Here is my form code:
for (int i = 0; i < Model.Workload_Category_Name.Count; i++)
{
<div class="row">
<div class="col-md-3">
<b></u>@Html.DisplayFor(model => model.Workload_Category_Name[i])</u> </b>
</div>
<div class="col-md-4">
@Html.TextBoxFor(model => model.Units_Service[i], new { style = "width: 15%", MaskedTextBox = "9999" })
@Html.ValidationMessageFor(model => model.Units_Service[i])
</div>
</div>
}Despite the fact, I have put the custom error message in my View Model as shown above, I keep getting the default message "The value '' is invalid". Please what is the solution to this kind of scenario ?
The problem is, you're validating the
Units_Service
property, not the individual items within it. And applying a regular expression validation to anything other than astring
property makes no sense. Probably the simplest option would be to create a viewmodel for each item in the list - for example:public class UnitsServiceViewModel
{
[Required(ErrorMessage = "You must enter a value.")]
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric.")]
public string Value { get; set; }
}public class OuterViewModel
{
public List Units_Service { get; set; }internal List Units\_Service\_Parsed { get { if (Units\_Service is null) return null; List value = new(Units\_Service.Count); foreach (UnitsServiceViewModel vm in Units\_Service) { int.TryParse(vm.Value, out int i); value.Add(i); } return value; } set { if (value is null) { Units\_Service = null; } else { List list = new(value.Count); foreach (int i in value) { list.Add(new() { Value = i.ToString() }); } Units\_Service = list; } } }
}
@for (int i = 0; i < Model.Units_Service.Count; i++)
{@Html.LabelFor(model => model.Units\_Service\[i\].Value, Model.Workload\_Category\_Name\[i\]) @Html.TextBoxFor(model => model.Units\_Service\[i\].Value, new { style = "width: 15%", MaskedTextBox = "9999", inputmode = "numeric" }) @Html.ValidationMessageFor(model => model.Units\_Service\[i\].Value)
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The problem is, you're validating the
Units_Service
property, not the individual items within it. And applying a regular expression validation to anything other than astring
property makes no sense. Probably the simplest option would be to create a viewmodel for each item in the list - for example:public class UnitsServiceViewModel
{
[Required(ErrorMessage = "You must enter a value.")]
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric.")]
public string Value { get; set; }
}public class OuterViewModel
{
public List Units_Service { get; set; }internal List Units\_Service\_Parsed { get { if (Units\_Service is null) return null; List value = new(Units\_Service.Count); foreach (UnitsServiceViewModel vm in Units\_Service) { int.TryParse(vm.Value, out int i); value.Add(i); } return value; } set { if (value is null) { Units\_Service = null; } else { List list = new(value.Count); foreach (int i in value) { list.Add(new() { Value = i.ToString() }); } Units\_Service = list; } } }
}
@for (int i = 0; i < Model.Units_Service.Count; i++)
{@Html.LabelFor(model => model.Units\_Service\[i\].Value, Model.Workload\_Category\_Name\[i\]) @Html.TextBoxFor(model => model.Units\_Service\[i\].Value, new { style = "width: 15%", MaskedTextBox = "9999", inputmode = "numeric" }) @Html.ValidationMessageFor(model => model.Units\_Service\[i\].Value)
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks Richard. Your answer works perfect :)
-
I am new to asp.net core. I have a form which is generated dynamically. I want the validation error message to be "The value must be numeric" instead I get "The value 'a' is invalid" when I submit the form. Here is my View Model:
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric")]
public List Units_Service { get; set; }Here is my form code:
for (int i = 0; i < Model.Workload_Category_Name.Count; i++)
{
<div class="row">
<div class="col-md-3">
<b></u>@Html.DisplayFor(model => model.Workload_Category_Name[i])</u> </b>
</div>
<div class="col-md-4">
@Html.TextBoxFor(model => model.Units_Service[i], new { style = "width: 15%", MaskedTextBox = "9999" })
@Html.ValidationMessageFor(model => model.Units_Service[i])
</div>
</div>
}Despite the fact, I have put the custom error message in my View Model as shown above, I keep getting the default message "The value '' is invalid". Please what is the solution to this kind of scenario ?
-
Fokwa Divine wrote:
"^[0-9]*$"
Note that this (and in the corrected code) allows for an empty string. You probably want the following
^[0-9]+$
You might also want to check for limits. So is '0' a correct value? Is '1000000000'?
Thanks for your input jschell Yes, I am checking for limit and '0' is a correct value per requirement