-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Custom DataAnnotation ValidationAttribute error message is not showing #15301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yes, just pass the member name in validation context in below line: Please try and let me know, if any issue. Will post source code reference. |
@PKYADAV Thank you for the help! I ended up ditching DataAnnotations out of frustration with custom attributes, and am now using FluentValidation. I appreciate such a quick response! 😄 |
Hii @pelhamrj, |
this is works for me but why? in asp.net projects this wasn't needed. |
Can you share more information and scenario on that you want to achieve? |
well, in asp.net project when I used custom data annotation attributes, I didn't need to explicitly put input name in returning of |
At least in Blazor Server 3.0 we can confirm the same behavior that @dev-masih is reporting. |
We were trying to use custom validation in Blazor Server 3.0. We were not having any success until we found this issue. Just wanted to share a clear example of what works for us in case it helps anyone.
|
@PKYADAV, copying my comment from #10643 (comment) As noted in #10643 (comment), on a form submit Blazor's In 3.1, we're doing a couple of things to address this:
<EditForm Model="myModel" OnValidSubmit="OnSubmit">
<DataAnnotationsValidator />
<InputText Name="Password" /> <ValidationMessage For=@(() => myModel.Password)" />
<InputText Name="ConfirmPassword" /> <ValidationMessage For=@(() => myModel.ConfirmPassword)" />
<ValidationSummary Model="myModel" />
<button class="btn btn-danger" type="submit">Submit</button>
</EditForm> The
This package is marked experimental since our plan is to roll these changes in to CoreFx as part of the 5.0 milestone. Hope this helps. |
Model Definition:
ValidationAttribute:
public class DbRequired : RequiredAttribute
{
DataContext db;
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if ((value == null || value.ToString() == string.Empty || value.ToString() == "0"))
{
return new ValidationResult("USER NAME IS REQUIRED FIELD !");
}
else
{
return ValidationResult.Success;
}
}
}
When using builtin Required then working fine but when overriding RequiredAttribute then error message not displayed:

The text was updated successfully, but these errors were encountered: