Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor ValidationMessage Failing #12000

Closed
aherrick opened this issue Jul 9, 2019 · 5 comments
Closed

Blazor ValidationMessage Failing #12000

aherrick opened this issue Jul 9, 2019 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@aherrick
Copy link

aherrick commented Jul 9, 2019

I have a setup like below:

<label class="control-label">User</label>
<input class="form-control" @bind="@VM.Connector.ConnectorDetails[0]" />
<ValidationMessage For="@(() => VM.Connector.ConnectorDetails.ElementAt(0))" />

With ValidationMessage I'm getting the following error:

ArgumentException: The provided expression contains a MethodCallExpression2 which is not supported. FieldIdentifier only supports simple member accessors (fields, properties) of an object.

How can I support displaying the error message for the given element in the array?

@analogrelay analogrelay added the area-blazor Includes: Blazor, Razor Components label Jul 9, 2019
@SteveSandersonMS
Copy link
Member

Does the following not work?

<ValidationMessage For="@(() => VM.Connector.ConnectorDetails[0])" />

What's the reason why you needed to use ElementAt?

@aherrick
Copy link
Author

aherrick commented Jul 9, 2019

Hey Steve. Not that I'm seeing. With the array accessor I'm seeing:

ArgumentException: The provided expression contains a InstanceMethodCallExpression1 which is not supported. FieldIdentifier only supports simple member accessors (fields, properties) of an object.

@SteveSandersonMS
Copy link
Member

Oh wait, yeah, it can't work.

The expression you're providing is to populate a FieldIdentifier, which is an (object, propertyName) pair. It's essential that we have a meaningful value for propertyName since that's the key into the edit context dictionary for that field. It would be bad if we allowed the propertyName to be something like 0 (in your example) as that would result in mismatches if items were inserted/deleted from the collection.

So instead what you need to do is use a data structure where you have reference-typed objects with fields/properties. For example, ConnectionDetails could be a List<Connector>, where Connector has a property DetailsText, and then you'd use:

<ValidationMessage For="@(() => VM.Connector.ConnectorDetails[0].DetailsText)" />

This will work because VM.Connector.ConnectorDetails[0] would resolve as an object, and we'd know to use DetailsText as the property name.

@aherrick
Copy link
Author

aherrick commented Jul 9, 2019

Thanks for the response! That's unfortunate as I'm not trying to create an object with a property of 1 just to support this situation. Since it can bind successfully to @VM.Connector.ConnectorDetails[0] I would assume we could link the validation message as well.

@aherrick
Copy link
Author

@SteveSandersonMS Is there anyway I can create a unique FieldIdentifier.FieldName so on the back end when validation occurs I can distinguish between the following:

    <div class="form-group">
        <label class="control-label">@VM.Parent.Children[0].DataKey</label>
        <InputText class="form-control" @bind-value="@VM.Parent.Children[0].DataValue" />
        <ValidationMessage For="@(() => VM.Parent.Children[0].DataValue)" />
    </div>

    <div class="form-group">
        <label class="control-label">@VM.Parent.Children[1].DataKey</label>
        <InputText class="form-control" @bind-value="@VM.Parent.Children[1].DataValue" />
        <ValidationMessage For="@(() => VM.Parent.Children[1].DataValue)" />
    </div>

Both just register as DataValue so I can't programmatically match the field up to the validation message I'm generating (using FluentValidation)

Thank you!

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

3 participants