Tetsuo
Tetsuo

Reputation: 25

Blazor Razor Validation Message don't display from component library

I have created a library of blazor components to be able to call the components from the app but the message validation doesn't show. At the moment, the validation is done in a InputText (it validates the format or the length of the Input) but the message or the style of the component is not shown.

The code of the component library:

CustomInputText

<input value="@Value" @oninput="OnValueChanged" placeholder=@placeholderText class="form-control i-component o-my-4" />
<ValidationMessage For="@(() => model)" />

@code {
    [Parameter]
    public string placeholderText { get; set; }

    [Parameter]
    public object model { get; set; }

    [Parameter]
    public string Value { get; set; }

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    private Task OnValueChanged(ChangeEventArgs e)
    {
        Value = e.Value.ToString();

        return ValueChanged.InvokeAsync(Value);
    }
}

I import the component from a nuget package to be able to use it in the App

The App code:

<CustomInputText placeholderText="Place Holder Test" model="filterPayroll.IPF" @bind-Value="filterPayroll.IPF"/>

When I put the ValidationMessage directly in the app it works correctly, but not in the library. For the two cases, the validation linked to the "filterPayroll" class is done correctly, the difference is that in one the message is displayed and the other does not.

Upvotes: 0

Views: 3813

Answers (1)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30016

You need to pass the For for the Validation Summary as an expression.

CustomInputText needs to look like this:

<input value="@Value" @oninput="OnValueChanged" placeholder=@placeholderText class="form-control i-component o-my-4" />
<ValidationMessage For="@For" />

@code {
    [Parameter]
    public string placeholderText { get; set; }

    [Parameter] public Expression<Func<string>>? For { get; set; }

    [Parameter]
    public string Value { get; set; }

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    private Task OnValueChanged(ChangeEventArgs e)
    {
        Value = e.Value.ToString();

        return ValueChanged.InvokeAsync(Value);
    }
}

And your markup:

<CustomInputText @bind-Value="filterPayRoll.IDF" For="() => filterPayRoll.IDF" />

Upvotes: 4

Related Questions