Validation Message for DateTime field is showing my column name

Viewed 705

enter image description here

This is a required field and I have the data annotation

[Required(ErrorMessage = "The Job Start Date field is Required")]

How do I display a custom error message for:

<ValidationMessage For="@(() => request.StartDate)"></ValidationMessage>

If it is not a valid date at the time?

2 Answers

You can refer to this example:

Schedule class:

public class Schedule 
    {
        [Required(ErrorMessage = "The Job Start Date field is Required")]
        public string StartDate { get; set; }

    }

Page:

@page "/test"
@using My_BlazorApp.Data

<h1>ValidateInput</h1>

<EditForm Model="@Model" >

    <DataAnnotationsValidator />

    <div class="form-group">
        <label for="StartDate">Job Start Date: </label>
        <InputDate Id="StartDate" Class="form-control" @bind-Value="@Model.StartDate"></InputDate>
        <ValidationMessage For="@(() => Model.StartDate)" />
    </div>

    <button type="submit">Ok</button>

</EditForm>

@code
{
    private Schedule Model = new Schedule();
}

Here is the result:

enter image description here

You can see my below sample code, error message getting display properly.

    @page "/counter"
@using System.ComponentModel.DataAnnotations;

<div class="form-horizontal container">
    <EditForm Model="@advertisementDto" OnValidSubmit="@OnSubmit">
        <DataAnnotationsValidator />
        <div class="form-group required">
            <label for="Title" class="control-label col-md-2">Title: </label>
            <div class="col-md-4">
                <InputText id="Title" @bind-Value="advertisementDto.Title" class="form-control" placeholder="Title" />

            </div>
            <div class="col-md-6">
                <ValidationMessage For="@(() => advertisementDto.Title)"></ValidationMessage>
            </div>
        </div>
        <div class="form-group required">
            <label for="Date" class="control-label col-md-2">Date: </label>
            <div class="col-md-4">
                <InputText id="Description" @bind-Value="advertisementDto.Date" placeholder="mm/dd/yyyy" class="form-control" />
            </div>
            <div class="col-md-6">
                <ValidationMessage For="@(() => advertisementDto.Date)"></ValidationMessage>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-4 col-md-offset-2">
                <input id="btnCreate" type="submit" value="Submit" class="btn-default" />
            </div>
        </div>
    </EditForm>
</div>
@code{
    public class AdvertisementDto
    {
        [Required(ErrorMessage = "Title is required here")]
        public string Title { get; set; }
        [Required(ErrorMessage = "Date is required here")]
        public string Date { get; set; }
    }
    private AdvertisementDto advertisementDto = new AdvertisementDto();

    public async Task OnSubmit()
    {


    }
    protected override async Task OnInitializedAsync()
    {

    }
    }

Result

enter image description here

Related