Validation for a dropdown in grid edit template doesn't work

Hi forum,

I have experienced a validation problem. In the attached file you will find the code as well as a print screen reporting the error.

Please note that:

  1. I have declared a custom context, because of "context" name already in use by AuthorizeView tag
  2. I have used naming convention as suggested for the Dropdownlist ID
So, what it is wrong?
Thank you for your support
Paolo

Attachment: Desktop_1bc0118f.zip

10 Replies

NP Naveen Palanivel Syncfusion Team April 26, 2022 03:32 PM UTC

Hi Paolo,


Greetings from Syncfusion.


We are currently checking the reported query at our end and we will update the further details within two business days(April 28, 2022). Until then we appreciate your patience.



Regards,

Naveen Palanivel,



NP Naveen Palanivel Syncfusion Team April 28, 2022 11:59 AM UTC

Hi Paolo,


We have analyzed the reported issue with the provided code and we prepared sample as per your suggestion to Validation for a dropdown in grid edit template.  Unfortunately, we are  unable to reproduce the reported issue at our end and it is working correctly in our sample.

Refer to the below code example of our sample (in the attachments) for your reference

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

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

    <GridColumns>

        <GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" IsPrimaryKey="true"  TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules{ Required= true })"  Width="120"></GridColumn>

        <GridColumn Context="gridContext"  Field="Name.FirstName" HeaderText="First Name"  ValidationRules="@(new ValidationRules{ Required=true})" Width="150">

            <EditTemplate>

                <SfDropDownList ID="Name___FirstName" TItem="Country" TValue="string" @bind-Value="@((gridContext as EmployeeData).Name.FirstName)" DataSource="@Countries">

                    <DropDownListFieldSettings Value="CountryName" Text="CountryName"></DropDownListFieldSettings>

                </SfDropDownList>

            </EditTemplate>

        </GridColumn>

        <GridColumn Field="Name.LastName" HeaderText="Last Name" Width="130"></GridColumn>

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

    </GridColumns>

</SfGrid>


Kindly get back to us if you need further assistance on this or if you are still facing the reported issue.


Regards,

Naveen Palanivel.


Attachment: Validation_cad5ab7d.zip


PA Paolo April 28, 2022 02:59 PM UTC

Hi Naveen,

thank you for providing your code sample.

I think I have understood the issue. The DropDownList validation works fine only if I bind it to the same property of the complex object of the grid's column. In my code this is:

GRID

<GridColumn Context="gridContext" Field="Categoria.CategoriaCasaCompleta" HeaderText="Categoria" TextAlign="TextAlign.Left" ValidationRules="@(new ValidationRules{ Required=true})" Width="150">

DDL

 @bind-Value="@((gridContext as CasaUsciteModel).Categoria.CategoriaCasaCompleta)"

But this is the string representation, not it's id. So as a consequence the

TValue="string" not an int.

But, after debugging, I discovered that the "TValue" passed is it's id value (the field is IDSubCategoria as int), but it is assigned to CategoriaCasaCompleta field, not to the id field. So it's a mess.... because the underlying model class doesn't match with the data passed into.

May you investigate on your side please? Because in my opinion this is absolutely an incorrect behavior.

Thank you

Paolo




RS Renjith Singh Rajendran Syncfusion Team April 29, 2022 09:13 AM UTC

Hi Paolo,


We suggest you to ensure to assign proper typed field to @bind-Value for components inside EditTemplate based on the GridColumn Field’s type. As the underlying GridColumn Field is string, you cannot store an int typed value to string field. And as you have provided column validation the validation will be assigned to CategoriaCasaCompleta field and not to IDSubCategoriaCasa. So the validation will behave based on the CategoriaCasaCompleta value in edit context.


So we suggest you to refer to our previous update and the below reference links and modify your application to overcome the reported problem.

Reference :

https://blazor.syncfusion.com/documentation/datagrid/editing#perform-crud-operation-for-complex-object-using-edittemplate

https://blazor.syncfusion.com/documentation/datagrid/cell-edit-types#custom-editors-using-templatecell-edit-template


If we have misunderstood your requirement or query, then kindly share with us more details of your sceanrio for us to proceed further.


Regards,

Renjith R



PA Paolo April 29, 2022 11:32 AM UTC

Hi,

