Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to customize the default error message "The value '' is invalid" for dynamically generated form

How to customize the default error message "The value '' is invalid" for dynamically generated form

Scheduled Pinned Locked Moved C#
asp-netquestioncsharphtmldotnet
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fokwa Divine
    wrote on last edited by
    #1

    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 ?

    R J 2 Replies Last reply
    0
    • F Fokwa Divine

      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 ?

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 a string 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

      F 1 Reply Last reply
      0
      • R Richard Deeming

        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 a string 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

        F Offline
        F Offline
        Fokwa Divine
        wrote on last edited by
        #3

        Thanks Richard. Your answer works perfect :)

        1 Reply Last reply
        0
        • F Fokwa Divine

          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 ?

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          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'?

          F 1 Reply Last reply
          0
          • J jschell

            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'?

            F Offline
            F Offline
            Fokwa Divine
            wrote on last edited by
            #5

            Thanks for your input jschell Yes, I am checking for limit and '0' is a correct value per requirement

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups