Boostrap form validation doesn't work
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with the Blazor Bootstrap number input validation example linked in the issue and compare its behavior with Bootstrap 5.3’s form validation guidance. The issue includes a minimal EditForm example using TextInput, NumberInput, and validation components; reproduce the missing form and field classes and the always green valid message. Done means validation feedback and Bootstrap styling reflect the model’s actual validation state.
由索引模型根据 Issue 内容生成。
描述
I added validation like this
https://docs.blazorbootstrap.com/forms/number-input#validations
Expecting this
https://getbootstrap.com/docs/5.3/forms/validation/
to happen.
But nothing happens. The form elements never get coloured.
The form never gets the needs-validation was-validated classes.
Is this supposed to work?
This is as close as I can get.
There's quite a lot of work to do --- surely this should be out-of-the-box behaviour?
Annoyingly, the form always shows the green valid message. I can't work out why.
@page "/"
@rendermode InteractiveServer
@using BlazorBootstrap
@using System.ComponentModel.DataAnnotations
@using System.Linq.Expressions
@code {
class FormModel
{
[Required]
[MinLength(3, ErrorMessage = "Custom error message: minimum length 3.")]
public string TextValue { get; set; }
[Required]
[Range(8, 21, ErrorMessage = "Must be between 8 and 21; preferably 13.")]
public int IntValue { get; set; }
}
FormModel Model { get; set; }
EditContext? EditContext { get; set; }
ValidationMessageStore? ValidationMessageStore { get; set; }
protected override void OnParametersSet()
{
if (Model == null)
{
Model = new FormModel();
}
EditContext = new EditContext(Model);
//EditContext.OnValidationRequested += OnEditContextValidationRequested;
EditContext.OnValidationStateChanged += OnEditContextValidationStateChanged;
ValidationMessageStore = new ValidationMessageStore(EditContext);
base.OnParametersSet();
}
void OnEditContextValidationStateChanged(object? sender, ValidationStateChangedEventArgs e)
{
// Seems to be impossible to get the actual validation value, so guess...
IsValid = !EditContext.GetValidationMessages().Any();
}
public void Dispose()
{
if (EditContext is not null)
{
// VERY IMPORTANT to unregister event handlers in Blazor!
EditContext.OnValidationStateChanged -= OnEditContextValidationStateChanged;
}
}
async Task OnClickValidSubmit()
{
throw new NotImplementedException();
}
bool? IsValid { get; set; }
string BootstrapFormValidationClasses
{
get
{
if (IsValid.HasValue)
{
return "was-validated " + (IsValid.Value ? "is-invalid" : "is-invalid");
}
return "";
}
}
string GetFormElementValidationClass(Expression<Func<object>> accessor)
{
if (IsValid.HasValue)
{
return EditContext.IsValid(accessor) ? "is-valid" : "is-invalid";
}
return "";
}
}
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<Card>
<CardHeader>This is BlazorBootstrap.Card</CardHeader>
<CardBody>
<EditForm FormName="BlazorBootstrapFormWithValidation" EditContext="EditContext" OnValidSubmit="OnClickValidSubmit" class="@BootstrapFormValidationClasses">
<DataAnnotationsValidator />
<div class="mb-3">
<label class="form-label" for="input-TextValue">Text value</label>
<TextInput @bind-Value="@Model.TextValue" Id="input-TextValue" Class="@GetFormElementValidationClass(() => Model.TextValue)" />
<div id="help-TextValue" class="form-text">Enter the text value.</div>
<div class="valid-feedback">OK</div>
<ValidationMessage For="@(() => Model.TextValue)" class="invalid-feedback" />
</div>
<div class="mb-3">
<label class="form-label" for="input-IntValue">Integer value</label>
<NumberInput @bind-Value="@Model.IntValue" Id="input-IntValue" Class="@GetFormElementValidationClass(() => Model.IntValue)" />
<div id="help-IntValue" class="form-text">Enter the integer value.</div>
<div class="valid-feedback">OK</div>
<ValidationMessage For="@(() => Model.IntValue)" class="invalid-feedback" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</CardBody>
</Card>
- 主要语言
- HTML
- 星标
- 1.2k
- 派生
- 111
- 平均合并
- 4 分钟
- 30 天内合并 PR
- 55
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
vikramlearning/blazorbootstrap 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
vikramlearning/blazorbootstrap#1388 ·
维护者通常 1 天内回复
-
NET 11 Support未关闭
难度 3/5 1-2 天 新手友好度 28/100
vikramlearning/blazorbootstrap#1362 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 25/100
vikramlearning/blazorbootstrap#1346 ·
维护者通常 1 天内回复
-
Showcase: rich sample websites built with Blazor Bootstrap可能已有人在做 @gvreddy04 于 8 天前认领。 未关闭
vikramlearning/blazorbootstrap#1343 · 已指派 1 人 ·
维护者通常 1 天内回复
-
area-tree-view new component
难度 5/5 一周以上 新手友好度 8/100
vikramlearning/blazorbootstrap#1339 ·
维护者通常 1 天内回复
查看 vikramlearning/blazorbootstrap 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
DioxusLabs/blitz#1614 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
boxlite-ai/boxlite#2777 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
quality-assurance
难度 2/5 1-3 小时 新手友好度 65/100
Paul-Austin-Oswego-CSC480-HCI521/gift-app#249 ·
维护者通常 1 天内回复
-
bug P3
难度 1/5 1 小时以内 新手友好度 87/100
维护者通常 1 天内回复