BlazorStrap icon indicating copy to clipboard operation
BlazorStrap copied to clipboard

Validation on BSForm object with DataAnnotation being ignored

Open georgeemr opened this issue 3 years ago • 1 comments

I have for example the class:

public class Person 
{
    [Required]
    public string Name {get;set;}

   private MoreInfo moreInfo;
   public MoreInfo MoreInfo 
        {
            get
            {
                if (moreInfo == null)
                {
                    moreInfo = new MoreInfo();
                }
                return moreInfo;
            }
            set
            {
                filiacionModel = value;
            }
        }
}

public class MoreInfo
{
   [Required]
   public string Country {get;set;}
}


@code{
    Person Model = new Person();
}


<BSForm @ref="formAdd" Model="@Model" OnValidSubmit="@Add">

<BSFormGroup Class="mb-3">
      <label>Name </label>
      <BSInput InputType="InputType.Text" @bind-Value="@Model.Name"></BSInput>
      <BSFormFeedback For="@(() => Model.Name)"></BSFormFeedback>
  </BSFormGroup>

  <BSFormGroup Class="mb-3">
      <label>Country</label>
      <BSInput InputType="InputType.Text" @bind-Value="@Model.MoreInfo.Country"></BSInput>
      <BSFormFeedback For="@(() => Model.MoreInfo.Country)"></BSFormFeedback>
  </BSFormGroup>

</BSForm>

What is happening is that the validation on the subclass is not firing, is this normal? Only the Name, in this case, jumps as required, if I write a value in Name validation pass. But the intention is that Country has to be required.

Thanks in advance

georgeemr avatar Mar 04 '21 21:03 georgeemr

You need <DataAnnotationsValidator /> inside BSForm if your using Data Annotations to validate

jbomhold3 avatar Mar 07 '21 05:03 jbomhold3