Home > Enterprise >  OnvalidSubmit fires and EditContext.Validate() returns true even though my model is intentionally in
OnvalidSubmit fires and EditContext.Validate() returns true even though my model is intentionally in

Time:11-02

In my Blazor Server App project, I have an EditForm which includes my EditContext and OnValidSubmit method.

<EditForm EditContext="@EditContext" OnValidSubmit="@UpdateProject"> I declare and initialize my EditContext as follows:

private EditContext EditContext;

    protected override async Task OnInitializedAsync()
    {
        EditContext = new(supplementedProjectModel);
    }

and my model:

    SupplementedProjectModel supplementedProjectModel = new();

When I submit my model with my submit button

        <button type="submit" >Spara</button>

it fires the OnValidSubmit event EVEN THOUGH my model is intentionally invalid with empty inputs where my model contains

[Required]

attributes. Why?

My second issue is inside the OnValidSubmit method, the EditContext.Validate() ALWAYS return true. Why is that?

protected async Task UpdateProject()
    {
bool IsValid = EditContext.Validate();
        EditContext = new(supplementedProjectModel);
        bool isreallyvalid = EditContext.Validate();
        if (IsValid)
        {
// UPDATE MODEL
}
}

I have tried with all these annotation validators and CLEARLY see my errors when I run the project and input invalid data.

@*<ObjectGraphDataAnnotationsValidator />*@
    <DataAnnotationsValidator />
    <ValidationSummary />

Within my UpdateProject method, should my EditContext be populated with the current model values or not? I would assume so but could not find anything therefore I test the Validate method before and after initializing it again.

Thanks for reading!

CodePudding user response:

No validation occurs here... For validation to happen you must embed the DataAnnotationsValidator component in your code, something like this:

<EditForm EditContext="@EditContext" OnValidSubmit="@UpdateProject">
    <DataAnnotationsValidator />

You may add a ValidationSummary to display a summary of the errors. You may also add a validation message under each field.

Note: Your code is badly formed...

This:

protected override async Task OnInitializedAsync()
    {
        EditContext = new(supplementedProjectModel);
    }

should be:

protected override void OnInitialized()
        {

            _supplementedProjectModel = new SupplementedProjectModel();
            EditContext = new(_supplementedProjectModel);
        } 

If you use async Task, you should await your method.

CodePudding user response:

it fires the OnValidSubmit event EVEN THOUGH my model is intentionally invalid with empty inputs where my model contains....

That's because it is valid. You may not believe so, but it is.

You haven't shown us the model you are trying to validate, which is somewhat vital in answering the question. Are your fields primitives?

Here's a test page that demos the EditForm and Validation in action using a model where [Required] works. Note:

  • Name? is not valid - it's a string and can be null.
  • Value is an int with a default value of 0 - therefore has a value and is valid.
@page "/"
@using System.ComponentModel.DataAnnotations;

<PageTitle>Index</PageTitle>

<EditForm EditContext=this.editContext OnValidSubmit=this.OnValidSubmit OnInvalidSubmit=this.OnInValidSubmit  >
    <DataAnnotationsValidator/>
    <div >Normal edit fields here</div>
    <div >
        <button  type="submit">Submit</button>
    </div>
    <ValidationSummary />
</EditForm>

<div >
    <div>Name: @model.Name</div>
    <div>Value: @model.Value</div>
</div>

@if (this.message != string.Empty)
{
    <div >
        @message
    </div>
}

@code {
    private MyModelData model = new();
    private EditContext? editContext;
    private string message = string.Empty;


    public Task OnValidSubmit()
    {
        this.message = "Valid Submit";
        return Task.CompletedTask;
    }

    public Task OnInValidSubmit()
    {
        this.message = "InValid Submit";
        return Task.CompletedTask;
    }

    protected override void OnInitialized()
    {
        this.editContext = new EditContext(model);
    }

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

        [Required]
        public int Value { get; set; }
    }
}
  • Related