Skip to content

自定义组件里定义Select组件如何触发验证 #3269

Answered by ArgoZhang
javazfj asked this question in Q&A
Discussion options

You must be logged in to vote

自定义组件 Student 包裹了 Select 组件

@inherits SingleSelectBase<string>

<Select TValue="string" Items="items" @bind-Value="Value"></Select>

@code {
    private List<SelectedItem> items { get; set; }

    protected override void OnInitialized()
    {
        items = items ?? new();
        items.Add(new SelectedItem("", "空"));
        items.Add(new SelectedItem("A", "A"));
        items.Add(new SelectedItem("B", "B"));
    }
}

组件绑定模型字段为 Name 标记了 RequiredStringLength 两个验证标签即自动生成两个验证规则

public class Student
{
    [Required]
    public int Id { get; set; }

    [Required(ErrorMessage = "{0} 不可为空")]
    [Display(Name = "姓名")]
    [StringLength(50, MinimumLength = 1, ErrorMessage = "最多50个字符")]
    …

Replies: 7 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by javazfj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
3 participants
Converted from issue

This discussion was converted from issue #3266 on April 12, 2024 10:36.