Skip to content

Commit a778be5

Browse files
committed
Updates
1 parent 8f3a0b2 commit a778be5

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

aspnetcore/diagnostics/bl0008.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,37 @@ uid: diagnostics/bl0008
1717

1818
## Cause
1919

20-
A property has the [`[SupplyParameterFromForm]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute) and is initialized with a property initializer.
20+
A property has the [`[SupplyParameterFromForm]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute) and is initialized with a non-default property initializer, where the initializer can be overwritten with `null` during form submission, causing the `EditForm` to fail with the following exception:
21+
22+
> :::no-loc text="InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.":::
2123
2224
## Rule description
2325

24-
The property can be overwritten with `null` when the component receives parameters, causing it to lose its initial value.
26+
The analyzer warns developers when this pattern is detected, while safely ignoring default value initializers (`null`, `null!`, `default`, `default!`) that don't cause the same issue.
27+
28+
Consider the following `Input` form model with a property initializer:
29+
30+
```csharp
31+
public class MyComponent : ComponentBase
32+
{
33+
[SupplyParameterFromForm]
34+
public InputModel Input { get; set; } = new InputModel();
35+
}
36+
```
37+
38+
The analyzer reports the following warning:
39+
40+
> :::no-loc text="Property 'MyComponent.Input' has [SupplyParameterFromForm] and a property initializer. This can be overwritten with null during form posts.":::
41+
42+
<span aria-hidden="true">✔️</span> Safe patterns that are ignored by analyzer:
43+
44+
```csharp
45+
[SupplyParameterFromForm]
46+
public InputModel Input { get; set; } = default!;
47+
48+
[SupplyParameterFromForm]
49+
public InputModel Input { get; set; } = null!;
50+
```
2551

2652
## How to fix violations
2753

0 commit comments

Comments
 (0)