Skip to content
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

Closed
matteoventuri7 opened this issue Feb 18, 2020 · 14 comments
Closed

Validation before insert or update #94

matteoventuri7 opened this issue Feb 18, 2020 · 14 comments

Comments

@matteoventuri7
Copy link
Contributor

I would like to integrate FluentValidation with Grid.Blazor.
I would like to register OnInsertEvent and OnUpdateEvent when I use Crud feature.

@gustavnavar
Copy link
Owner

gustavnavar commented Feb 18, 2020

I can add these events to the GridComponent object:

    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?

@gustavnavar
Copy link
Owner

The current used events are: https://github.com/gustavnavar/Grid.Blazor/blob/master/docs/blazor_server/Events.md

@matteoventuri7
Copy link
Contributor Author

Perfect! Thanks very much! Greate job!

@gustavnavar
Copy link
Owner

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 GridComponent object and the second one the item to be validated.

@matteoventuri7
Copy link
Contributor Author

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.
How can i return something more specific? for example a dictionary field/error message.

@gustavnavar
Copy link
Owner

Whats your goal? To write the error in a log? Or to show an error message to the user on the screen?

@matteoventuri7
Copy link
Contributor Author

show an error message to the user on the screen like Blazor default validation form or MVC default validation form.

@gustavnavar
Copy link
Owner

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.

@matteoventuri7
Copy link
Contributor Author

Thanks.

@gustavnavar
Copy link
Owner

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 Freight field: https://github.com/gustavnavar/Grid.Blazor/blob/master/GridBlazorServerSide/Pages/Crud.razor

The used validator in the demo is: https://github.com/gustavnavar/Grid.Blazor/blob/master/GridBlazorServerSide/Models/OrderValidator.cs

@matteoventuri7
Copy link
Contributor Author

Thank you.

@faina09
Copy link
Contributor

faina09 commented May 26, 2020

@gustavnavar this is working well but there are some limitations with respect to the FluentValidation{8.6.2} component:

  • If there are multiple messages, these are displayed on a single line and not one on each line
  • there is no validation message close to the failed field
  • there is no red/green frame around the wrong/correct fields
    I think these are very nice features; it is possible to have them in GridBlazor?
    Thank you

I used a FluentValidation minimal sample, the output is here:
fv

@gustavnavar
Copy link
Owner

gustavnavar commented May 27, 2020

Version 1.6.6 implements form validation at field level:
https://github.com/gustavnavar/Grid.Blazor/blob/master/docs/blazor_client/Events.md#events-and-crud-validation

There is a new property of the GridComponent object for error messages for each form field. It's name is ColumnErrors.

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:
https://gridblazor.azurewebsites.net/crud

@faina09
Copy link
Contributor

faina09 commented May 28, 2020

very nice! thank you

rgentry09 added a commit to rgentry09/ERP-ASP.NET that referenced this issue Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants