Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 26, 2010 06:37 PM by ricka6
Contributor
3932 Points
3789 Posts
Mar 26, 2010 12:27 AM|LINK
Hello,
I would like to change how the ValidateFor renders from:
<span>Error message</span>
To:
<div><span>Error message</span></div>
I think I should, maybe, create a new extension that calls ValidateFor and replaces the html before returning. Something like:
// ValidationFor public static void ValidationFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { return Regex.Replace(htmlHelper.ValidateFor(expression), "<span([^>]*)>([^<]*)</span>", "<div$1><span>$2</span></div>"); } // ValidationFor
However the ValidateFor returns nothing instead of a string.
How can I solve this problem?
Thank You,
Miguel
4426 Points
864 Posts
Mar 26, 2010 01:22 AM|LINK
The span has a css class assigned to it, so it actually looks like this: <span class="field-validation-error"
In your css, do this and it will appear as a div tag:
<style> .field-validation-error { display: block; } </style>
Mar 26, 2010 02:15 AM|LINK
Yes, I know.
But I would like to have <div><span>Error Message</span></div>
That is the only way to get the styling I need.
Mar 26, 2010 02:25 AM|LINK
I have this working with ValidationMessage but not with ValidatorFor ... And this is what I was trying to do.
Does anyone, please, know how to do this?
Mar 26, 2010 12:18 PM|LINK
Please, anyone?
I have been looking at ValidationExtensions code in MVC source and I can't figure out a way to create a ValidateFor extension that renders diferently.
My problem is that calling ValidateFor renders nothing ...
Anyone? Any idea?
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Mar 26, 2010 06:37 PM|LINK
see my blog http://blogs.msdn.com/rickandy/archive/2009/08/18/dry-ing-out-the-mvc-2-templated-helpers.aspx where I do this. ValidationFor is now supported - so don't implement that.
shapper
Contributor
3932 Points
3789 Posts
ValidateFor Extension
Mar 26, 2010 12:27 AM|LINK
Hello,
I would like to change how the ValidateFor renders from:
<span>Error message</span>
To:
<div><span>Error message</span></div>
I think I should, maybe, create a new extension that calls ValidateFor and replaces the html before returning. Something like:
// ValidationFor public static void ValidationFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { return Regex.Replace(htmlHelper.ValidateFor(expression), "<span([^>]*)>([^<]*)</span>", "<div$1><span>$2</span></div>"); } // ValidationForHowever the ValidateFor returns nothing instead of a string.
How can I solve this problem?
Thank You,
Miguel
andrewjboyd
Contributor
4426 Points
864 Posts
Re: ValidateFor Extension
Mar 26, 2010 01:22 AM|LINK
The span has a css class assigned to it, so it actually looks like this: <span class="field-validation-error"
In your css, do this and it will appear as a div tag:
<style> .field-validation-error { display: block; } </style>================================================
Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
shapper
Contributor
3932 Points
3789 Posts
Re: ValidateFor Extension
Mar 26, 2010 02:15 AM|LINK
Yes, I know.
But I would like to have <div><span>Error Message</span></div>
That is the only way to get the styling I need.
shapper
Contributor
3932 Points
3789 Posts
Re: ValidateFor Extension
Mar 26, 2010 02:25 AM|LINK
I have this working with ValidationMessage but not with ValidatorFor ... And this is what I was trying to do.
Does anyone, please, know how to do this?
Thank You,
Miguel
shapper
Contributor
3932 Points
3789 Posts
Re: ValidateFor Extension
Mar 26, 2010 12:18 PM|LINK
Please, anyone?
I have been looking at ValidationExtensions code in MVC source and I can't figure out a way to create a ValidateFor extension that renders diferently.
My problem is that calling ValidateFor renders nothing ...
Anyone? Any idea?
Thank You,
Miguel
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: ValidateFor Extension
Mar 26, 2010 06:37 PM|LINK
see my blog http://blogs.msdn.com/rickandy/archive/2009/08/18/dry-ing-out-the-mvc-2-templated-helpers.aspx where I do this. ValidationFor is now supported - so don't implement that.