diff --git a/docs/Masa.Blazor.Docs/Examples/components/forms/ValidateField.razor b/docs/Masa.Blazor.Docs/Examples/components/forms/ValidateField.razor
new file mode 100644
index 0000000000..daea0feda7
--- /dev/null
+++ b/docs/Masa.Blazor.Docs/Examples/components/forms/ValidateField.razor
@@ -0,0 +1,47 @@
+@using System.ComponentModel.DataAnnotations
+@using Microsoft.AspNetCore.Components.Forms
+
+
+
+
+
+
+
+
+ Send Code
+
+
+
+
+@code {
+
+ private MForm? _form;
+ private Model _model = new();
+
+ class Model
+ {
+ [Required] [EmailAddress] public string? Email { get; set; }
+
+ [Parameter] public string? Code { get; set; }
+ }
+
+ private void SendCode()
+ {
+ var isEmailValid = _form!.Validate(FieldIdentifier.Create(() => _model.Email));
+ if (isEmailValid)
+ {
+ // send code
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/en-US.md b/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/en-US.md
index cef1f0c754..9f5560d59d 100644
--- a/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/en-US.md
+++ b/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/en-US.md
@@ -31,6 +31,12 @@ In addition to validating on each input component via the `Rules` prop, you can
+#### Validate single field {released-on=v1.6.0}
+
+Validate a single field using the `Validate` method of the **MForm** instance.
+
+
+
#### Validation with submit and clear {updated-in=v1.6.0}
You can use the methods provided by `Context` in the content of **MForm**, or use the component instance provided by `@ref` outside of **MForm**.
diff --git a/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/zh-CN.md b/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/zh-CN.md
index 0a6b4583a5..8134e71621 100644
--- a/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/zh-CN.md
+++ b/docs/Masa.Blazor.Docs/wwwroot/pages/components/forms/zh-CN.md
@@ -31,6 +31,12 @@ related:
+#### 验证单个字段 {released-on=v1.6.0}
+
+使用**MForm**实例的`Validate`方法验证单个字段。
+
+
+
#### 通过提交和清除进行验证 {updated-in=v1.6.0}
在 **MForm** 的内容里可以使用 `Context` 提供的方法,在 **MForm** 外部可以使用 `@ref` 拿到组件实例提供的方法。
diff --git a/src/Masa.Blazor/Components/Form/MForm.razor.cs b/src/Masa.Blazor/Components/Form/MForm.razor.cs
index 68f8dbb714..4979f782c3 100644
--- a/src/Masa.Blazor/Components/Form/MForm.razor.cs
+++ b/src/Masa.Blazor/Components/Form/MForm.razor.cs
@@ -113,6 +113,10 @@ private async Task HandleOnSubmitAsync(EventArgs args)
}
}
+ ///
+ /// Validate the all fields in the form
+ ///
+ ///
[MasaApiPublicMethod]
public bool Validate()
{
@@ -126,7 +130,7 @@ public bool Validate()
valid = false;
}
}
-
+
if (EditContext != null)
{
var success = EditContext.Validate();
@@ -139,6 +143,44 @@ public bool Validate()
return valid;
}
+ ///
+ /// Validate the specified field in the form
+ ///
+ ///
+ ///
+ [MasaApiParameter]
+ public bool Validate(IValidatable validatable)
+ {
+ var valid = validatable.Validate();
+
+ if (valid && EditContext is not null)
+ {
+ EditContext.NotifyFieldChanged(validatable.ValueIdentifier);
+ valid = valid && !EditContext.GetValidationMessages(validatable.ValueIdentifier).Any();
+ }
+
+ return valid;
+ }
+
+ ///
+ /// Validate the specified field in the form
+ ///
+ ///
+ ///
+ ///
+ [MasaApiParameter]
+ public bool Validate(FieldIdentifier fieldIdentifier)
+ {
+ var index = Validatables.FindIndex(item => item.ValueIdentifier.Equals(fieldIdentifier));
+ if (index == -1)
+ {
+ throw new ArgumentException($"Field {fieldIdentifier.FieldName} not found in form.");
+ }
+
+ var validatable = Validatables[index];
+ return Validate(validatable);
+ }
+
///
/// parse form validation result,if parse faield throw exception
///