我得到了erorr:
不显眼的客户端验证规则中的验证类型名称必须是唯一的.以下验证类型不止一次出现:必需.以下验证类型不止一次出现:必需
我用过服务器验证.一切正常.但现在我说要使用客户端验证,我遇到了这个问题.
这是我的验证类代码:
public class TestViewDataValidation : BaseTestCreateViewDataValidation<BaseTestCreateViewData>
{
public TestViewDataValidation ()
{
this.RuleFor(x => x.Login).NotNull();
this.RuleFor(x => x.Login).NotEmpty();
this.RuleFor(x => x.Login).EmailAddress();
}
}
Run Code Online (Sandbox Code Playgroud)
但如果我留下一个验证器 - 一切正常.我该怎么做才能为字段提供更多的验证.
我试图验证是否使用FluentValidation在客户端上选中了复选框.我无法想象我的生活.
可以使用不显眼的验证吗?
我可以为对象添加多个验证器吗?例如:
public interface IFoo
{
int Id { get; set; }
string Name { get; set; }
}
public interface IBar
{
string Stuff { get; set; }
}
public class FooValidator : AbstractValidator<IFoo>
{
public FooValidator ()
{
RuleFor(x => x.Id).NotEmpty().GreaterThan(0);
}
}
public class BarValidator : AbstractValidator<IBar>
{
public BarValidator()
{
RuleFor(x => x.Stuff).Length(5, 30);
}
}
public class FooBar : IFoo, IBar
{
public int Id { get; set; }
public string Name { get; set; } …Run Code Online (Sandbox Code Playgroud) 在MVC中,我可以创建一个可以采用依赖关系的模型验证器.我通常使用FluentValidation.例如,这允许我检查帐户注册是否未使用电子邮件地址(注意:这是一个简化的示例!):
public class RegisterModelValidator : AbstractValidator<RegisterModel> {
private readonly MyContext _context;
public RegisterModelValidator(MyContext context) {
_context = context;
}
public override ValidationResult Validate(ValidationContext<RegisterModel> context) {
var result = base.Validate(context);
if (context.Accounts.Any(acc => acc.Email == context.InstanceToValidate.Email)){
result.Errors.Add(new ValidationFailure("Email", "Email has been used"));
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
使用FluentValidation的Web API不存在此类集成.已经有夫妇的尝试,在这一点,但也没有解决的依赖注入方面,只有静态验证工作.
这很困难的原因是由于MVC和Web API之间的ModelValidatorProvider和ModelValidator的实现不同.在MVC中,这些是按请求实例化的(因此注入上下文很容易).在Web API中,它们是静态的,ModelValidatorProvider为每种类型维护ModelValidators的缓存,以避免对每个请求进行不必要的反射查找.
我一直试图自己添加必要的集成,但一直试图获得依赖范围.相反,我想我会退后一步,询问是否还有其他问题的解决方案 - 如果有人提出了执行模型验证的解决方案,可以注入依赖关系.
我不想在Controller中执行验证(我使用ValidationActionFilter来保持这一点),这意味着我无法从控制器的构造函数注入中获得任何帮助.
我正在测试PUT两个string:
company.CurrencyCode = request.CurrencyCode ?? company.CurrencyCode;
company.CountryIso2 = request.Country ?? company.CountryIso2;
Run Code Online (Sandbox Code Playgroud)
我尝试过如下规则:
public UpdateCompanyValidator()
{
RuleSet(ApplyTo.Put, () =>
{
RuleFor(r => r.CountryIso2)
.Length(2)
.When(x => !x.Equals(null));
RuleFor(r => r.CurrencyCode)
.Length(3)
.When(x => !x.Equals(null));
});
}
Run Code Online (Sandbox Code Playgroud)
因为我不介意获得null这些属性,但我想测试属性Length 何时不是null.
在属性是什么时应用规则的最佳方法是什么nullable,我们只想测试它是否为空?
我有一个域模型/实体,根据其填充方式需要进行不同的验证。假设我想出了 3 个验证器,如下所示:
public class Product1Validator : AbstractValidator<Ticket>
{
public Product1Validator()
{
RuleFor(ticket => ticket.Policy.PolicyNumber)
.NotEmpty()
.WithMessage("Policy Number is missing.");
RuleFor(ticket => ticket.Policy.ApplSignedInState)
.NotEmpty()
.WithMessage("Application Signed In State is missing or invalid.");
}
}
public class Product2Validator : AbstractValidator<Ticket>
{
public Product2Validator()
{
RuleFor(ticket => ticket.Policy.PolicyNumber)
.NotEmpty()
.WithMessage("Policy Number is missing.");
RuleFor(ticket => ticket.Policy.ApplSignedInState)
.NotEmpty()
.WithMessage("Application Signed In State is missing or invalid.");
}
}
public class Product3Validator : AbstractValidator<Ticket>
{
public Product3Validator()
{
RuleFor(ticket => ticket.Policy.PolicyNumber)
.NotEmpty()
.WithMessage("Policy Number …Run Code Online (Sandbox Code Playgroud) 我想通过Nuget Package Manager在我的asp.net核心应用程序中添加FluentValidation Nuget包.虽然添加我收到此错误:
严重级代码说明项目文件行抑制状态错误检测到Microsoft.CodeAnalysis.Common的版本冲突.直接从项目引用包以解决此问题.项目名称 - > FluentValidation.AspNetCore 7.6.103 - > Microsoft.AspNetCore.Mvc 2.1.0 - > Microsoft.AspNetCore.Mvc.TagHelpers 2.1.0 - > Microsoft.AspNetCore.Mvc.Razor 2.1.0 - > Microsoft.CodeAnalysis. Razor 2.1.0 - > Microsoft.CodeAnalysis.Common(> = 2.8.0)项目名称 - > Microsoft.VisualStudio.Web.CodeGeneration.Design 2.0.0 - > Microsoft.VisualStudio.Web.CodeGenerators.Mvc 2.0.0 - > Microsoft.VisualStudio.Web.CodeGeneration 2.0.0 - > Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore 2.0.0 - > Microsoft.VisualStudio.Web.CodeGeneration.Core 2.0.0 - > Microsoft.VisualStudio.Web.CodeGeneration.Templating 2.0 .0 - > Microsoft.VisualStudio.Web.CodeGeneration.Utils 2.0.0 - > Microsoft.CodeAnalysis.CSharp.Workspaces 2.3.1 - > Microsoft.CodeAnalysis.Workspaces.Common 2.3.1 - > Microsoft.CodeAnalysis.Common(= 2.3 0.1).
我该如何解决这个错误?
当dropdownlist值为,yes并且字段必须为date 时,我正在尝试使用FluentValidation validaton .当dropdownlist yes检查时它正在工作date.但是当我选择No它时,它也会显示验证Must be date.
它应该不再验证是否下拉列表值除了yes.我们怎么做?
RuleFor(x => x.DtPublishedTimeText)
.NotEmpty()
.When(HasMaterialPublishedElseWhereText)
.WithMessage("Required Field")
.Must(BeAValidDate)
.WithMessage("Must be date");
private bool BeAValidDate(string val)
{
DateTime date;
return DateTime.TryParse(val, out date);
}
private bool HasMaterialPublishedElseWhereText(MeetingAbstract model)
{
return model.HasMaterialPublishedElseWhereText != null &&
model.HasMaterialPublishedElseWhereText.Equals("yes");
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用Fluent Validation for MVC 5的ASP.NET MVC 5项目.我还使用jQuery masking插件自动添加数千个双值.
在模型中我有:
[Display(Name = "Turnover")]
[DisplayFormat(ApplyFormatInEditMode = true,ConvertEmptyStringToNull =true,DataFormatString ="#,##0")]
public double? Turnover { get; set; }
Run Code Online (Sandbox Code Playgroud)
在视图中我有:
<th class="col-xs-2">
@Html.DisplayNameFor(model=>model.Turnover)
</th>
<td class="col-xs-4">
@Html.TextBoxFor(model => model.Turnover, new { @class = "form-control number", placeholder="Enter number. Thousands added automatically" })
</td>
<td class="col-xs-6">
@Html.ValidationMessageFor(model => model.Turnover, "", new { @class = "text-danger" })
</td>
Run Code Online (Sandbox Code Playgroud)
为包含模型定义了一个流畅的验证器,但它不包含任何规则.我只使用服务器端验证.
public class MyModelValidator: AbstractValidator<MyModel>
{
public MyModelValidator()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用模型绑定来解决这个问题.但是模型绑定器的断点永远不会受到打击 - 流畅的验证似乎阻止了达到模型绑定器的价值.
我目前正在使用Fluent Validation而不是使用Data Annotations我的Web API并使用swagger来获取API文档.流畅的验证规则没有反映在swagger模型中,因为我无法使用swagger模式过滤器配置流畅的验证规则.
本博客对ASP.net MVC使用它有很好的解释.但我无法配置它在ASP.net Core中使用它.
到目前为止,我已经尝试了以下代码,但我无法获得验证器类型.
services.AddSwaggerGen(options => options.SchemaFilter<AddFluentValidationRules>());
public class AddFluentValidationRules : ISchemaFilter
{
public void Apply(Schema model, SchemaFilterContext context)
{
model.Required = new List<string>();
var validator = GetValidator(type); // How?
var validatorDescriptor = validator.CreateDescriptor();
foreach (var key in model.Properties.Keys)
{
foreach (var propertyValidator in validatorDescriptor.GetValidatorsForMember(key))
{
// Add to model properties as in blog
}
}
}
}
Run Code Online (Sandbox Code Playgroud) fluentvalidation ×10
c# ×6
asp.net-core ×2
asp.net-mvc ×2
.net ×1
c#-4.0 ×1
jquery ×1
swagger ×1