c# - Fluent Validation,在编辑模式下,需要在属性更改时触发规则
问题描述
在编辑值时,如果 property2 的值发生更改,我必须验证 property1。
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public PersonType PersonType { get; set; }
public bool IsVIP { get; set; }
}
public enum PersonType
{
VIP,
AnyType1,
AnyType2
}
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(x => x.PersonType).Must(CantChangePersonTypeForVIP);
}
private bool CantChangePersonTypeForVIP(Person person, PersonType arg)
{
// even if PersonType was not changed this method will call and a VIP person validation will always fail
if (person.IsVIP)
{
return false;
}
return true;
}
}
如评论中所述,即使 PersonType 未更改,此方法也会调用并且 VIP 人员验证将始终失败。
我该如何实施?理想情况下,我期望如果 PersonType 在编辑时没有更改,它就不会调用。
我不知道如何实现这一点。
我是否应该点击 DB 来比较原始对象?
解决方案
推荐阅读
- ionic4 - Ionic 我应该何时导入 app.module 以及何时导入页面模块?
- javascript - 如何在反应js中将分页应用于动态表
- rust - 国库模块的 set_pot 功能是否可供公众使用?
- python - Python 脚本产生文件之间的差异并解析输出的 DNS 查询
- java - 如何将 JAVA 8 后端链接到 Angular 前端?
- python - Odoo 11 在同一模型的两个不同区域添加不同的操作菜单
- knn - ELKI KNNDistancesSampler
- android - IOS 等效于 Android 的广播接收器
- sql - 如何仅获取具有修改的特定行值的数据表的第一行
- arrays - 如何检查(字符串)是否等于(数组内的字符串)?我可以用 If 语句检查它吗?