0

I am working in Blazor WASM where my Entity Model classes are in the shared folder so the client and the server can pass instances. API controllers are used to interact with the DB Context, and a Repository pattern in the Client interacts with the controllers through a HttpService. I would like to create a custom ValidationAttribute that leverages client side validation of an Indexed(Unique) property of an entity and flags the InputText before a post/put request can be sent. I already have Entity.HasIndex().IsUnique() setup in OnModelCreating, so this logic is forced on the back end, but I would like to have something in the EditForm. The problem is, the Shared project has no access to the Server project (naturally), so it cannot ping the DB to check for violations. Is there a way around this (maybe by creating a validation attritute that can be applied directly to the front end)?

  • You already pointed out the problem: You can't 'know' this client-side. You could make a Check (Validation) API call but that would still be subject to concurrency problems. – H H Aug 29 '21 at 15:08
  • Just post the data and process the error(s). – H H Aug 29 '21 at 15:13
  • For the short term I have had the API catch the DbUpdateException and forward it with BadRequest(). Then my repository throws and error, which is caught by client, which interrupts the flow and displays a message, but it seems extremely inefficient. – Josh Johner Aug 29 '21 at 16:20
  • You can define your own ok/error response. How abstract does your API have to be? I wouldn't handle this with a 4xx code. – H H Aug 29 '21 at 16:33

0 Answers0