BlazorStrap icon indicating copy to clipboard operation
BlazorStrap copied to clipboard

Validation with DataAnnotations not firing

Open lgulliver opened this issue 6 years ago • 3 comments

Given I have a form setup like the below:

<BSModal @ref="newEstimationSession" IsCentered="true">
    <BSForm Model="@newEstimationSessionViewModel" OnValidSubmit="@CreateNewEstimationSession"  ValidateOnInit="true" @ref="newEstimationSessionForm">
        <BSModalHeader OnClick="@ToggleNewEstimationSessionModal">Start a new estimation session</BSModalHeader>
        <BSModalBody>

            <DataAnnotationsValidator />
            <ValidationSummary />

            <BSFormGroup>
                <BSLabel For="@newEstimationSessionViewModel.RoomCreator.Name">Your Name</BSLabel>
                <BSInput Id="@newEstimationSessionViewModel.RoomCreator.Name" InputType="InputType.Text" PlaceHolder="Your name" @bind-Value="newEstimationSessionViewModel.RoomCreator.Name" ValidateOnChange="true" />
            </BSFormGroup>

            <BSFormGroup>
                <BSLabel For="@newEstimationSessionViewModel.Room.Name">Room Name</BSLabel>
                <BSInput Id="@newEstimationSessionViewModel.Room.Name" InputType="InputType.Text" PlaceHolder="Room name (e.g. Guardians of the Galaxy's Room)" @bind-Value="newEstimationSessionViewModel.Room.Name" ValidateOnChange="true" />
            </BSFormGroup>

        </BSModalBody>
        <BSModalFooter>
            <BSButton Color="Color.Secondary" @onclick="@ToggleNewEstimationSessionModal">Close</BSButton>
            <BSButton Color="Color.Primary" ButtonType="ButtonType.Submit">Start</BSButton>
        </BSModalFooter>
    </BSForm>
</BSModal>

With the ViewModel:

public class NewEstimationSessionViewModel
    {
        public EstimationRoom Room { get; set; } //Extends class Room
        public RoomUser RoomCreator { get; set; }

        public NewEstimationSessionViewModel()
        {
            Room = new EstimationRoom();
            RoomCreator = new RoomUser();
        }
    }

And the DataAnnotations on the underlying classes

Room:

using Squaddle.Shared.Resources;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Squaddle.Shared.Models
{
    public class Room
    {
        [Display(Name = "Room name")]
        [Required(ErrorMessage = "{0} is a required field.", AllowEmptyStrings = false)]
        public string Name { get; set; }
        public RoomType RoomType { get; set; }
        public string RoomCode { get; set; }
    }
}

RoomUser:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Squaddle.Shared.Models
{
    public class RoomUser
    {
        [Required(ErrorMessage = "{0} is a required field.", AllowEmptyStrings = false)]
        [Display(Name ="Your Name")]
        [MinLength(2, ErrorMessage = "{0} is too short! It must be at least {1} characters long.")]
        public string Name { get; set; }

        public bool IsRoomOwner { get; set; }
        
        [Required]
        [Display(Name = "Room Code")]
        [MinLength(8, ErrorMessage ="{0} entered is invalid.")]
        [MaxLength(8, ErrorMessage = "{0} entered is invalid.")]
        public string RoomCode { get; set; }

    }
}

I would expect that the form is invalid by default, however, on load, even with AllowEmptyStrings = false the form is valid.

image

If I enter the fields, they validate again and are correctly shown as invalid:

image

However, if the submit button <BSButton Color="Color.Primary" ButtonType="ButtonType.Submit">Start</BSButton> is clicked, it believes the form to be valid again and submits anyway, ignoring the validation errors.

Am I doing something wrong here or have I found a genuine issue?

lgulliver avatar Jun 06 '20 14:06 lgulliver

What version of blazorstrap

jbomhold3 avatar Jun 06 '20 14:06 jbomhold3

This is with 1.3.2

lgulliver avatar Jun 06 '20 14:06 lgulliver

Just a slight update to this. I also have a different form with a ViewModel that isn't tied to an entity with a much simpler form i.e. no ValidateOnChange or ValidateOnInit and it works as expected with no submit allowed if the form is invalid.

<BSForm Model="@joinRoomViewModel" OnValidSubmit="@JoinExistingSession">

    <DataAnnotationsValidator />
    <ValidationSummary />

    <BSFormGroup>
        <BSLabel For="@joinRoomViewModel.UserName">Your Name</BSLabel>
        <BSInput Id="@joinRoomViewModel.UserName" InputType="InputType.Text" PlaceHolder="Your name" @bind-Value="joinRoomViewModel.UserName" />
    </BSFormGroup>

    <BSFormGroup>
        <BSLabel For="@joinRoomViewModel.RoomCode">Room Code</BSLabel>
        <BSInput Id="@joinRoomViewModel.RoomCode" InputType="InputType.Text" PlaceHolder="Room code (e.g. r0sE8ud1)" @bind-Value="joinRoomViewModel.RoomCode" />
    </BSFormGroup>

    <BSButton Color="Color.Primary" ButtonType="ButtonType.Submit">Join room</BSButton>

</BSForm>

ViewModel:

public class JoinRoomViewModel
    {
        [Display(Name = "Your name")]
        [Required]
        public string UserName { get; set; }

        [Display(Name = "Room code")]
        [Required]
        public string RoomCode { get; set; }
    }

lgulliver avatar Jun 07 '20 10:06 lgulliver