thank you for your answer. I understand what you mean. Now I fixed most of the problems. But, again, I get problems on validating this field. It seems that if I use in the dropdownlist TValue and bind the "text" field, validation works; but not the insert in DB (the relevant id takes default 0). On the contrary, if I use id field, insert works but not validation: the behavior is the same, asking the required field also if I select a value from the Dropdownlist.

I tried to use Data Annotation, but this doesn't work at all.

Just to fix also this issue, I attach in zip models classes, and index component page. Please note that methods used to get and save data are handled by a service class, and then a Rest client class. Both extremely simple.


Attachment: Syncfusion_42eabb4.zip


RS Renjith Singh Rajendran Syncfusion Team May 2, 2022 01:10 PM UTC

Hi Paolo,


We have prepared a sample by modifying the codes from your shared code to make validation work for the Complex GridColumn Field(Categoria.CategoriaCasaCompleta). Please download and refer the sample attached in this ticket.


Kindly download and refer the attached sample and check this from your side. If you are still facing difficulties then share with us an issue reproducing version of the attached sample along with a video explaining your issue replication scenario for us to proceed further.


Regards,

Renjith R


Attachment: ServerApp_9b569c06.zip


PA Paolo May 2, 2022 02:11 PM UTC

Hi Renjith ,

thank you for sharing your code.

Unfortunately it doesn't fit my requirement. I mean it is not handled the id (primary key) of the complex type. The validation works, because it is managed throw the string description of the complex type. But when I activate the "save" function, i.e. to a DB, your code misses the id relevant to the complex type. But this is a normal relational DB scenario. Here my debug on your code, just adding a line to catch the args.Data passed to the handler:

As you can see, the id takes the default (zero).

If the issue is not clear, let me know and I will provide a video.

Best regards

Paolo



RS Renjith Singh Rajendran Syncfusion Team May 3, 2022 09:31 AM UTC

Hi Paolo,


We checked this scenario with the sample attached from our previous update. We could see that in that sample IDSubCategoriaCasa is not defined/used as primary key column in grid. And we would like to inform you that, if you are not entering value for IDSubCategoriaCasa in edit form, then the default 0 value will only be applied in IDSubCategoriaCasa field.


At these cases, you can modify the values in args.Data inside OnActionBegin event. The modified args.Data in OnActionBegin event will only be added in Grid. So we suggest you to modify the values of args.Data inside OnActionBegin event to update values of Fields which you are not modifying manually in edit form.


Also we have DefaultValue property, which you can use to define default value for a column while inserting a record. Please refer our UG documentation for more details,

https://blazor.syncfusion.com/documentation/datagrid/editing#default-column-values-on-adding-new-record


If we have misunderstood your query or of you are still facing difficulties, kindly get back to us with a detailed issue replication scenario for us to proceed further.


Regards,

Renjith R




PA Paolo May 5, 2022 05:08 PM UTC

Hi Renjith,

it doesn't work. Your code doesn't represent the scenario I have (a common one).

Consider this as a main open point in the grid component, in my opinion.

Just few question to workaround to this problem/scenario:

  • is it possible to open a new page, a sort of modal page, when I want to add/edit a record in a grid? So, not using the EditMode.Dialog in GridEditSettings. In this way I think I should have more control...
  • Or: is it possible, during ActionBeginHandler function, analyzing args.Data values, to rise a validation message in the dialog form?
Consider that the DataAnnotation, in your Grid component, doesn't work for complex type object. The annotation tag you suggested [ValidateComplexType] doesn't work without ObjectGraphDataAnnotationsValidator; and, as far as I know, Syncfusion Blazor Grid doesn't handle this validator object.
I wait for a response
Thank you
Paolo Capirci


RS Renjith Singh Rajendran Syncfusion Team May 6, 2022 11:02 AM UTC

Hi Paolo,


Query : is it possible to open a new page, a sort of modal page, when I want to add/edit a record in a grid? So, not using the EditMode.Dialog in GridEditSettings. In this way I think I should have more control..

We have discussed a similar topic in the below thread. We suggest you to refer the below thread for more details on achieving this requirement.

http://www.syncfusion.com/forums/174494/actions-edit-seettings-redirect-to-page?reply=Sgp7ZP


Please get back to us if you need further assistance.


Regards,

Renjith R


Loader.
Up arrow icon