-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation before insert or update #94
Comments
I can add these events to the public event Func<T, Task> BeforeInsert;
public event Func<T, Task> BeforeUpdate;
public event Func<T, Task> BeforeDelete; So you should be able to register funcions like this one: private async Task BeforeInsert(T item)
{
....
await Task.CompletedTask;
} You will get the item to be validated as parameter and return a Task. Is that ok for you? Do you need any other parameter? |
The current used events are: https://github.com/gustavnavar/Grid.Blazor/blob/master/docs/blazor_server/Events.md |
Perfect! Thanks very much! Greate job! |
GridBlazor 1.3.20 and GridMvcCore 2.11.17 support these events: public event Func<object, T, Task> BeforeInsert;
public event Func<object, T, Task> BeforeUpdate
public event Func<object, T, Task> BeforeDelete; You can use them as follows: private async Task BeforeInsert(object sender, T item)
{
....
await Task.CompletedTask;
} The first pareameter is the |
I am trying to write validation code like this: Task IGridRepository<Tdto>.BeforeInsert(object sender, Tdto item)
{
var valid = Validator.Validate(item);
if (!valid.IsValid)
{
throw new ValidationException(valid.ToString());
}
return Task.CompletedTask;
} If validation fail, I read errors into console and a generic message error into web page. |
Whats your goal? To write the error in a log? Or to show an error message to the user on the screen? |
show an error message to the user on the screen like Blazor default validation form or MVC default validation form. |
Understood. GridBlazor is doing that using annotations on the model class (see Range annotation here https://github.com/gustavnavar/Grid.Blazor/blob/master/GridBlazorServerSide/Models/OrderDetail.cs). I will add a mechanism to allow you to access the error field on the CRUD forms that GridBlazor currently uses. I will tell you as soon as it will be implemented. |
Thanks. |
GridBlazor 1.3.21 support these events for CRUD: public event Func<GridCreateComponent<T>, T, Task<bool>> BeforeInsert;
public event Func<GridUpdateComponent<T>, T, Task<bool>> BeforeUpdate;
public event Func<GridDeleteComponent<T>, T, Task<bool>> BeforeDelete; This page in the demo implements a validation for the The used validator in the demo is: https://github.com/gustavnavar/Grid.Blazor/blob/master/GridBlazorServerSide/Models/OrderValidator.cs |
Thank you. |
@gustavnavar this is working well but there are some limitations with respect to the FluentValidation{8.6.2} component:
I used a FluentValidation minimal sample, the output is here: |
Version 1.6.6 implements form validation at field level: There is a new property of the This is an example for the insert validation event: @code {
private async Task<bool> BeforeInsert(GridCreateComponent<Order> component, Order item)
{
var orderValidator = new OrderValidator();
var valid = await orderValidator.ValidateAsync(item);
if (!valid.IsValid)
{
component.Error = "Insert operation returned one or more errors";
foreach (var error in valid.Errors)
{
component.ColumnErrors.AddParameter(error.PropertyName, error.ErrorMessage);
}
}
return valid.IsValid;
}
} This sample includes validation at field level for insert and update forms: |
very nice! thank you |
I would like to integrate FluentValidation with Grid.Blazor.
I would like to register OnInsertEvent and OnUpdateEvent when I use Crud feature.
The text was updated successfully, but these errors were encountered: