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);
}