0

I have a Blazor Wasm app that I use with an ASP.Net Core API.
I have one page with a form that I use Blazors built in validation with (data annotations and so on).
However now I have added a new feature and I don't know how to solve the validation any more.
The nu feature is that the user can choose to either choose a value in a dropdown menu (this has been there from start and is Required) or click a button and enter some values. If the user clicks the button and enter the values needed the dropdown is no longer required.
I have no idea on how to solve this conditional validation. I have tried to read MS documentation but they don´t really cover this and other googling has not help either.
Picture of part of form
For other features on the same Form I use EditContext for the EditForm element instead of a model.
If the user clicks the button and fills in the form correctly an otherwise empty Object is instantiated
Do anyone have a solution for this? The model used in the EditForm is this:

public class NewJobDTO
{
    [Required(ErrorMessage = "A hash file must be added")]
    public string HashFileName { get; set; }
    public string? JobName { get; set; }
    public string HashFileContent { get; set; }
    [Required]
    [Range(0, 99999)]
    public int HashType { get; set; }
    // [Required(ErrorMessage = "Please select a Dictionary")]
    // [Range(1, 20, ErrorMessage = "Please select a Dictionary")]
    public int Dictionary { get; set; }
    [Required(ErrorMessage = "Please select a Rule")]
    [Range(1, 2, ErrorMessage = "Please select a Rule")]
    public int Rule { get; set; }
    [Required]
    [EmailAddress]
    [Display(Name = "Email address")]
    public string Owner { get; set; }
    [Required(ErrorMessage = "A public gpg key file must be added")]
    public string KeyFileName { get; set; }
    public string KeyFileContent { get; set; }
}

Parts of the form:

<EditForm method="post" OnValidSubmit="Submit" EditContext="editContext" FormName="AddJob" Enhance>
   <div class="row">
      <div class="form-floating mb-2 col">
         <InputSelect id="dictionary" class="form-select" aria-label="Dictionary select"
                        @bind-Value="newJob!.Dictionary">
            <option value="0">Select dictionary to use</option>
               @if(wordlists.Count != 0)
               {
                  @foreach (var dict in wordlists)
                  {
                     <option value="@dict.Id">@dict.Name (@GetSize(dict.Size))</option>
                   }
                }
         </InputSelect>
         <label for="dictionary">Dictionary*</label>
         @if(newJob.Dictionary != 0)
         {
         <small>@wordlists.Where(w => w.Id == newJob.Dictionary).First().Description</small>
         }
         <ValidationMessage For="() => newJob.Dictionary" class="text-danger" />
      </div>
      <div class="col">
         <button class="btn btn-secondary" type="button" @onclick="AddCewlConfig">Use Cewl</button>
      </div>
   </div>
   <button class="btn btn-primary" type="submit" disabled="@formInvalid">Submit</button>
</EditForm>

When clicking on Use Cewl button this function is called:

private async void AddCewlConfig()
{
   var parameters = new Dictionary<string, Object>();
   parameters.Add("SaveCewl", new EventCallbackFactory().Create<CewlData>(this, async (CewlData result) =>
      {
         await modal.HideAsync();
         if(result != null)
         {
            cewl = result;
         }
      }));
      await modal.ShowAsync<AddCewlData>(title: "Add CeWL data", parameters: parameters);
}
5
  • Can you share the model you're using and the blazor code for the form ? Commented May 16 at 10:08
  • RequiredIf custom validator will help. Commented May 16 at 14:31
  • @YoussefBennourSahli I have edit my post and added Module and Form (only the parts that is needed for the question. Commented May 19 at 7:20
  • @GHDevOps Can you please show how you mean?? Commented May 19 at 8:17
  • @cisci here's an example: learn.microsoft.com/en-us/previous-versions/aspnet/… Commented May 20 at 12:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.