0

This is different than the one that it just got marked as a duplicate of because I'm not wondering about how to validate a date. I got that part figured out. I want to know how to get HTML to render correctly on the page. Like my question says.

I'm writing a custom validation because the Range attribute doesn't seem to work with dates in mvc annotations.

So I've written the validations and everything seems to work, but I can't get it to render HTML when the error message is displayed.

this.ErrorMessage = string.Format(@"To verify item prior to {0},  please visit the [an anchor tag] for further details.", minimumDate);

The anchor tag then is of course printing out raw html and not rendering it as an anchor tag.

If I put this same exact message in the Required annotation constructor it actually renders the html on the page and looks like I want it to so I know it's possible. Is there anything that I'm missing?

Thanks.

EDIT

The way I fixed this for now:

$(document).ready(function () {
    $(".field-validation-error").html($(".field-validation-error").text());
});

I'm not crazy about it, but it seems to be the only way I can do it right now. I'm still open to any new suggestions once this gets the duplicate tag removed.

asven
  • 133
  • 1
  • 7
  • *I'm writing a custom validation*. You mean that you have created your own `DateValidator` ? A class that inherits from `ValidationAttribute`? – Salah Akbari Mar 12 '18 at 19:41
  • 2
    You may have to use an `@Html.Raw(...)` in your view to display the validation message. – sovemp Mar 12 '18 at 19:42
  • You are correct S. Akbari. I've also tried to use that, but then I get this outputted: Microsoft.AspNetCore.Mvc.Rendering.TagBuilder Here's what I'm putting into my view: @Html.Raw(Html.ValidationMessageFor(m => m.Date)) – asven Mar 12 '18 at 20:07
  • Why are you expecting the dupe to be removed? It shows how you should be doing this. It is pointless to write you own custom validation attribute (and you did not even show you code for it) - the `[Range]` will work fine, but you just need to override the validator as explained in the dupe to make client side validation work correctly –  Mar 13 '18 at 23:29
  • Because I'm now wondering how to get the HTML to render correctly, not how to make the range work. In my mind those are 2 different questions. Rather that hack around built in jQuery validation, it seems more supported to use a custom validator and just make the HTML render correctly. – asven Mar 14 '18 at 23:57
  • @asven. First it not a hack! And how in the world do you think the 'correct html' has anything to do with client side validation. Client side validation in asp.net-mvc is done using the `jquery.validate.js` plugin, so you need to override its defaults if you want it to work for dates (just as you would need to override the defaults is you were rending dates in `dd/MM/yyyy` format, or rendering numbers with the comma as a decimal separator for example). –  Mar 15 '18 at 00:03

0 Answers0