Skip to content

Allow Blazor components with [Parameter] attributes on properties to be aware of the required modifier. #44974

@benjaminsampica

Description

@benjaminsampica

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Using EditorRequired for specifying whether a Blazor component has a required parameter or not seems redundant now with the C# 11 language feature required which can be put on properties.

Currently, you have to do something like the following in order to tell consumers of your component that it's a required parameter.

public partial class FetchData
{
    [Parameter, EditorRequired] public string City { get; set; } = null!;
}

I think it's confusing and verbose to have both a required attribute and modifier on a singular property that a developer wishes to do the same thing - make the parameter required.

Describe the solution you'd like

Simply put, the required modifier should invoke the analyzer warning.

RZ2012: Component FetchData expects a value for the parameter 'City', but a value may not have been provided.

public partial class FetchData
{
    [Parameter] public required string City { get; set; }
}

Backwards compatibility should be maintained on the existing attribute.

It would be nice to notify developers who move from earlier language versions to newer ones that EditorRequired is deprecated in favor of the required modifier.

Additional context

Capture

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions