locked
Blazor Forms And Validation RRS feed

  • Question

  • User2041008840 posted

    Hi Guys,

    I Applied the EditForms in and DataAnnotationValidation but when i click on Insert button its its inserting value into database 
    When textbox is empty it still inserting empty value. 

    Create.Razor Page

    <EditForm Model="@Department" OnInvalidSubmit="@Invalid" OnValidSubmit="@Insert">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <div class="row">
            <div class="col-md-4">
     
                <div class="form-group">
                    <label asp-for="Name" class="-label">Name</label>
                    <input type="text" class="form-control" @bind="@Department.Name" placeholder="Name" />
                    <span class="text-danger"></span>
                </div>
                <div class="form-group">
                    <a href="/departments" class="btn btn-link">Cancel</a>
                    <button @onclick="Insert" class="btn btn-primary">Create</button>
                </div>
     
            </div>
        </div>
    </EditForm>
    @code {
        string Name;
        public Department Department { get; set; } = new Department();
        protected void Invalid()
        {
            Console.WriteLine("Not Valid");
        }
     
        protected async Task Insert()
        {
            //Department d = new Department()
            //{
            //    Name = Name
            //};
     
     
            await departmentsService.InsertDepartmentsAsync(Department);
     
            NavigationManager.NavigateTo(nameof(Departments));
        }
    }
    

    Model

    Department Model - 
    public
     int ID { get; set; }       [Required]       public string Name { get; set; }
    Wednesday, March 18, 2020 10:19 AM

Answers

  • User415553908 posted

    I believe the issue is with this line:

    <button @onclick="Insert" class="btn btn-primary">Create</button>

    you see, the DataAnnotationsValidator is meant to kick in on form submit:

    <EditForm Model="@Department" OnInvalidSubmit="@Invalid" OnValidSubmit="@Insert"> // you've got two callbacks here - you already handle Insert so there's no need to change it

    but since you are simply handling button onClick, which as far as Blazor is concerned can be anything.

    To fix, change button type to submit:

    <button @type="submit" class="btn btn-primary">Create</button>

    See more details here: https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 20, 2020 5:41 PM

All replies

  • User415553908 posted

    I believe the issue is with this line:

    <button @onclick="Insert" class="btn btn-primary">Create</button>

    you see, the DataAnnotationsValidator is meant to kick in on form submit:

    <EditForm Model="@Department" OnInvalidSubmit="@Invalid" OnValidSubmit="@Insert"> // you've got two callbacks here - you already handle Insert so there's no need to change it

    but since you are simply handling button onClick, which as far as Blazor is concerned can be anything.

    To fix, change button type to submit:

    <button @type="submit" class="btn btn-primary">Create</button>

    See more details here: https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 20, 2020 5:41 PM
  • User2041008840 posted

    OK SIR

    Saturday, March 21, 2020 1:01 PM