Editor validation

Hi,

In the below code, i wanted to make validation works based on the FirstName property of 

EmployeeName. I tried the [Required] attribute as in the code but it is not working. How can I accomplish this?.


Thanks in advance


@using Syncfusion.Blazor.Grids

@using Syncfusion.Blazor.DropDowns

@using System.ComponentModel.DataAnnotations;

@using Syncfusion.Blazor.Inputs


<SfGrid DataSource="@Employees" Height="315" Toolbar="@(new List<string>() { "Add", "Edit" ,"Delete","Update","Cancel" })">

    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>

    <GridColumns>

        <GridColumn Field=@nameof(EmployeeData.CustomerID) HeaderText="CustomerID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>

        <GridColumn Field="Data.EmployeeID" HeaderText="EmployeeID" Width="130">

            <EditTemplate>

                @{

                    <SfNumericTextBox ID="Data___EmployeeID" @bind-Value="@((context as EmployeeData).Data.EmployeeID)"></SfNumericTextBox>


                }

            </EditTemplate>

        </GridColumn>

        <GridColumn Field="Data.FirstName" HeaderText="First Name" Width="150" >

            <EditTemplate >

                @{

                    <SfDropDownList ID="Data___FirstName" @bind-Value="@((context as EmployeeData).Data.FirstName)" TItem="EmployeeData" TValue="string" DataSource="@Employees">

                        <DropDownListFieldSettings Value="Data.FirstName" Text="Data.FirstName"></DropDownListFieldSettings>

                    </SfDropDownList>

                }

            </EditTemplate>

        </GridColumn>

        <GridColumn Field=@nameof(EmployeeData.Title) HeaderText="Title" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>

    </GridColumns>

</SfGrid>


@code {

    public List<EmployeeData> Employees { get; set; }

    protected override void OnInitialized()

    {


        Employees = Enumerable.Range(1, 9).Select(x => new EmployeeData()

            {

                CustomerID = x,

                Data = new EmployeeName()

                {

                    FirstName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[new Random().Next(5)],

                    EmployeeID = 1000 + x

                },

                Title = (new string[] { "Sales Representative", "Vice President, Sales", "Sales Manager",

                                      "Inside Sales Coordinator" })[new Random().Next(4)],

            }).ToList();

    }


    public class EmployeeData

    {

        [Required]

        public int? CustomerID { get; set; }

        public EmployeeName Data { get; set; }

        [Required]

        public string Title { get; set; }

    }


    public class EmployeeName

    {

        [Required]

        public int? EmployeeID { get; set; }

        [Required]

        public string FirstName { get; set; }

    }

}


5 Replies

PS Prathap Senthil Syncfusion Team November 3, 2023 12:52 PM UTC

Hi Muhammad


To validate a complex column based on your requirements, you can utilize the 'ValidateComplexType' data annotation attribute. We have already documented this topic; please refer to the documentation link below for more information.

Reference: https://blazor.syncfusion.com/documentation/datagrid/column-validation#validate-complex-column-using-data-annotation-attribute

Regards,
Prathap S



MU Muhammad replied to Prathap Senthil November 3, 2023 08:49 PM UTC

[ValidateComplexType] attribute not found under the namespace 

System.ComponentModel.DataAnnotations (I am using .net 7)



PS Prathap Senthil Syncfusion Team November 6, 2023 12:25 PM UTC

To include the package "Microsoft.AspNetCore.Components.DataAnnotations.Validation" in your project to meet the requirements for complex type validation, please refer to the following code snippet and sample for your reference.

<Project Sdk="Microsoft.NET.Sdk.Web">

 

  <PropertyGroup>

    <TargetFramework>net7.0</TargetFramework>

    <Nullable>enable</Nullable>

    <ImplicitUsings>enable</ImplicitUsings>

  </PropertyGroup>

 

  <ItemGroup>

    <PackageReference Include="Syncfusion.Blazor" Version="23.1.43" />

         <PackageReference Include="Microsoft.AspNetCore.Components.DataAnnotations.Validation" Version="3.2.0-rc1.20223.4" />

  </ItemGroup>

 

</Project>


Attachment: DataGridComplexValidation_845d8c10.zip


MU Muhammad December 13, 2023 06:30 AM UTC

Thanks for all your reply

I managed to solve my problem after properly using ForeignKyColumn of SfGrid as per the documentation. I found this is the finest solution



PS Prathap Senthil Syncfusion Team December 14, 2023 10:41 AM UTC

Thanks for the update,
We are happy to hear that the issue has been resolved on your end. We are closing the thread now.


Loader.
Up arrow icon