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 .Net Framework Validation Error Messages

How to customize the default .Net Framework Validation Error Messages

Scheduled Pinned Locked Moved C#
questioncsharphtmldotnethelp
4 Posts 2 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 have in my view model the following code:

    public DateTime? Activation_Date { get; set; }
    public DateTime? Deactivation_Date { get; set; }

    On the form, if I put the date as "abc" and click Save, I get the following validation error:

    The value 'abc' is not valid for Activation_Date.

    What I simply want is to modify Activation_Date. I want it to be "Activation Date" so the message will look like below. The end user doesn't want the underscore.

    The value 'abc' is not valid for Activation Date.

    View Code to display error messages

    @Html.ValidationMessageFor(s => s.Activation_Date)

    What is the technique to make this kind of change ?

    P 1 Reply Last reply
    0
    • F Fokwa Divine

      I have in my view model the following code:

      public DateTime? Activation_Date { get; set; }
      public DateTime? Deactivation_Date { get; set; }

      On the form, if I put the date as "abc" and click Save, I get the following validation error:

      The value 'abc' is not valid for Activation_Date.

      What I simply want is to modify Activation_Date. I want it to be "Activation Date" so the message will look like below. The end user doesn't want the underscore.

      The value 'abc' is not valid for Activation Date.

      View Code to display error messages

      @Html.ValidationMessageFor(s => s.Activation_Date)

      What is the technique to make this kind of change ?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The ValidationMessageFor extension method has an override that accepts a custom validation message as the second parameter.

      @Html.ValidationMessageFor(s => s.Activation_Date, "Where we're going Marty, we don't need roads");

      Advanced TypeScript Programming Projects

      F 1 Reply Last reply
      0
      • P Pete OHanlon

        The ValidationMessageFor extension method has an override that accepts a custom validation message as the second parameter.

        @Html.ValidationMessageFor(s => s.Activation_Date, "Where we're going Marty, we don't need roads");

        Advanced TypeScript Programming Projects

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

        Hi Pete, It doesn't work for me. @Html.ValidationMessageFor(s => s.Activation_Date, "Activation Date is invalid"); 1. I always have "Activation Date is invalid" visible beneath the activation date input field 2. Secondly, I have other validation error messages in a Validator class for Activation_Date field

        RuleFor(x => x.Activation_Date).NotEmpty().WithMessage("Activation Date is required");
        RuleFor(x => x.Activation_Date).GreaterThanOrEqualTo(DateTime.Today).WithMessage("Activation Date must be greater than or equal to the current date");

        P 1 Reply Last reply
        0
        • F Fokwa Divine

          Hi Pete, It doesn't work for me. @Html.ValidationMessageFor(s => s.Activation_Date, "Activation Date is invalid"); 1. I always have "Activation Date is invalid" visible beneath the activation date input field 2. Secondly, I have other validation error messages in a Validator class for Activation_Date field

          RuleFor(x => x.Activation_Date).NotEmpty().WithMessage("Activation Date is required");
          RuleFor(x => x.Activation_Date).GreaterThanOrEqualTo(DateTime.Today).WithMessage("Activation Date must be greater than or equal to the current date");

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You appear to be mixing FluentValidation with ASP MVC Validation. I would choose one and stick with that. In this case, FluentValidation is going to be more flexible for you.

          Advanced TypeScript Programming Projects

          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