question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Null reference on validator even when manually setting Validator="new Validator()"

See original GitHub issue

I’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 Screenshot 2022-07-25 101048 Screenshot 2022-07-25 101020 Screenshot 2022-07-25 101132 Screenshot 2022-07-25 101156 Screenshot 2022-07-25 101217

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:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ScottKanecommented, Aug 2, 2022

@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.

0reactions
Sharaf-Mansourcommented, Aug 2, 2022

TL;DR;

Do null Checking

    private FluentValidationValidator? _validator;
    private bool? Validated => _validator?.Validate(options => { options.IncludeAllRuleSets(); });

and for the razor

 <MudButton OnClick="SubmitAsync" DisableElevation Variant="Variant.Filled"
 Disabled='@(!Validated ?? false)' Color="Color.Secondary">@(Model.Id != 0 ? "Update" : "Save")</MudButton>

@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

    private FluentValidationValidator _validator;

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

    private FluentValidationValidator? _validator;
    private bool? Validated => _validator?.Validate(options => { options.IncludeAllRuleSets(); });

and for the razor

 <MudButton OnClick="SubmitAsync" DisableElevation Variant="Variant.Filled"
 Disabled='@(!Validated ?? false)' Color="Color.Secondary">@(Model.Id != 0 ? "Update" : "Save")</MudButton>

To understand it more do this.

 @(_validator is null)
<button class="btn" @onclick="()=>StateHasChanged()">ref</button>

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found