I have some custom validation that I am trying to add to a CreateOrEditDto. As per the documentation I have implmented the ICustomValidate interface. I did a simple test where I add a Validation Result to the context results where I do a very basic check.
The validation result is added and I am presented with a message box that contains my error message and it does not submit the form which is fine.
I have two questions:
- In the form there is no translation for the error title or error narration. How do I go about adding this?
- There is an argument that can be passed to the ValidationResults constructor that takes a string IEnumerable of member names. What is the purpose of passing in this list of property names? I had assumed it would update the form showing the validation errors with the invalid form inputs but it doesn't do this.
I couldn't find anything specific in the documentation regarding the above two questions. Any advice on how this should work would be appreciated.
The code I have is below and a screen grab of the message box attached:
public void AddValidationErrors(CustomValidationContext context)
{
if(Name == "Test")
{
context.Results.Add(new ValidationResult("This is a test", new [] { "Name" }));
}
}
3 Answer(s)
-
0
Hi @james.marley,
- Localization is not possible when you use ICustomValidate. If you have fixed messages, you may use https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#dataannotations-localization.
- For ValidationResult class, you can take a look at, https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validationresult?view=netcore-3.1
-
0
Hi,
I'm not too concerned about Localization for this part. What I cannot find is where [Validation Error] and [Validation narrative title] is coming from. How would you set these to string literals when showing the above message? I will check the first link again that you've sent in more detail to see if I'm missing something.
-
0