简体   繁体   English

Blazor 自定义控件验证

[英]Blazor Custom Control Validation

I have created some custom input controls that do not use a input tag to gather user input.我创建了一些不使用输入标签来收集用户输入的自定义输入控件。 For example, a directory locator for Blazor desktop.例如,Blazor 桌面的目录定位器。 I understand how Blazor does normal form validation via the edit form.我了解 Blazor 如何通过编辑表单进行常规表单验证。 But this approach is dependent on you having some type of input tag for the form to validate.但是这种方法取决于您是否有某种类型的输入标签来验证表单。 How can we allow our custom component also get validated in our forms?我们如何才能让我们的自定义组件在我们的表单中也得到验证?

<div class="directoryBrowserContainer">
    <MyButton Text="Select Folder" OnClick="OpenDirectoryBrowser" />
    <div>
        @Value
    </div>
</div>

@code {
    [Inject]
    public IDirectoryBrowser Browser { get; set; }

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }
    
    private string _value;
    public string Value
    {
        get => _value;
        set
        {
            if(_value == value) { return; }

            _value = value;
            ValueChanged.InvokeAsync(value);
        }
    }

    private void OpenDirectoryBrowser()
    {
        Value = Browser.Open();
    }

}

In the above example I would want to validate that the Value has some value when the form is submitted.在上面的示例中,我想在提交表单时验证 Value 是否具有某些值。

Thank you, Travis谢谢你,特拉维斯

Here's a starting point.这是一个起点。 I've removed the Browser stuff and just toggle the string between empty and a value as a demo.我已经删除了浏览器的东西,只是将字符串在空和值之间切换为演示。

This inherits from InputBase<T> which is a template that doesn't actually have an input .这继承自InputBase<T> ,它是一个实际上没有input的模板。

All you do is:你要做的就是:

  1. Wire up setting CurrentValueAsString from whatever code you have.从您拥有的任何代码中连接设置CurrentValueAsString
  2. Override TryParseValueFromString to parse the string value (supplied to CurrentValueAsString ) to the correct type.覆盖TryParseValueFromString以将字符串值(提供给CurrentValueAsString )解析为正确的类型。

I've also shown how to implement the basic validation directly in the control.我还展示了如何直接在控件中实现基本验证。

@using System.Diagnostics.CodeAnalysis
@inherits InputBase<string>

<div class="directoryBrowserContainer">
    <button class="btn btn-primary" @onclick=ChangeValue>Change Value</button>
    <div>
        @CurrentValue
    </div>
</div>

@code {
    string newValue = string.Empty;

    protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out string result, [NotNullWhen(false)] out string validationErrorMessage)
    {
        result = value;
        var ret = value != string.Empty;

        validationErrorMessage = ret
        ? string.Empty
        : "Value can't be empty";

        return ret;
    }

    private void ChangeValue()
    {
        if (this.newValue == string.Empty)
            this.newValue = "Hello";
        else
            this.newValue = string.Empty;

        this.CurrentValueAsString = newValue;
    }
}

For more complex setups you can replace CurrentValueAsString with another property which you set from your code, and mimic what it does - here's the source code - https://github.com/dotnet/aspnetcore/blob/e6dd3946f1f14e4182c70ac532dda10dd0f25f4b/src/Components/Web/src/Forms/InputBase.cs#L87对于更复杂的设置,您可以将CurrentValueAsString替换为您从代码中设置的另一个属性,并模仿它的作用 - 这是源代码 - https://github.com/dotnet/aspnetcore/blob/e6dd3946f1f14e4182c70ac532dda10dd0f25f4b/src/Components/Web /src/Forms/InputBase.cs#L87

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

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