1

FIXED

Instead of using ValidationResult? IsValid(object?, ValidationContext) i just used bool IsValid(object), and that seems to be working fine.


I've created a custom validation attribute for email, but it does not validate as it should.
Atm, i have set it to always fail, but somehow it still displays as if it was valid?
This is the class that is being used as the attribute, as you can see, it should be invalid no matter what.. (Or have i misunderstood)?

public class MailRegex : ValidationAttribute
{
    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
    {
        //if (RegexExpr.MailValidator(value.ToString()))
        //    return ValidationResult.Success;

        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }
}

As you can see here, it gives the error as it should:
Failed IMG
And this is when i press enter:
enter image description here

The property Test is not being changed, when i submit (Which would mean that it isn't valid, right?) It would mean that the input is not valid but is being displayed as a valid input which doens't make any sense.

<EditForm Model="s" OnValidSubmit='(() => Test = "asd")'>
    <DataAnnotationsValidator />
    <InputText placeholder="Mail" @bind-Value="s.Email"></InputText>
    <ValidationMessage For="@(() => s.Email)" />
</EditForm>
@Test
@code {
    public NewClass s { get; set; } = new();
    public string Test = "Test string";

    protected override void OnAfterRender(bool firstRender)
    {
        StateHasChanged();
    }
}

I have tried to remove the validation, and make it fail on purpose (but still displays as valid).
I have not tried anything else really, beacuse i have no clue nor any idea how to fix this.

H H
  • 263,252
  • 30
  • 330
  • 514
WhySoShy
  • 21
  • 3
  • Have you checked your MessageStore? Maybe it gets reinitialized hence you're loosing the error message. Try adding more fields with validation and if those fields are not showing the error as you expect, probably you're reinitializing the MessageStore. – Raffy Jun 09 '23 at 16:43
  • It looks to me that when you submit your form using OnValidSubmit that you THEN change the text to asd. At this point validation has been triggered and you're programmatically changing the value afterwards. – paulpitchford Jun 10 '23 at 07:57
  • Dontk now why it did not work with IsValid(object, ValidationResult).. But it seems to be working with just using the bool IsValid(object), thanks for the help! – WhySoShy Jun 12 '23 at 05:55

0 Answers0