Closed suatsuphi closed 3 months ago
Postback happens when using RuleFor with When in FluentValidation... Is it normal ?
Yes, it is. Because When()
can contain complex expressions that may not be processable on the client-side.
There are two problems when I use "when" for postback.
1- html b tag is not processed 2- is-invalid class is not added to input. Instead input-validation-error is added.
1- html b tag is not processed
Where does the b
tag come from? We don't render HTML tags in standard validation messages. Maybe a custom message? You have to call Html.Raw(...)
in the template that renders the validation messages in order to output the message as is.
2- is-invalid class is not added to input. Instead input-validation-error is added.
Is fixed by 2d2b15f
1- html b tag is not processed
public class PageObjectModelValidator : SmartValidator<PageObjectModel>
or
public class PageObjectModelValidator : AbstractValidator<PageObjectModel>
When(x => (x.Zone?.Contains("PopupWindow")).GetValueOrDefault(false), () =>
{
RuleFor(x => x.Delay).NotEmpty();
});
or
When(x => (x.Zone?.Contains("PopupWindow")).GetValueOrDefault(false), () =>
{
RuleFor(x => x.Delay).NotEmpty().WithMessage(T("Plugins.Smartstore.BT.NotEmptyValidator"));
});
I use model's validator and get the same result
for testing purposes @Html.ValidationMessageFor(m => m.Delay).ToHtmlString().Value
<div class="adminRow">
<div class="adminTitle">
<smart-label asp-for="Delay" />
</div>
<div class="adminData">
<editor asp-for="Delay" />
<span asp-validation-for="Delay"></span>
@Html.ValidationMessageFor(m => m.Delay).ToHtmlString().Value
</div>
</div>
result : <b>Delay</b> cannot be empty
<b>Delay</b> cannot be empty
<span class="field-validation-error" data-valmsg-for="Delay" data-valmsg-replace="true"><b>Delay</b> cannot be empty</span>
Just @Html.ValidationMessageFor(m => m.Delay)
is sufficient. You could also try @Html.Raw(@Html.ValidationMessageFor(m => m.Delay))
<span>@Html.Raw(ViewData.ModelState["Delay"]?.Errors.FirstOrDefault()?.ErrorMessage)</span>
If I use it this way, my question is solved.
Source of the problem asp-validation-for="Delay"
<span asp-validation-for="Delay"></span>
the same problem in <div asp-validation-summary="All"></div>
This problem occurs when postback.
<div class="mt-4" asp-validation-summary="All"></div>
<span asp-validation-for="PropertyName"></span>
solution to the problem
$(document).ready(function () {
$('.field-validation-error').each(function () {
$(this).html($(this).text());
});
$('.validation-summary-errors').each(function () {
$(this).html($(this).text());
});
});
chatgpt suggested something like this... replaces the message that comes with ajaxComplete.
$(document).ajaxComplete(function () {
$('.field-validation-error').each(function () {
var rawHtml = $(this).html();
$(this).html(rawHtml.replace(/</g, '<').replace(/>/g, '>'));
});
$('.validation-summary-errors').each(function () {
var rawHtml = $(this).html();
$(this).html(rawHtml.replace(/</g, '<').replace(/>/g, '>'));
});
});
Hi,
Postback happens when using RuleFor with When in FluentValidation... Is it normal ?