Custom Serverside Validation

Do you have an example of custom serverside validation ?
I followed the instructions at https://www.radzen.com/documentation/customizing-generated-application/#a-idcustom-server-validationcustom-server-validationa however was unable to retain the edit form onscreen to allow correction. The following image should indicate what I tried to date.

Hi @mumfie,

You are right - the instructions are outdated and we will fix them shortly.

Here is what you need to do:

  1. Throw exception when validation fails.
    partial void OnCategoryUpdated(Category item)
    {
         if (item.CategoryName == "Test")
         {
             throw new Exception("Custom error");
         }
    }
    
  2. In order for the form to remain editable update the code which sets the Value of the canEdit property. From !(result.status == 400) to result.status != 400 || result.error
  3. Finally if you want to display the error message update the Notify action. Set Detail to Unable to update Category: ${result.error.message}

Cheers!

Thanks Korchev. That works great. Thanks.

Let me bump this thread up.

I am having a similar problem. I am using an upload component and some business logic to parse the uploaded file. During that process many errors could happen which I want to display to the users. How is the current process of throwing custom errors? And if I throw an exception in the backend, how can I display its message in a notification?

Thanks in regards

Hi @radzenuser1,

Are you using Angular or Blazor? Also are how are you uploading the file?

I am using Angular and I am using the Upload Component

In this case you can check this section from the documentation. It shows how to upload files with a custom method and return JSON from the server. You can include any errors in the JSON response. Then you could use the Notify action with a condition to display some error message e.g.

Thank you for the advice. I followed the steps in your link, but the request does not reach the specified custom method (UploadRechnungen()). I set a breakpoint right into it but it never triggers. Logging output is as follows:

info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2]
Successfully validated the token.
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2]
Successfully validated the token.
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1]
Authorization was successful.
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1]
Authorization was successful.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.1.14-servicing-32113 initialized 'ApplicationIdentityDbContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.1.14-servicing-32113 initialized 'ApplicationIdentityDbContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "MigrationId", "ProductVersion"
FROM "__EFMigrationsHistory"
ORDER BY "MigrationId";
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "MigrationId", "ProductVersion"
FROM "__EFMigrationsHistory"
ORDER BY "MigrationId";
infoinfo: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.
infoinfo: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action RadzenPm.Controllers.ServerMethodsController.UploadRechnungen (server) in 286.5926ms
: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action RadzenPm.Controllers.ServerMethodsController.UploadRechnungen (server) in 286.5926ms
infoinfo: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 315.1863ms 400 application/json; charset=utf-8
: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 315.1863ms 400 application/json; charset=utf-8

The last line from the log actually says it executed the action:

Executed action RadzenPm.Controllers.ServerMethodsController.UploadRechnungen (server) in 286.5926ms

Make sure you have attached to the right process.

You were right, somehow it only did not geht the breakpoint.

Another question: What is the best option to pass another parameter to the backend alongside the file? I would like to pass a date from the calendar to the custom method in the servermethodscontroller. However I am not quite shure what to pass for the parameters in radzen?

You need to create a property and data-bind the Value of the DatePicker to it. Then use that property for the method parameter.