如何使用特定规则验证 winform C# 中的电子邮件列表
我的担忧如下。 我有这 2 个类:
public class Campaign
{
public string name { get; set; }
public string description { get; set; }
public string campaignFolder { get; set; }
public string campaignAttachment { get; set; }
public List<Contact> from { get; set; }
public string subject { get; set; }
public string mailBody { get; set; }
}
public class Contact
{
public string name { get; set; }
public string mail { get; set; }
}
我有一个 Windows 表单,其中有 3 个姓名和电子邮件关联。 名字、电子邮件夫妇必须填写且电子邮件有效,第二对和第三对可以为空,但如果不是,则它们必须有效(姓名填写且电子邮件有效)。
我怎样才能用 fluidvalidation 做到这一点?
我尝试这样做,但验证器检查每封电子邮件 形式为:
private void btnOK_Click(object sender, EventArgs e)
{
Campaign campagne = new Campaign();
Contact contact = new Contact();
List<Contact> contactFrom = new List<Contact>();
List<Contact> contactCC = new List<Contact>();
campagne.name = tbNameCampaign.Text;
campagne.description = tbDescriptionCampaign.Text;
campagne.campaignFolder = tbWorkFolder.Text;
contactFrom.Add(new Contact { name = tbNameFrom1.Text, mail = tbMailFrom1.Text });
contactFrom.Add(new Contact { name = tbNameFrom2.Text, mail = tbMailFrom2.Text });
contactFrom.Add(new Contact { name = tbNameFrom3.Text, mail = tbMailFrom3.Text });
campagne.from = contactFrom;
// ----- Validation des données saisies
CampaignValidator campaignValidator = new CampaignValidator();
ContactValidator contactValidator = new ContactValidator();
ValidationResult campaignErrors = campaignValidator.Validate(campagne);
ValidationResult contactErrors = contactValidator.Validate(contact);
if (campaignErrors.Errors.Count > 0)
{
foreach (ValidationFailure failure in campaignErrors.Errors)
{
MessageBox.Show(failure.ErrorMessage, "Erreur de saisie");
}
}
else
MessageBox.Show("Saisie validée");
}
对于验证器类:
public class CampaignValidator : AbstractValidator<Campaign>
{
public CampaignValidator()
{
RuleFor(c => c.name)
.Cascade(CascadeMode.Stop)
.NotEmpty().WithMessage("Name is required");
RuleFor(c => c.campaignFolder)
.Cascade(CascadeMode.Stop)
.NotEmpty().WithMessage("Directory is required.")
.Must(BeAValidFolder).WithMessage("Directory doesn't exist");
RuleForEach(x => x.from).SetValidator(new ContactValidator());
}
protected bool BeAValidName(string name)
{
name = name.Replace(" ", "");
name = name.Replace("-", "");
return name.All(Char.IsLetter);
}
protected bool BeAValidFolder(string folder)
{
if (!Directory.Exists(folder))
return false;
else
return true;
}
}
public class ContactValidator : AbstractValidator<Contact>
{
public ContactValidator()
{
RuleFor(x => x.mail)
.EmailAddress().WithMessage("Erreur dans la saisie de l'Email").When(x => x.mail == null);
}
}
感谢您的帮助(并对我糟糕的英语表示歉意)。
Mine concern is the following.
I have these 2 classes :
public class Campaign
{
public string name { get; set; }
public string description { get; set; }
public string campaignFolder { get; set; }
public string campaignAttachment { get; set; }
public List<Contact> from { get; set; }
public string subject { get; set; }
public string mailBody { get; set; }
}
public class Contact
{
public string name { get; set; }
public string mail { get; set; }
}
I have a windows form with 3 names and emails associated.
The first name, email couple must be filled and email valid, the second and third couples can be empty but if not they must be valid (name filled and email valid).
How can I do this with fluentvalidation ?
I try this but the validtor check for each email
In the form :
private void btnOK_Click(object sender, EventArgs e)
{
Campaign campagne = new Campaign();
Contact contact = new Contact();
List<Contact> contactFrom = new List<Contact>();
List<Contact> contactCC = new List<Contact>();
campagne.name = tbNameCampaign.Text;
campagne.description = tbDescriptionCampaign.Text;
campagne.campaignFolder = tbWorkFolder.Text;
contactFrom.Add(new Contact { name = tbNameFrom1.Text, mail = tbMailFrom1.Text });
contactFrom.Add(new Contact { name = tbNameFrom2.Text, mail = tbMailFrom2.Text });
contactFrom.Add(new Contact { name = tbNameFrom3.Text, mail = tbMailFrom3.Text });
campagne.from = contactFrom;
// ----- Validation des données saisies
CampaignValidator campaignValidator = new CampaignValidator();
ContactValidator contactValidator = new ContactValidator();
ValidationResult campaignErrors = campaignValidator.Validate(campagne);
ValidationResult contactErrors = contactValidator.Validate(contact);
if (campaignErrors.Errors.Count > 0)
{
foreach (ValidationFailure failure in campaignErrors.Errors)
{
MessageBox.Show(failure.ErrorMessage, "Erreur de saisie");
}
}
else
MessageBox.Show("Saisie validée");
}
For the validator class :
public class CampaignValidator : AbstractValidator<Campaign>
{
public CampaignValidator()
{
RuleFor(c => c.name)
.Cascade(CascadeMode.Stop)
.NotEmpty().WithMessage("Name is required");
RuleFor(c => c.campaignFolder)
.Cascade(CascadeMode.Stop)
.NotEmpty().WithMessage("Directory is required.")
.Must(BeAValidFolder).WithMessage("Directory doesn't exist");
RuleForEach(x => x.from).SetValidator(new ContactValidator());
}
protected bool BeAValidName(string name)
{
name = name.Replace(" ", "");
name = name.Replace("-", "");
return name.All(Char.IsLetter);
}
protected bool BeAValidFolder(string folder)
{
if (!Directory.Exists(folder))
return false;
else
return true;
}
}
public class ContactValidator : AbstractValidator<Contact>
{
public ContactValidator()
{
RuleFor(x => x.mail)
.EmailAddress().WithMessage("Erreur dans la saisie de l'Email").When(x => x.mail == null);
}
}
Thanks for your help (and sorry for my poor english).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为所有联系人分配相同的验证器:
同时您希望为某些联系人执行不同的验证逻辑。您可以将联系人验证器分为两个,第一个验证器更严格,要求电子邮件和姓名不为空且有效:
第二个验证器对所有后续联系人更加宽容,允许空值,但前提是两个字段都为空或 null:
最后,活动验证器按照您需要的顺序分配联系人验证器。请注意,我留给您添加一些针对联系人数量的验证检查。如果营销活动中没有足够的联系人,请考虑是否要返回错误。
You are assigning the same validator for all the contacts:
While you want different validation logic executed for some contacts. You can split contact validators into two, the first one being more strict and requiring both email and name to be not empty and valid:
and second validator more forgiving for all the subsequent contacts, allowing empty values but only if both of the fields are empty or null:
Finally, the campaign validator that assigns contact validators in order you need. Note that I left for you to add some validation check against the amount of contacts. Consider if you want to return an error if there are not enough contacts present in campaign.