[Bug] Null reference on validator even when manually setting Validator="new Validator()"
See original GitHub issueI’m getting a null reference on the validator for some strange reason, I have another form that uses the exact same method of validating. The validator for this form and the other are in the exact same place. The other validator works fine, but this one throws a null reference:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at KitchensPlus.Client.Components.Dialogs.AddEditCustomer.get_Validated() in D:\Work\KitchensPlus\Client\Components\Dialogs\AddEditCustomer.razor:line 111
<EditForm Model="@Model" OnValidSubmit="SubmitAsync">
<FluentValidationValidator @ref="@_validator" />
@code {
private FluentValidationValidator _validator;
private bool Validated => _validator.Validate(options => { options.IncludeAllRuleSets(); });
}
I’ve even tried setting the validator manually:
<FluentValidationValidator Validator="new AddEditCustomerCommandValidator()" @ref="@_validator" />
It still results in a null reference. Am I missing something here? It’s driving me crazy.
Screenshots
RegisterRequestValidator works fine, AddEditCustomerCommandValidator isn’t working
Hosting Model (is this issue happening with a certain hosting model?): Blazor WebAssembly
.NET 6.0.7 Latest preview version of Blazored.FluentValidation (tried non preview versions too)
Issue Analytics
- State:
- Created a year ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
FluentValidation throws NullReferenceException when ...
Our validator fails on a complex model and I have no clue why, which model, which rules, which whatever. Object reference not set...
Read more >Angular forms custom validator null return not removing ...
As you are finding out, unless you manually remove the error from the FormControl, it will stick around. Validators will only alter the...
Read more >When validating an item in a workflow action, null ...
Navigate to /sitecore/system/Settings/Validation Rules/Field Types/YourFieldType · Go to the Workflow field under "Validation Rules" and deselect ...
Read more >Package validator
Package validator implements value validations for structs and individual fields based on tags. It has the following unique features: Cross Field and Cross ......
Read more >Using Fluent Validation in ASP.NET Core
Fluent Validation is a free-to-use .NET validation library that helps you make your validations clean, easy to create, and maintain. It even works...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Sharaf-Mansour Thanks for the reply, That makes sense, not sure I like that one works and one doesn’t but it’s good to know the fix is simple. I’ve moved to just initialising the validator manually with my own component while I was waiting so at least everything is working now.
TL;DR;
Do null Checking
and for the razor
@ScottKane I tried your repo and here is the issue I think we need to address. Using @ref will trigger OnAfterRender. So of course the _validator will be null because it is not pre-initialized
You can understand more about how ref work in blazor here. https://blazor-university.com/javascript-interop/calling-javascript-from-dotnet/passing-html-element-references/ and here. https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-6.0#capture-references-to-components So as a workaround we just need to handle the Null with some logic Something more like this
and for the razor
To understand it more do this.
Before you press on the refresh button it will say true. meaning that _validator is indeed null, If you call StateHasChanged the _validator will be initialized and so it will say false… And about why it is working fine with Register.razor file Simply call
@(Validated)
And it will trigger the same error.