You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The IValidateObject is indeed a good option. I did not realize that the validations could be implemented in a flexible way. Below an example , Text1 is now fixed and Text2 is dependent on the Status.
I already realized that custom validators is possible as well, but then I have to rewrite the validators which I find not a great solution.
Thanks for the solution.
@using System.ComponentModel.DataAnnotations;
<Select @bind-SelectedValue=mydata.Status>
<SelectItem>New</SelectItem>
<SelectItem>Completed</SelectItem>
</Select>
<Validations @ref=valref1 Model=mydata Mode="ValidationMode.Manual">
<Validation>
<TextEdit @bind-Text=mydata.Text1></TextEdit>
</Validation>
<Validation>
<TextEdit @bind-Text=mydata.Text2></TextEdit>
</Validation>
</Validations>
<Button Background="Background.Primary" @onclick=Submit>Submit</Button>
@code {
someData mydata=new();
Validations valref1;
async Task Submit()
{
valref1.ClearAll();
await valref1.ValidateAll();
}
class someData : IValidatableObject
{
[Required]
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Status { get; set; } = "New";
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Status == "Completed")
{
if (string.IsNullOrEmpty(Text2))
{
yield return new ValidationResult("Text2 is required", new[] { nameof(Text2) });
}
}
}
}
}
I already realized that custom validators is possible as well, but then I have to rewrite the validators which I find not a great solution.
Thanks for the solution.
Originally posted by @blenke in #5705 (comment)
The text was updated successfully, but these errors were encountered: