skip to Main Content

I have a Blazor web application which uses System.ComponentModel.DataAnnotations attribute for validation in a registration form.
I have a property named NationalCode which is nullable long
I did not added [Required] attribute above my NationalCode property
But when I want to submit the form if I leave the field NationalCode empty this validation message appears
"The value ” is not valid for ‘NationalCode’."

If I change my code and add [Required] attribute like this :

[Required(ErrorMessage = "Please Enter your national code", AllowEmptyStrings = true)]
public long? NationalCode { get; set; } = null;

Two message appears :

- "The value '' is not valid for 'NationalCode'."
- "Please Enter your national code"

But I want only my message appear.
Where is the first message come from ?
How can I disable it ?

This is my codes :

<div class="form-floating">
    <InputNumber @bind-Value="Input.NationalCode" autocomplete="nationalCode" aria-required="true" />
    <label for="family">NationalCode</label>
    <ValidationMessage For="() => Input.NationalCode" class="text-danger" />
</div>

 private sealed class InputModel
 {
     public long? NationalCode { get; set; }
 }

.NET Version

8.0.3

2

Answers


  1. First you need to clear the statement. Let’s look at "Required(ErrorMessage = "Please Enter your national code", AllowEmptyStrings = true)]". So, is NationalCode required or it can be empty?

    After that, you will be able to program it.

    Login or Signup to reply.
  2. I’ve taken the code you’ve provided and produced this Minimal Reproducible Example.

    It works, so there must be doing something you’re not showing.

    @page "/"
    @using System.ComponentModel.DataAnnotations
    
    <PageTitle>Home</PageTitle>
    
    <h1>Hello, world!</h1>
    
    Welcome to your new app.
    
    <EditForm EditContext="_editContext" OnValidSubmit="this.OnValidSubmit">
        <DataAnnotationsValidator/>
        <div class="mb-3">
            <label class="form-label">National Code</label>
            <InputNumber class="form-control" @bind-Value="_model.NationalCode" autocomplete="nationalCode" aria-required="true" />
            <ValidationMessage For="() => _model.NationalCode" class="text-danger" />
        </div>
        <div class="text-end">
            <button tyoe="submit" class="btn btn-primary">Submit</button>
        </div>
    </EditForm>
    
    <div class="bg-dark text-white m-2 p-2">
        <pre>Value: @(_model.NationalCode?.ToString() ?? "null")</pre>
    </div>
    @code{
        private Model _model = new();
        private EditContext? _editContext;
    
        protected override void OnInitialized()
        {
            _editContext = new(_model);
        }
    
        public Task OnValidSubmit()
        {
            return Task.CompletedTask;    
        }
    
        public class Model {
            public long? NationalCode { get; set; } = null;
        }
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search