asp.net 向动态表单添加客户端验证

cetgtptt  于 2023-03-09  发布在  .NET
关注(0)|答案(1)|浏览(172)

我有一个表单(在一个.Net Core 6 Web应用程序中),其中的输入字段是基于项目数组的内容在代码中创建的。每个项目定义了字段/数据的类型,以及它是否是必填字段。我有生成表单的代码,但我需要添加可选的验证。我需要它在客户端和服务器端工作(要用ModelState.IsValid测试)。
cshtml包含(为简洁起见,删除了一些代码):

@using Microsoft.AspNetCore.Mvc.TagHelpers
@inherits UmbracoViewPage<AdditionalInfoViewModel>

    <fieldset class="panel--shadow">
        <div class="panel theme--white">
        @for (var i = 0; i < Model.AdditionalInfo.Count; i++)
        {
            var item = Model.AdditionalInfo[i];
            var required = item.IsMandatory ? "required" : "";
            var fieldId = item.UdfName.Replace(' ', '-');

            <div class="form-group mb-4 @required">
                <input asp-for="@Model.AdditionalInfo[i].UdfName" type="hidden" />

                @{
                    var fieldName = Model.AdditionalInfo[i].TitleOverride;

                    switch (Model.AdditionalInfo[i].UdfDataType)
                    {
                        case "Text":
                            <label for="@fieldId" class="form-label mb-2">@fieldName</label>
                            <input class="form-control" id="@fieldId" asp-for="@Model.AdditionalInfo[i].UdfValue" type="text"/>
                            break;

                        default:
                            <label for="@fieldId" class="form-label mb-2">@fieldName</label>
                            <input class="form-control d-inline-block w-auto" id="@fieldId" asp-for="@Model.AdditionalInfo[i].UdfValue" type="text"/>
                            break;
                    }

                    if (Model.AdditionalInfo[i].IsMandatory)
                    {
                        <span class="field-validation-valid" data-valmsg-for="@fieldId" data-valmsg-replace="true"></span>
                    }

                }
            </div>

        }
        </div>

        <div class="panel theme--river text-center">
            <button class="btn btn-theme btn-primary js-additionalinfo-continue"
                    type="@Html.If(Model.HasNextStep, "button", "submit")">
                @if (!Model.HasNextStep)
                {
                    <i class="far fa-shopping-basket" aria-hidden="true"></i>
                }
                @Html.If(Model.HasNextStep, "Continue", "Add to basket")
                <i class="fa-regular fa-arrow-right" aria-hidden="true"></i>
            </button>
        </div>
    </fieldset>

视图模型为:

public class AdditionalInfoViewModel
{
    public AdditionalInfoViewModel()
    {
        Notice = string.Empty;
        AdditionalInfo = new List<AdditionalInfoListItem>();
    }

    public string Notice { get; set; }
    public bool HasNextStep { get; set; }

    public List<AdditionalInfoListItem>? AdditionalInfo { get; set; }
}

附加信息列表项定义为:

public class AdditionalInfoListItem
{
    public string UdfName { get; set; }
    public string? TitleOverride { get; set; }
    public bool IsMandatory { get; set; }
    public string? UdfDataType { get; set;}
    public List<ListValueItem>? ListValues { get; set; }
    public string? UdfValue { get; set; }
}

public class ListValueItem
{
    public string ListValueName { get; set; }
    public bool Selected { get; set; }
}

因此,如果“item.isMandatory”为真,我需要将条目输入设为必填项,并启用客户端和服务器端验证;如果为假,我希望将条目输入设为非必填项(即不需要验证)。
因为ViewModel不包含一组特定的字段,所以我不能在ViewModel上使用通常的注解(至少,我假设我不能?)
Web应用程序目前包含“aspnet-validation.js”,但我是否需要一些其他的js来进行客户端验证?

bgtovc5b

bgtovc5b1#

添加属性验证在这个场景中是有效的,但是我使用了<form method="post"></form>标记。
例如:在UdfName上添加一些要求。

public class AdditionalInfoListItem
    {
        
        public string UdfName { get; set; }
        public string? TitleOverride { get; set; }
        public bool IsMandatory { get; set; }
        public string? UdfDataType { get; set; }
        public List<ListValueItem>? ListValues { get; set; }
        [RegularExpression(@"^[A-Z]+[a-zA-Z\s]*$")]
        [Required]
        public string? UdfValue { get; set; }
    }

然后将<form method="post"></form>放入整个字段集,并将asp-validation-for span添加到要验证的输入下,如下所示:

@using Microsoft.AspNetCore.Mvc.TagHelpers
@inherits UmbracoViewPage<AdditionalInfoViewModel>

<fieldset class="panel--shadow">
    <form method="post">
        <div class="panel theme--white">

            @for (var i = 0; i < Model.AdditionalInfo.Count; i++)
            {
                var item = Model.AdditionalInfo[i];
                var required = item.IsMandatory ? "required" : "";
                var fieldId = item.UdfName.Replace(' ', '-');

                <div class="form-group mb-4 @required">
                    <input asp-for="@Model.AdditionalInfo[i].UdfName" type="hidden" />

                    @{
                        var fieldName = Model.AdditionalInfo[i].TitleOverride;

                        switch (Model.AdditionalInfo[i].UdfDataType)
                        {
                            case "Text":
                                <label for="@fieldId" class="form-label mb-2">@fieldName</label>
                                <input class="form-control" id="@fieldId" asp-for="@Model.AdditionalInfo[i].UdfValue" type="text" />
                                <span asp-validation-for="@Model.AdditionalInfo[i].UdfValue" class="text-danger"></span>
                                break;

                            default:
                                <label for="@fieldId" class="form-label mb-2">@fieldName</label>
                                <input class="form-control d-inline-block w-auto" id="@fieldId" asp-for="@Model.AdditionalInfo[i].UdfValue" type="text" />
                                <span asp-validation-for="@Model.AdditionalInfo[i].UdfValue" class="text-danger"></span>
                                break;
                        }

                        if (Model.AdditionalInfo[i].IsMandatory)
                        {
                            <span class="field-validation-valid" data-valmsg-for="@fieldId" data-valmsg-replace="true"></span>
                        }

                    }
                </div>

            }
        </div>

        <div class="panel theme--river text-center">
            <button class="btn btn-theme btn-primary js-additionalinfo-continue"
                    type="@Html.If(Model.HasNextStep, "button", "submit")">
                @if (!Model.HasNextStep)
                {
                    <i class="far fa-shopping-basket" aria-hidden="true"></i>
                }
                @Html.If(Model.HasNextStep, "Continue", "Add to basket")
                <i class="fa-regular fa-arrow-right" aria-hidden="true"></i>
            </button>
        </div>
    </form>
    
</fieldset>

对于此输入,将出现一个验证警报,如下所示:

相关问题