I’m struggling with this since a week at least. So the thing is I have a little sandbox to play with blazor. There is a form with a few fields. Basic.
I’m trying to put some validation on that. The input fileds are custom component, applied some bootstrap on them.
In short, this is the input component:
@using System.Linq.Expressions
<div class="input-group mb-3">
<span class="input-group-text" id=@Id>@Label</span>
<InputText @bind-Value=@Value @oninput=OnInput type="text" class="form-control" placeholder=@PlaceHolder aria-label=@Label aria-describedby=@Id />
<ValidationMessage For=@For />
</div>
@code {
[Parameter]
public string Id { get; set; }
[Parameter]
public string Label { get; set; }
[Parameter]
public string PlaceHolder { get; set; }
[Parameter]
public string Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
ValueChanged.InvokeAsync(value);
}
}
}
private string _value;
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
[Parameter]
public Expression<Func<string>> For { get; set; }
[Parameter]
public Func<string> OnFormChange { get; set; }
private async Task OnInput(ChangeEventArgs e)
{
Value = e?.Value.ToString();
await ValueChanged.InvokeAsync(Value);
}
}
and a piece from the page:
...
<div class="container-sm">
<Web.Components.Modal Id="exampleModal" Title="Fegyver bevezetése" OnSave="HandleSave" TriggerValidation="Validate" IsValid=@ModalIsValid>
<EditForm Model=@weaponUnderEdit OnValidSubmit="HandleValidSubmit" OnInvalidSubmit="HandleInvalidSubmit">
<DataAnnotationsValidator />
...
@* <Web.Components.TextInput Id="weight" Label="Súly" PlaceHolder="Súly pl.: 0.3 (Tőr), 2.5 (Csatacsákány)" @[email protected] For="@(() => @weaponUnderEdit.Weight)" /> *@
<Web.Components.TextInput Id="price" Label="Ár" PlaceHolder="Ár pl.: 1a 2e 3r, 1e 50r" @[email protected] For="@(() => @weaponUnderEdit.Price)" />
<ValidationSummary />
</EditForm>
</Web.Components.Modal>
</div>
...
of course, there are data annotation attributes on the model:
public class CommonWeaponPrototype
{
...
[Required, RegularExpression(@"^d+(.d{1,2})?$")]
public string Weight { get; set; }
[Required, RegularExpression(@"^d+(.d{1,2})?$")]
public string Price { get; set; }
}
Now the validation works like that:
everything there…
and if the second input is uncomment… everthing gone; no more validation.
Please help me out, I tried so many ways, feels so dumb with that task…
So far tried to use custom validation, maybe I can do with that, using Regex check and so on… but it is really annoying at this time that I can’t make this work, and also, why custom? it is a really simple form to validate, some regex will it be.