是否可以将值传递给错误消息.我尝试过这样的事情:
RuleFor(x => x.ForeName).Length(1, 255).WithLocalizedMessage(() => String.Format(ValidationErrors.TooLong, "255"));
Run Code Online (Sandbox Code Playgroud)
ValidationErrors是我的资源文件,其中包含:
TooLong请使用少于{0}个字符.
这个:
RuleFor(x => x.ForeName).Length(1, 255).WithLocalizedMessage(() => ValidationErrors.TooLong);
Run Code Online (Sandbox Code Playgroud)
工作良好.
我有一个MVC 3站点,但我正在使用非MVC FluentValidation dll.我已经创建了一个验证器类,并在构造函数中放置了所有的RuleFors,然后在我的模型类上设置了一个属性
[FluentValidation.Attributes.Validator(typeof(MyValidator))]
Run Code Online (Sandbox Code Playgroud)
问题是验证器类上的构造函数永远不会被调用.我想这可能是因为我没有使用dll的MVC版本,但后来我也无法让这个版本适用于我.
任何帮助,将不胜感激.
谢谢,
萨钦
partial-classes entity-framework-4 fluentvalidation asp.net-mvc-3
我试图使用ASP.NET MVC项目的流畅验证.我正在尝试验证我的视图模型.
这是我的viewmodel,
[Validator(typeof(ProductCreateValidator))]
public class ProductCreate
{
public string ProductCategory { get; set; }
public string ProductName { get; set; }
....
}
Run Code Online (Sandbox Code Playgroud)
这是我的验证员班,
public class ProductCreateValidator : AbstractValidator<ProductCreate>
{
public ProductCreateValidator()
{
RuleFor(product => product.ProductCategory).NotNull();
RuleFor(product => product.ProductName).NotNull();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我正在检查我的ModelState是否有效,
[HttpPost]
public ActionResult Create(ProductCreate model)
{
/* This is a method in viewmodel that fills dropdownlists from db */
model.FillDropDownLists();
/* Here this is always valid */
if (ModelState.IsValid)
{
SaveProduct(model);
return RedirectToAction("Index");
}
// If we …Run Code Online (Sandbox Code Playgroud) 我正在尝试在MVC模型中验证此属性,该模型可以包含由逗号分隔的零个或多个电子邮件地址:
public class DashboardVM
{
public string CurrentAbuseEmails { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
问题是我如何使用电子邮件地址的内置流畅验证规则?现在我有一个使用Must和正则表达式的解决方案,但是我找不到它...优雅.
public DashboardVMValidator()
{
RuleFor(x => x.CurrentAbuseEmails).Must(BeValidDelimitedEmailList).WithMessage("One or more email addresses are not valid.");
}
private bool BeValidDelimitedEmailList(string delimitedEmails)
{
//... match very very long reg. expression
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,最接近的解决方案包括RuleFor(...).EmailAddress()在下面创建了一个自定义Validator,并在每个电子邮件中调用Validate,但由于某种原因这不起作用(AbuseEmailValidator无法获取我的谓词x => x - 在每封电子邮件上调用validator.Validate时).
public class AbuseEmailValidator : AbstractValidator<string>
{
public AbuseEmailValidator()
{
RuleFor(x => x).EmailAddress().WithMessage("Email address is not valid");
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法以一些简单的方式做到这一点?类似于这个解决方案的东西,但有一个字符串而不是字符串列表,因为我不能使用SetCollectionValidator(或者我可以吗?):如何使用Fluent验证对列表中的每个字符串进行验证?
我想使用流利的验证器验证bool属性.我应该使用哪种方法?
.NotNull()和.NotEmpty()函数不起作用.
谢谢.
我在ASP.Net MVC 3项目中设置了FluentValidation.我有一个有两个输入的表格.两者都可以是空白,但不能同时为空.
这就是我所拥有的:
RuleFor(x => x.username)
.NotEmpty()
.When(x => string.IsNullOrEmpty(x.email) == true)
.WithMessage("Please enter either a username or email address")
Run Code Online (Sandbox Code Playgroud)
这正确地将错误直接放在我的用户名字段上方.但是,当两个字段都留空时,我更喜欢验证摘要来显示消息
有没有办法做到这一点?我一直在想我可以在模型中创建一个未使用的字段并将错误放在那里(如果其他两个是空白的话),
RuleFor(x => x.unused_field)
.NotEmpty()
.When(x => string.IsNullOrEmpty(x.email) == true && string.IsNullOrEmpty(x.username) == true)
.WithMessage("Please enter either a username or email address")
Run Code Online (Sandbox Code Playgroud)
但这感觉就像是一种尴尬的方式.有没有办法可以在验证摘要中添加消息?
我试图使用FluentValidation验证三个字段中只有一个具有值.
RuleFor(x => x.Date1)
.Must(x => !x.HasValue)
.When(x => x.Date2.HasValue || x.Date3.HasValue)
.WithMessage("Select only one of Date 1, Date 2 and Date 3");
Run Code Online (Sandbox Code Playgroud)
其他2个日期重复此操作.正如所料,这会产生匹配的每条规则的消息.
还有其他规则,所以有没有办法执行其他规则,但在这三个中的第一个失败?我已经看到我可以在全球范围内设置CascadeMode.StopOnFirstFailure,但我希望这三个规则之外的其他规则能够像现在这样工作.
我正在尝试自动验证我的视图模型,我知道我可以添加一个属性来指定我的验证,但是有一个选项可以设置工厂来自动化所有这些,我看着:这个答案并提出来了这使用简单的注射器3.1:
public class CustomValidatorFactory:ValidatorFactoryBase
{
private readonly Container siContainer;
public CustomValidatorFactory(Container siContainer)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
this.siContainer = siContainer;
this.siContainer.Register(typeof(IValidator<>), assemblies);
}
public override IValidator CreateInstance(Type validatorType)
{
//var instances = siContainer.GetAllInstances(validatorType);
var implementation = ((IServiceProvider)siContainer).GetService(validatorType);
var validatorInstance = implementation != null ? (implementation as IValidator) : null;
return validatorInstance;
}
}
Run Code Online (Sandbox Code Playgroud)
然后视图模型可以是类似的
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; …Run Code Online (Sandbox Code Playgroud) FluentValidation可以使用分层集合吗?是否可以验证具有任意数量的Child节点的以下对象?
public class Node
{
public string Id { get; set; }
public List<Node> ChildNodes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
用非常简单的术语来说,我希望以下代码可以工作:
public class NodeValidator : AbstractValidator<Node>
{
public NodeValidator()
{
RuleFor(x => x.ChildNodes).SetCollectionValidator(new NodeValidator());
RuleFor(x => x.Id).NotEmpty();
}
}
Run Code Online (Sandbox Code Playgroud)
此行导致StackOverflow异常:
RuleFor(x => x.ChildNodes).SetCollectionValidator(new NodeValidator());
Run Code Online (Sandbox Code Playgroud)
如何验证深层嵌套对象的属性"Id"?
我使用的库"FluentValidation.AspNetCore": "6.4.0-beta3"中.netcore WebApi的一个项目。您可以在下面看到项目结构。如果我将 CurrencyDTO.cs代码放在第2节(Project FH.WebAPI)中,并且如果将相同的代码放在第1节(Class Library DTO)中,则库工作正常。要求是我必须将代码放置在Class库中FH.Common。周围有什么解决办法。我已经搜索了,但是没有找到任何东西
项目结构
CurrencyDTO.cs
[Validator(typeof(CurrencyDTOValidator))]
public class CurrencyDTO
{
public int Id { get { return CurrencyId; } }
public int CurrencyId { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
}
public class CurrencyDTOValidator : AbstractValidator<CurrencyDTO>
{
public CurrencyDTOValidator()
{
RuleFor(x => x.Name).NotEmpty().NotNull().WithMessage("The currency 'Name' is required.")
.Length(0, 250).WithMessage("The currency 'Name' cannot be more than 250 characters.");
RuleFor(x …Run Code Online (Sandbox Code Playgroud)