简体   繁体   English

blazor 动态表单添加没有模型类的验证

[英]blazor dynamic forms add validation without model class

im learning blazor and wanted to build some small dynamic form generator i known that there is already something like VxFormGenerator but wanted to learn by myself a bit - at least for simple forms purposes.我正在学习 blazor 并想构建一些小型动态表单生成器,我知道已经有类似 VxFormGenerator 的东西,但想自己学习一点——至少对于简单的表单目的。

so i have it like this:所以我有这样的:

DynamicFormsComponent:动态表单组件:

<EditForm Model = "@Params" OnValidSubmit="OnValidSubmit">

<DataAnnotationsValidator/>

@if(Params != null ) @foreach (var field in Params.FormFields)
{
   <div class="mb-3">
     <label for= "@field.Id">@field.Label :</label>


    @switch (field.Type)
            {
                case FormFieldType.Text:
                {
                        <InputText id="@field.Id"  @bind-Value="@field.StrValue" placeholder="@field.PlaceHolder" class="form-control"></InputText>
                        break;
                }
                case FormFieldType.Number:
                {
                        <InputNumber id="@field.Id" @bind-Value="@field.IntValue" placeholder="@field.PlaceHolder"  class="form-control"> ></InputNumber>
                        break;
                    }
                 case FormFieldType.Date:
                {
                        <InputDate id="@field.Id" @bind-Value="@field.DateValue" placeholder="@field.PlaceHolder" class="form-control"></InputDate>
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
   </div>
}  
<ValidationSummary></ValidationSummary>  
<button type="submit" class="btn btn-primary">@Params?.SendButtonText</button>
public partial class DynamicFormComponent:ComponentBase
{
    [Parameter]
    public  DynamicFormParams Params { get; set; } = new DynamicFormParams();

    [Parameter]
    public EventCallback<DynamicFormParams> OnValidSubmitCallback { get; set; }
    void OnValidSubmit()
    {
        Console.WriteLine("onValidSubmit");
        if (OnValidSubmitCallback.HasDelegate )    OnValidSubmitCallback.InvokeAsync(Params);
        //NavigationManager.navigateto.....
    }
}

public class DynamicFormParams
{
    public List<DynamicFormField> FormFields { get; set; } = new List<DynamicFormField>();
    public string FormTitle { get; set; } = string.Empty;
    public string SendButtonText { get; set; } = "Send";
}

public class DynamicFormField
{
    public string? Label { get; set; }
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string PlaceHolder { get; set; } = string.Empty;
    public FormFieldType? Type { get; set; }
    public string? StrValue { get; set; }
    public int? IntValue { get; set; }
    public DateTime? DateValue { get; set; }

}

public enum FormFieldType
{
    Text,
    Number,
    Date       
}

so the usage would be所以用法是

<DynamicFormComponent Params="@p" OnValidSubmitCallback=@onChildFormSubmit ></DynamicFormComponent>

DynamicFormParams p = new DynamicFormParams()
    {
        FormTitle = "test form Title",
        SendButtonText = "Wyślij",
        FormFields = new List<DynamicFormField>()
            {
                new DynamicFormField()
                {
                     Label="testLabelStr",
                     Id="anyid-notGuId",
                     StrValue="a",
                     PlaceHolder="asdadsad",
                     Type=FormFieldType.Text
                },
                new DynamicFormField()
                {
                     Label="testLabelInt",
                     Type=FormFieldType.Number,
                      PlaceHolder="enter nr"
                },
                new DynamicFormField()
                {
                     Label="testLabelDate",
                     Type=FormFieldType.Date,
                     DateValue=DateTime.Parse("2021-04-01")
                }
            }
    };

private void onChildFormSubmit(DynamicFormParams pp)
{
    Console.WriteLine("from local variable");
    Console.WriteLine(JsonSerializer.Serialize(p));
    Console.WriteLine("from event arg");
    Console.WriteLine(JsonSerializer.Serialize(pp));

}

and the question is:问题是:

how with this approach i can use form validation ?我如何通过这种方法使用表单验证? i have no clasic'model' so probably would need something like add 'list of validators ' to my DynamicFormField class我没有经典的“模型”,所以可能需要将“验证器列表”添加到我的 DynamicFormField 类中

and somehow force DataAnnotationsValidator to use this list ?并以某种方式强制 DataAnnotationsValidator 使用此列表? is it possible withoud clasic 'model' and 'data annotations attributes' on it ?没有经典的“模型”和“数据注释属性”可以吗?

thanks and regards谢谢并恭祝安康

The only way to validate form without a model is to use the Blazorise validation system.在没有模型的情况下验证表单的唯一方法是使用 Blazorise 验证系统。 https://blazorise.com/docs/components/validation . https://blazorise.com/docs/components/validation

PS. PS。 I'm a Blazorise creator.我是 Blazorise 的创造者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM