4
votes

How do I add a custom validation message in the SaveItem event (prefer not to use data annotations)? It should show up in the "ValidationMessage For=" context.

@using System.ComponentModel.DataAnnotations
@page "/edititem"


<EditForm Model="@model" OnSubmit="@Submit" OnValidSubmit="@SaveItem">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <InputText id="ItemName" @bind-Value="@model.ItemName" />
    <ValidationMessage For="@(() => model.ItemName)" />
    <button type="submit">Submit</button>
</EditForm>

@code {
    ItemModel model = new ItemModel();

    private void Submit()
    {

    }
    private void SaveItem()
    {


    }

    public class ItemModel
    {
        [Required]
        public string ItemName{ get; set; }
    }
}
1
well, you could create your own custom component but for most uses that would surely be overkill. What's your objection to using data annotations? [Required(ErrorMessage ="xxx Is Required")] - App Pack
For this validation, I have to check the database and would prefer to do it in the component event rather than data annotation. - xam86

1 Answers

0
votes

You can add custom validation in server side by add some Component which contain error message. Display validation error from the server