C# 如何将{PropertyName}从FluentValidation强制转换为WinForms中的特定控件?
我在我的Windows窗体应用程序中使用C# 如何将{PropertyName}从FluentValidation强制转换为WinForms中的特定控件?,c#,winforms,fluentvalidation,C#,Winforms,Fluentvalidation,我在我的Windows窗体应用程序中使用FluentValidation来验证用户从中输入的内容 我的控制器TextBoxName,TextBoxAddress,TextBoxContactInfo和comboxgender 在我的验证器上,我设置了一个规则for(user=>user.DomainModel…,如果用户输入空字符或无效字符,将提示错误,然后将禁止程序将数据保存到数据库中 RuleFor(user => user.Name) .Cascade(Cas
FluentValidation
来验证用户从中输入的内容
我的控制器TextBoxName
,TextBoxAddress
,TextBoxContactInfo
和comboxgender
在我的验证器上,我设置了一个规则for(user=>user.DomainModel…
,如果用户输入空字符或无效字符,将提示错误,然后将禁止程序将数据保存到数据库中
RuleFor(user => user.Name)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("Provide {PropertyName}")
.Must(BeAValidName).WithMessage("{PropertyName} contains invalid characters")
.Length(2, 50).WithMessage("Length ({TotalLength}) of {PropertyName} is between 2-50
characters");
在我的表单上,当出现错误时,我尝试公开{PropertyName}
的值,但它只返回通用值{System.Windows.Forms.Textbox}
,因此无法将其分配回特定的Textbox或Combobox控件名(即TextBoxName.name)
if(validationResult.IsValid==false)
{
MetroTextBox mtxtb=新的MetroTextBox();
List errorList=新列表();
foreach(validationResult.Errors中的ValidationFailure失败)
{
//我试图将{PropertyName}从FluentValidation分配给可工作控件,但这不起作用
mtxtb.Name=failure.PropertyName;
Control[]ctrl=gboxUser.Controls.Find(mtxtb.Name.Replace(“,”),true);
如果(ctrl.Length!=0)
{
找到的控件=ctrl[0];
//打印暴露的控件值并查看代码是否正常工作
Console.WriteLine(找到.ToString());
}
//在错误列表中添加错误
errorList.Add(failure.ErrorMessage);
//选择控件并修改颜色属性的步骤
mtxtb.SelectAll();
mtxtb.BackColor=Color.MistyRose;
}
//提示空数据和无效数据错误的步骤
如果(errorList.Count>0)
{
StringBuilder sb=新的StringBuilder();
sb.AppendLine(string.Join(Environment.NewLine,errorList));
Show(sb.ToString()+Environment.NewLine,“错误”,
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
我怎样才能做到这一点?我应该在哪里编写代码
我想要实现的是:
如果无法验证
TextBox/Combobox
会提示错误,程序将SetFocus
或将BackColor
更改为发生错误的特定TextBoxName/ComboBoxName Control
。这有点繁琐,但它将验证规则与UI保持分离
- 对于每个需要验证的输入控件,创建关联的“验证”事件
- 从数据源中获取当前记录的对象,并将其放在“预览”对象中
- 将预览对象中的属性设置为发件人发送的值
- 将该对象传递到验证器
- 使用验证结果填充ErrorProvider
private void mtxtb_Validating(object sender, CancelEventArgs e)
{
string error = null;
if (myDataSource.Count > 0)
{
User preview = (User)myDataSource.CurrencyManager.Current;
preview.Name= ((TextBox)sender).Text;
UserValidator validator = new UserValidator();
ValidationResult _validationResults = validator.Validate(preview);
ValidationFailure failure = _validationResults.Errors.Find(x => x.PropertyName == "Name");
if (failure != null)
{
error = failure.ErrorMessage;
e.Cancel = true;
}
}
errorProvider1.SetError((Control)sender, error);
}
private void mtxtb_Validating(object sender, CancelEventArgs e)
{
string error = null;
if (myDataSource.Count > 0)
{
User preview = (User)myDataSource.CurrencyManager.Current;
preview.Name= ((TextBox)sender).Text;
UserValidator validator = new UserValidator();
ValidationResult _validationResults = validator.Validate(preview);
ValidationFailure failure = _validationResults.Errors.Find(x => x.PropertyName == "Name");
if (failure != null)
{
error = failure.ErrorMessage;
e.Cancel = true;
}
}
errorProvider1.SetError((Control)sender, error);
}