System.NullReferenceException: Object reference not set to an instance of an object. at Radzen.Blazor.ValidatorBase.ValidateModel(Object sender, ValidationRequestedEventArgs args)

I am doing dynamic form creation due to dynamic schema retrieved from API.

I am getting a runtime exception for:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Radzen.Blazor.ValidatorBase.ValidateModel(Object sender, ValidationRequestedEventArgs args)
   at Microsoft.AspNetCore.Components.Forms.EditContext.Validate()
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()

When I have this:

   builder.OpenComponent<RadzenRequiredValidator>(0);                
   builder.AddAttribute(1, "Component", fieldKey);                
   builder.AddAttribute(2, "Text", "This field is required");
   builder.AddAttribute(3, "Style", "color: red;");
   builder.AddAttribute(100, "Name", fieldKey + "_reqval"); /// just in case its needed (trying to fix the null ref exception)
   builder.CloseComponent();

I've confirmed that the Component matches the Name property for the fields and looked through your source, but I am wondering if the issue is due to needing to reference the form on the controls.


I have the form setup like:

EditForm Model="@formData" OnValidSubmit="HandleValidSubmit"

And the Fields Being Built like this:

 private void RenderTextBox(RenderTreeBuilder builder, Field field, string fieldKey, object fieldValue)
 {
     builder.OpenComponent<RadzenTextBox>(0);
     SetCommonAttributes(builder, field, fieldKey, fieldValue?.ToString());
     if(field.input_type == "email" || field.input_type == "tel")
     {
         builder.AddAttribute(2, "InputType", field.input_type);
     }
     builder.CloseComponent();
 }

private void SetCommonAttributes<T>(RenderTreeBuilder builder, Field field, string fieldKey, T value)
{
    builder.AddAttribute(1, "Value", value);
    builder.AddAttribute(2, "ValueChanged", EventCallback.Factory.Create(this, (T newValue) => formData[fieldKey] = newValue));
    builder.AddAttribute(100, "Name", fieldKey);
    builder.AddAttribute(101, "Id", fieldKey);
}

Hi @zasage,

Please format your code perf the forum FAQ instructions. Thanks!

Sorry, I've edited my post to have the code formatted (looks much easier to read now, so I can understand why this is important).

Hi @zasage

It may be worth adding the Radzen project to your own solution. This way, you can debug what's going on in the library. May give you a clue as to what the error is.

Regards

Paul

Hey @Paul_Ruston, I love that idea, but what is the best way to do that exactly so I am not getting conflicts with the versions and Nuget?

Hi @zasage

I only have instructions for Visual Studio.

  1. Download Radzen source from -
    radzenhq/radzen-blazor: Radzen Blazor is a set of 70+ free native Blazor UI components packed with DataGrid, Scheduler, Charts and robust theming including Material design and FluentUI. (github.com)

  2. Open your solution in Visual Studio.

  3. Right click Solution Icon in Solution Explorer and Add -> Existing Project.

  4. Add the project in Radzen.Blazor directory downloaded in step one.

  5. Remove Radzen.Blazor nuget package.

  6. Under your own project, right-click Dependencies and add a project reference to the Radzen.Blazor project.

In step one, you could clone the project, which will put it under git control. You can then 'Pull' the latest Radzen.Blazor as updates are made.

Regards

Paul

1 Like

All RadzenValidator components require a RadzenTemplateForm. They won't work inside EditForm. I am positive this is the reason you are getting the exception.

1 Like