Aspnetcore: Required parameters to blazor components

Created on 2 Jul 2019  路  24Comments  路  Source: dotnet/aspnetcore

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

I'm building a component where optional parameter doesn't make sense.

Describe the solution you'd like

Add required parameter to Parameter attribute

[Parameter(required = true)]
protected string Name { get; set; }
Components Big Rock affected-most area-blazor enhancement severity-minor

Most helpful comment

Reopening this. We do plan to do a feature like this but it won't happen in 3.0.

All 24 comments

This is an old idea. It was moved from original Blazor repository to this #5455. Unfortunately it is now closed.

Thank you @Andrzej-W. I close it as duplicate.

@montyclt Maybe you shouldn't close. As I wrote above issue #5455 was closed a few months ago but I'm afraid [Parameter(required = true)] hasn't been implemented yet and I cannot find any other tracking item for this.

Reopening this. We do plan to do a feature like this but it won't happen in 3.0.

The implementation of this may need to mix with the #nullable variable feature in c# 8. The null checks will flag [Parameter(required = true)] protected T myVar {get; set;} as uninitialized, but in fact this attribute is saying that the variable must be initialized; but it is initialized elsewhere.

Clearing the milestone as I'd like to consider this for 5.0 now that we have a bunch of components that have required parameters.

This could have came in handy for me too, for now I just wrapped the property.

        [Parameter]
        public string MessageText
        {
            get => _MessageText ?? throw new NotImplementedException();
            set => _MessageText = value;
        }

        private string _MessageText { get; set; }

Is not possible to add this to 3.1?
now we to set #nullable disable in each params section to disable nullable warnings.

now we to set #nullable disable in each params section to disable nullable warnings.
@kattunga

Use null! instead, much more nice:

    [Parameter]
    public UserEditViewModel UserEdit { get; set; } = null!;

Use null! instead, much more nice:

That works, but it really just tells the compiler to ignore the problem.

It'd be nicer if we can have the compiler surface the problem on the component _consumer_'s end:

<MyComponent /> <!-- compiler warning: UserEdit isn't set -->
<MyComponent UserEdit="@foo" /> <!-- no warning! -->

It would be nice if there is a possibility to tell the components, that some arguments are mandatory to function like in HTML.
For example it is from an html view totally ok to not specify an alt-tag, but an img-tag without src would not work.

It would be nice if there is a possibility to tell the components, that some arguments are mandatory to function like in HTML.
For example it is from an html view totally ok to not specify an alt-tag, but an img-tag without src would not work.

@MarkusRodler The idea of required parameters is that should throw an error when building the project if some component is called without its required parameter.

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

Moving this out of 5.0 given the cost.

Splitting this work in to two parts. For 5.0, we'll introduce the property on ParameterAttribute. This should allow us to scavenge this metadata at a later point when tooling can support it. Tooling work tracked via https://github.com/dotnet/aspnetcore/issues/25055.

The implementation in https://github.com/dotnet/aspnetcore/pull/25054 would have implemented a Required property on the ComponentParameter. The implementation verified that a value for each required component parameter was specified as part of SetParametersAsync. When we discussed this implementation, a few issues came up:

  • The runtime check could not be guaranteed in the event SetParametersAsync is overridden. We may recommend overriding this method for certain kinds of components, which makes the benefit of the runtime check a bit lax.

  • The runtime check does not guarantee non-nullness of reference types. Often components may need to indicate that a parameter is specified and not null.

Given these limitations, it seems the benefit of the runtime check is questionable. Instead, we think could focus on the tooling aspects of this. Tooling can look for an attributed on parameters that indicates if a parameter is required. We can look up such attributes by name, so it would not require a runtime feature and we could enable it any point before 6.0:

[Parameter]
[UIRequired] // Name is TBD
public string MyRequiredParameter { get; set; }

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

I believe the purpose of the request is for some kind of analyzer to output an error if we don't set a required parameter's value when consuming the component in a razor file.

Am I correct @montyclt ?

FYI: https://github.com/excubo-ag/Generators.Blazor/ has experimental support for this kind of feature.

The way we interpreted this was to have a design time gesture that indicated that parameters that were marked as required were not specified. There's a risk there are false positives (particularly with splatting) that could not be statically inferred so we think treating it as a nudge-in-the-right-direction sort of feature is less bothersome.

I believe the purpose of the request is for some kind of analyzer to output an error if we don't set a required parameter's value when consuming the component in a razor file.

Am I correct @montyclt ?

Yes, but if it is possible, I would like to analyze OpenComponent method of BuildRenderTree

Yes, but if it is possible, I would like to analyze OpenComponent method of BuildRenderTree

This is exactly what the experimental analyzer in https://github.com/excubo-ag/Generators.Blazor/ does. See https://github.com/excubo-ag/Generators.Blazor/blob/main/Blazor/RequiredParameterAnalyzer.cs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

farhadibehnam picture farhadibehnam  路  3Comments

markrendle picture markrendle  路  3Comments

rbanks54 picture rbanks54  路  3Comments

UweKeim picture UweKeim  路  3Comments

Pixel-Lord picture Pixel-Lord  路  3Comments