New answer
You can assign some properties on an InputText, with which you can achieve this. You will still need an EditForm though, for the validation. This updates everytime the user hits enter or leaves the InputText:
@using Microsoft.AspNetCore.Components.Forms
@using System.ComponentModel.DataAnnotations;
<EditForm Model="Model" @ref="Form" >
<DataAnnotationsValidator/>
<InputText Value="@Model.Property"
ValueChanged="ValueChanged"
ValueExpression="() => Model.Property"/>
</EditForm>
@code{
EditForm? Form { get; set; }
YourModel Model { get; set; } = new();
public void ValueChanged(string value) {
Model.Property = value;
if(Form?.EditContext?.Validate()??false)
{
// valid
}else
{
// invalid
}
}
}
old answer
You could create a component for that. To make it easier, you could derive from the base type (like InputText or InputCheckbox).
I made one for InputText:
@using Microsoft.AspNetCore.Components.Forms
@inherits InputText
<input attributes="@AdditionalAttributes"
class="@CssClass"
value="@CurrentValue"
@oninput="EventCallback.Factory.CreateBinder<string?>(this,
value => CurrentValueAsString = value, CurrentValueAsString)" />
This is a simple one which updates the bound value on every change of it's own value. In this way, you do not really need a submit button. It also includes a label for displaying the DisplayName.
Now you can use this and bind any value to it from your parent component, just like any other InputText. For validation, you will still need the EditForm though.
<EditForm Model="inputModel" OnValidSubmit="ValidSubmit">
<BetterInputText @bind-value="TextProperty" />
</EditForm>
@code{
public string TextProperty { get; set; }
}