Save valid data on inline grid site command button

Hello, 
I am new to blazor, ( and syncfusion) and I am using command button in a inline grid to save my model in database.
I am using the event command click to do so: when the event is fired, it check if it’s the save button that was clicked, and then call the method to save in database. 
The problem is that I want to call the method, only if the data is valid. If it is not valid, it should do nothing
Can you tell me how to do this? And as I said, I am new to all this. So please tell me if there is a better way.
Thank you. 

7 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team March 4, 2021 06:39 AM UTC

Hi Audrey, 
 
Greetings from Syncfusion support. 
 
We have validated your query and we suggest you to use the OnActionBegin event of the grid and check whether the data is valid or not. If it is not valid then you can set args.Cancel as true to cancel the corresponding action. Please refer the below code snippet and the sample for your reference. 
 
 
<SfGrid DataSource="@Orders" AllowPaging="true" Height="315"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
<GridEvents TValue="Order" OnActionBegin="ActionBegin"></GridEvents> 
.. 
</SfGrid> 
 
@code{ 
 
    public void ActionBegin(ActionEventArgs<Order> args) { 
        if (args.Action == "Edit" && args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save) && args.Data.Sum < 10) { 
//For example, here we check the Sum column has valid value or not 
            args.Cancel = true; 
        } 
    } 
} 


Documentation : 

Please get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 



PS psyk March 9, 2021 08:49 PM UTC

Hello,
 sorry for the delay.
The datasource is a IEnumerable of my model User
My model has data annotations (required) included. 
If  your grid can displays error messages when my row is not valid, there must be a simpler way, to check if the submitted row is valid,  no? 





JP Jeevakanth Palaniappan Syncfusion Team March 10, 2021 01:11 PM UTC

Hi Audrey , 
 
We have checked your query but we are quite unclear about your exact requirement. Are you expecting the grid to show validation messages or to show any custom validation messages?. If so we have columns level validation and data annotation support. Please refer the below documentation for your reference. 


If this is not our scenario then kindly share us the exact requirement which will be helpful for us to proceed further. 

Regards, 
Jeevakanth SP. 



PS psyk March 10, 2021 08:34 PM UTC

Hello, 
Again, I am new to all this.
So it is possible that there is something obvious to you I don't see/understand.
I will try to be as clear as possible: ( also, note that english is not my native language)

So I have created a list of user with syncfusion grid (inline) .
I have command columns in this grid. (edit, delete, save cancel).
I have managed to save my model when I click on the save button (with event commandClicked).
It calls a function that saves the data in my database.
Also, with data annotation in my model, when I click on the save button, if  something is not valid, it displays correctly the validation errors.

The problem is: even if the model is not valid, it will always call my function. Because the command button has been clicked.

Your solution will work :  when the actionBegin event is fired, check for validation and cancel if not valid.

But do I have to do it manually? checking every property of my model? (example: if firstname is null then cancel, if login is null, then cancel....)
Since syncfusion grid can display validation errors based on data annotation, I was wondering if there isn't a function or property in the events that could tell me if my model is valid or not.

I hope I made it clearer 
Thank you




JP Jeevakanth Palaniappan Syncfusion Team March 11, 2021 11:20 AM UTC

Hi Audrey , 
 
We suspect that you are calling a method to save the data in the database but if the data (eg: firstname ) is null then with the grid’s data annotation, validation message is shown in the grid. But here the data is saved in the db due to invoking a method to save record to db in the CommandClicked event. 
 
Based on this scenario we suggest you to use the Custom binding feature in which there are method like Read, Insert, Update, Remove. You have to invoke the method to save record to db in the Insert/Update/Remove methods which will be triggered only when the records are valid based on your data annotation attribute. 
 
Please refer the below documentation for your reference. 
 

Regards, 
Jeevakanth SP. 


Marked as answer

PS psyk March 15, 2021 02:26 PM UTC

Hello, 
thank you for the answer, it worked!

one other thing though: I would like to display messages when data is save or the are errors.

Exemple, the username entered already exists in the database. 

Inside the insert method , It calls the method that saves my user in the database.
When there is a problem, this method return an error message.
What is the best approach to display this message on my page?


JP Jeevakanth Palaniappan Syncfusion Team March 16, 2021 10:42 AM UTC

Hi Audrey , 
 
You can achieve this requirement by using DataAnnotation and apply custom validation for the column. Please refer and use as like the below codes to achieve custom validation for the ShipCountry column. 

We have prepared a sample based on this requirement. Please download the sample from the link below, 

 
public static List<Order> Orders { getset; } = new List<Order>(); 
public class Order 
{ 
    public int? OrderID { getset; } 
    public string CustomerID { getset; } 
    public DateTime? OrderDate { getset; } 
    public int OrdQty { getset; } 
    public int PicQty { getset; } 
    [ShipCountryValidation] 
    public string ShipCountry { getset; } 
} 
public static bool ValidFlag;[AttributeUsage(AttributeTargets.Property)]public class ShipCountryValidationAttribute : ValidationAttribute{    protected override ValidationResult IsValid(object value, ValidationContext validationContext)    {        // Here you can perform your custom validation and return true/false value        int index = Orders.FindIndex(x => x.ShipCountry.Equals(value.ToString()));   //Check for availability of email in Orders         if (index>=0) {            ValidFlag = true;        }        else            ValidFlag = false;        return ValidFlag ? new ValidationResult("Email Already Exists", new[] { validationContext.MemberName }) : ValidationResult.Success;    }}
 

Please refer the below documentation also for more information 


Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon