blazor display validation message
EditContext _editContext = new EditContext(_foodTruck);
private async void HandleValidSubmit()
{
var messageStore = new ValidationMessageStore(_editContext);
if (_foodTruck.StartDelivery >= _foodTruck.EndDelivery)
{
messageStore.Add(_editContext.Field("EndDelivery"), "Bad time entered");
_editContext.NotifyValidationStateChanged();
}
if (!_editContext.Validate()) return;
}<ValidationMessage For="@(() => starship.MaximumAccommodation)" />
@using System.Globalization
@typeparam TValue
@inherits InputBase<TValue>
<input @attributes="AdditionalAttributes" type="radio" value="@SelectedValue"
checked="@(SelectedValue.Equals(Value))" @onchange="OnChange" />
@code {
[Parameter]
public TValue SelectedValue { get; set; }
private void OnChange(ChangeEventArgs args)
{
CurrentValueAsString = args.Value.ToString();
}
protected override bool TryParseValueFromString(string value,
out TValue result, out string errorMessage)
{
var success = BindConverter.TryConvertTo<TValue>(
value, CultureInfo.CurrentCulture, out var parsedValue);
if (success)
{
result = parsedValue;
errorMessage = null;
return true;
}
else
{
result = default;
errorMessage = $"{FieldIdentifier.FieldName} field isn't valid.";
return false;
}
}
}
@page "/RadioButtonExample"
@using System.ComponentModel.DataAnnotations
<h1>Radio Button Group Test</h1>
<EditForm Model="model" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
@for (int i = 1; i <= 5; i++)
{
<label>
<InputRadio name="rate" SelectedValue="i" @bind-Value="model.Rating" />
@i
</label>
}
<button type="submit">Submit</button>
</EditForm>
<p>You chose: @model.Rating</p>
@code {
private Model model = new Model();
private void HandleValidSubmit()
{
Console.WriteLine("valid");
}
public class Model
{
[Range(1, 5)]
public int Rating { get; set; }
}
}