Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:17:58+00:00 2026-05-30T23:17:58+00:00

I just implemented FluentValidation , everything seems awesome except one awkward little detail. @using

  • 0

I just implemented FluentValidation, everything seems awesome except one awkward little detail.

@using (Html.BeginForm("Create", "Posts", FormMethod.Post, new { @class = "create-post" }))
{
    @Html.TextBoxFor(m => m.Text, new { @class = "post-input" })
    @Html.Validation(m => m.Text)
    <input type="submit" value="@Html.Resource("Publish")" />
}

    public static MvcHtmlString Validation<T>(this HtmlHelper<T> helper, Expression<Func<T, string>> expression)
    {
        return helper.ValidationMessageFor(expression, null, Resources.Constants.ModelValidationMessageCssClass);
    }

With the default implementation from ASP.NET MVC 3, this created an <span> that had whatever class the validator deemed appropriate AND input-validation (which is the one from my resource). FluentValidation seems to just ignore this setting. Any idea how I can add a CSS class selector to the span generated by FluentValidation?

On a similar note, take for example this output it produces:

<form method="post" class="create-post" action="/posts/create" novalidate="novalidate">
    <input type="text" value="" name="Text" id="Text" data-val-required="complete this!" data-val-regex-pattern="long-stuff" data-val-regex="wrong regex!" data-val="true" class="post-input input-validation-error">
    <span data-valmsg-replace="true" data-valmsg-for="Text" class="field-validation-error">
        <span for="Text" generated="true" class="">wrong regex!</span>
    </span>
    <input type="submit" value="Publicar!">
</form>

I feel I’m back in ASP.NET WebForms here 🙁 .. , how can I remove stuff like novalidate="true" or the generated="true" attribute? I’m fine with data attributes, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:17:59+00:00Added an answer on May 30, 2026 at 11:17 pm

    So the first thing I think is causing trouble is your Validation extension method. It calls ValidationMessageFor, which is an MVC3 extension on the HTML helper, but with a signature that would match passing it the validation message instead:

    public static MvcHtmlString ValidationMessageFor<TModel, TProperty>
        (this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage)
    

    Based on your code, I would actually expect the error message to be the CSS class name in your resource, based on this snippet from how the <span> is generated:

    if (!String.IsNullOrEmpty(validationMessage)) {
            builder.SetInnerText(validationMessage);
        }
        else if (modelError != null) {
            builder.SetInnerText(GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState));
        }
    

    So you probably want to modify your extension to something like (if it doesn’t get overruled):

    public static MvcHtmlString Validation<T>(this HtmlHelper<T> helper, Expression<Func<T, string>> expression)
    {
        return helper.ValidationMessageFor(expression, null, null, new { _class = Resources.Constants.ModelValidationMessageCssClass });
    }
    

    Bottom line though, I don’t think FluentValidation has anything to do with the markup generated – it should only be feeding your validation errors to ModelState, and ASP.NET MVC is doing the rest. If you want to take even more control you could just write your own HTML tag builder; I grabbed most of this from the MVC3 source, so you could follow their patterns or go a different way. You can also reference the FluentValidation source for some more insights.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just implemented uploadify in my project, and I noticed what seems like an
I am currently building a web site and I just implemented SqlCacheDependency using LinqToSQL
I am developing a Silverlight app using VS2008 Express. I have just implemented a
I've just implemented SQLCipher in my app to encrypt one fairly simple database. I
I have just implemented Facebook Authentication on my website using Facebook JavaScript. If user
I've just implemented django-comments. settings.py INSTALLED_APPS = ( ... 'django.contrib.comments', ) product_detail.html {% get_comment_count
I have just implemented URL Rewrite Module 2.0 for IIS 7 and it seems
I've just implemented OmniAuth (using Ryan Bates' Screencast http://asciicasts.com/episodes/235-omniauth-part-1 ) and am writing Rspec
I just implemented a number of custom counter_cache s using code like this: def
I just implemented the android server check in my app. I am using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.