2

I am having trouble getting form validation to work properly in a Blazor WASM client application.

When an InputText element is encapsulated in a Blazor component, the InputText no longer performs validation.

When using a model like

public class Customer {
    [Required]
    [StringLength(100)]
    public string customerName {get; set;} = "";
}

in the form of

<EditForm Model=@customer>

<DataAnnotationsValidator />
<ValidationSummary />

<div class="form-row">

    <div class="form-group mb-0 col-sm-12">

        <div class="input-group input-group-sm mb-1 mt-1">
            <div class="input-group-prepend">
                <span class="input-group-text" style="width:6em;">Firma</span>
            </div>
            <InputText type="text" class="form-control" @bind-Value=customer.customerName />
        </div>

    </div>
</EditForm>

the validation works fine!

But when creating a modular Blazor component

@page "/inputGroup"

    <div class="input-group input-group-sm mb-1 mt-1">
        <div class="input-group-prepend">
            <span class="input-group-text" style="width:6em;">@label</span>
        </div>
        <InputText type=@type class="form-control" @bind-Value=@data @oninput=@onChange />
    </div>

@code {

    [Parameter]
    public string label {get; set;} = "Label:";

    [Parameter]
    public string type {get; set;} = "text";

    [Parameter]
    public string data {get; set;} = "";

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

    private Task onChange(ChangeEventArgs e) {

        data = (string)e.Value;
        return dataChanged.InvokeAsync(data);
    }
}

and then using it in this form

...
<div class="form-row">
    <div class="form-group mb-0 col-sm-12">
        <InputGroup label="Firma:" @bind-data=customer.customerName />
    </div>
</div>
...

the validation does not work!?

2
  • Could you update your pasted code to show where the EditForm is? Also, why does your embeddedable component have a @page directive? Do you intend to allow the user to navigate to /inputGroup? Commented Jun 11, 2020 at 10:58
  • @bind-data=customer.customerName is that a typo ? Commented Jun 11, 2020 at 12:01

1 Answer 1

0

You could do something like this where you subclass InputText

InputTextGroup.razor

@inherits Microsoft.AspNetCore.Components.Forms.InputText

<div class="mt-1">
       <input type="text" id="@Id"
           @attributes="@AdditionalAttributes" @bind="@CurrentValueAsString"
           class="@CssClass myOtherCssClasses" />
</div>

@if (ValidationFor != null)
{
    <div class="myValidationClass">
        <ValidationMessage For="ValidationFor" />
    </div>
}

And InputTextGroup.razor.cs

public partial class InputTextGroup
{
    [Parameter] public string Id { get; set; } = HeadlessUI.HtmlElement.GenerateId();
    [Parameter] public string? Label { get; set; }
    [Parameter] public Expression<Func<string>>? ValidationFor { get; set; }
}

I took inspiration from this SO answer and this Microsoft docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.