dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Asp.Net Core Blazor in EditForm InValid model but Valid operation set #20077

Closed Qorxi closed 4 years ago

Qorxi commented 4 years ago

Hello. The problem is that, on the basis of the model given EditForm, there are validation errors in including invalid model internal propositions, but the OnValidSubmit operation is performed regardless of the model being invalid.

The codes in the problem part are as follows. The application was made with ASP.NET Core 3.2 version.

<EditForm Model="@product" OnValidSubmit="@HandleSave">
        <DataAnnotationsValidator />
        <div class="form-group">
            <InputText PlaceHolder="Produkt adı" Class="form-control" Id="productName" @bind-Value="@product.ProductName" />
            <ValidationMessage For="@(() => product.ProductName)" />
        </div>
        <div class="form-group">
            <InputNumber PlaceHolder="Produkt qiyməti" Class="form-control" Id="unitPrice" @bind-Value="@product.UnitPrice" />
            <ValidationMessage For="@(() => product.UnitPrice)" />
        </div>
        <div class="form-group">
            <InputTextArea PlaceHolder="Produkt desktipsiyası" Class="form-control" Id="quantityPerUnit" @bind-Value="@product.QuantityPerUnit" />
            <ValidationMessage For="@(() => product.QuantityPerUnit)" />
        </div>
        <div class="form-group">
            <InputNumber PlaceHolder="Stok ədədi" Class="form-control" Id="unitInStock" @bind-Value="@product.UnitsInStock" />
            <ValidationMessage For="@(() => product.UnitsInStock)" />
        </div>
        <div class="form-group">
            @if (category != null)
            {
                <InputSelectNumber Class="form-control" Id="categorId" @bind-Value="@product.CategoryId">
                    <option value="">Seçin</option>
                    @foreach (var item in category)
                     {
                        <option value="@item.CategoryId">@item.CategoryName</option>
                     }
                </InputSelectNumber>
                <ValidationMessage For="(() => product.CategoryId)" />
            }
        </div>
        <button type="submit" class="btn btn-primary" @onclick="@HandleSave">Yadda saxla</button>
        <button type="submit" class="btn btn-danger" @onclick="@ReturnToProductList">Siyahıya qayıt</button>

    </EditForm>
@code {
    [Parameter]
    public int productId { get; set; }

    private ProductViewModel product { get; set; }

    private List<CategoryListViewModel> category { get; set; }

    private EditForm _editContext;

    protected override async Task OnInitializedAsync()
    {
        product = await _productService.GetProductById(productId);

        category = await _categoryService.GetCategoryListModel();
    }

    private void HandleSave()
    {
        _productService.Save(product);

        ReturnToProductList();

    }

    private void ReturnToProductList()
    {
        navigate.NavigateTo("/ProductList");
    }
}
javiercn commented 4 years ago

@Qorxi thanks for contacting us.

@SteveSandersonMS you are the expert here. Can you take a look?

SteveSandersonMS commented 4 years ago
<button type="submit" class="btn btn-primary" @onclick="@HandleSave">Yadda saxla</button>

This button triggers HandleSave regardless of validation.

Instead, remove the @onclick handler from here, so it is just a <button type="submit"> with no click handler. Your <EditForm> will then receive the "submit" event and trigger either OnValidSubmit on OnInvalidSubmit.

Qorxi commented 4 years ago

@Qorxi thanks for contacting us.

@SteveSandersonMS you are the expert here. Can you take a look? @javiercn You are welcome, I applied due to the problem I faced.

Qorxi commented 4 years ago
<button type="submit" class="btn btn-primary" @onclick="@HandleSave">Yadda saxla</button>

This button triggers HandleSave regardless of validation.

Instead, remove the @onclick handler from here, so it is just a <button type="submit"> with no click handler. Your <EditForm> will then receive the "submit" event and trigger either OnValidSubmit on OnInvalidSubmit.

Thanks, the problem has been resolved but I think there is a problem in this part because any event triggered within the edit form should be checked with this logic because it is within the edirform, otherwise it can cause such incomprehensibility.

ghost commented 4 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.