Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor EditContext : How to check validation message for nested object

$
0
0

I have an extension method to check if a specific field is valid or not.

        public static bool IsValid(this EditContext editContext, string fieldName)        {            var fieldIdentifier = editContext.Field(fieldName);            editContext.NotifyFieldChanged(fieldIdentifier);            var result = !editContext.GetValidationMessages(fieldIdentifier).Any();            Console.WriteLine($"{fieldName}  : {result}");            return result;        }

It's working fine for non-complex fields but not for complex fields. e.g

My model is

    public class RegisterCompanyRequest    {        [Required(ErrorMessage = "Company Name is required")]        [MaxLength(200, ErrorMessage = "Maximum allowed length for company name is 200 characters")]        public string NameEn { get; set; }        [ValidateComplexType]        public List<CompanyLocation> CompanyLocations { get; set; }        [ValidateComplexType]        public CompanyAdminInfo CompanyAdminInfo { get; set; } = new CompanyAdminInfo();    }    public class CompanyAdminInfo    {        [Required(ErrorMessage = "Full name is required")]        [MaxLength(200, ErrorMessage = "Maximum allowed length for full name is 200 characters")]        public string FullName { get; set; }    }    public class CompanyLocation    {        public int Id { get; set; }        [Required]        [MaxLength(200, ErrorMessage = "Maximum allowed length for location name is 200 characters")]        public string Name { get; set; }    }

and if I pass that method NameEn it returns false if validation fails and true otherwise, but if i pass it CompanyLocations[0].Name or CompanyAdminInfo.FullName it always return true.

How can I achieve the same functionality for nested objects in my Model?

To Reproduce: Copy the below code to new blazor WASM project.also install the following package to validate complex type (Install-Package Microsoft.AspNetCore.Components.DataAnnotations.Validation -Version 3.2.0-rc1.20223.4)

@page "/"@using System.ComponentModel.DataAnnotations<p>"Name" : @company.Name <span>@nameValid</span> </p> <p>"Admin Name" : @company.Admin.Name <span>@adminNameValid</span> </p><EditForm EditContext="editContext"><ObjectGraphDataAnnotationsValidator /><input type="text" @bind-value="@company.Name"/><input type="text" @bind-value="@company.Admin.Name"/><button @onclick="Validate" type="submit">Validate</button></EditForm>@code{    private bool nameValid, adminNameValid;    private Company company = new Company();    EditContext editContext { get; set; }    protected override async Task OnInitializedAsync()    {        editContext = new(company);    }    private async Task Validate()    {        var isValid = IsValid(editContext, "Name");        nameValid = isValid;        isValid = IsValid(editContext, "Admin.Name");        adminNameValid = isValid;    }    public bool IsValid(EditContext editContext, string fieldName)    {        var fieldIdentifier = editContext.Field(fieldName);        editContext.NotifyFieldChanged(fieldIdentifier);        return !editContext.GetValidationMessages(fieldIdentifier).Any();    }    public class Company    {        [Required]        public string Name { get; set; }        [ValidateComplexType] /*(Install-Package Microsoft.AspNetCore.Components.DataAnnotations.Validation -Version 3.2.0-rc1.20223.4)*/        public Admin Admin { get; set; } = new Admin();    }    public class Admin    {        [Required]        public string Name { get; set; }    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>