自定义验证仅运行一次
我正在创建一个 MVVM Silverlight 应用程序,并尝试向我的模型添加验证。
在这种特殊情况下,我有多个电话号码框,并且要求至少必须输入一个电话号码。为了方便起见,我在模型上使用 CustomValidation 属性装饰了我的属性(HomePhone、WorkPhone、MobilePhone 和 OtherPhone)。
示例属性:
private string _otherPhone;
[CustomValidation(typeof(MyModel), "ValidatePhoneNumbers")]
public string OtherPhone
{
get { return _otherPhone; }
set
{
if (_otherPhone == value) return;
_otherPhone = value;
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OtherPhone" });
RaisePropertyChanged(() => HomePhone);
RaisePropertyChanged(() => WorkPhone);
RaisePropertyChanged(() => MobilePhone);
RaisePropertyChanged(() => OtherPhone);
}
}
连同自定义验证器本身:
public static ValidationResult ValidatePhoneNumbers(string number, ValidationContext validationContext)
{
MyModel myModel = (MyModel)validationContext.ObjectInstance;
return string.IsNullOrEmpty(myModel.HomePhone) && string.IsNullOrEmpty(myModel.WorkPhone) && string.IsNullOrEmpty(myModel.MobilePhone) && string.IsNullOrEmpty(myModel.OtherPhone)
? new ValidationResult("At least one phone number must be entered")
: ValidationResult.Success;
}
我已确保在运行验证之前在属性设置器中设置后备存储,以确保 ValidatePhoneNumbers 方法能够检查最新值。
当我尝试保存模型时,我正在对视图中的所有绑定执行 UpdateBindingExpression。现在,我第一次尝试保存时,它可以正常工作,并且所有四个字段都突出显示为有错误。
但是,如果我尝试再次保存,所有字段都会标记为已通过验证,并且永远不会命中 ValidatePhoneNumbers 中的断点。这是为什么呢?
谢谢
I am creating an MVVM Silverlight application and am trying to add validation to my Model.
In this particular instance I have multiple phone number boxes and have the requirement that at least one phone number must be entered. To facilitate I have my properties (HomePhone, WorkPhone, MobilePhone and OtherPhone) on my model decorated with an a CustomValidation attribute.
Example property:
private string _otherPhone;
[CustomValidation(typeof(MyModel), "ValidatePhoneNumbers")]
public string OtherPhone
{
get { return _otherPhone; }
set
{
if (_otherPhone == value) return;
_otherPhone = value;
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "OtherPhone" });
RaisePropertyChanged(() => HomePhone);
RaisePropertyChanged(() => WorkPhone);
RaisePropertyChanged(() => MobilePhone);
RaisePropertyChanged(() => OtherPhone);
}
}
Along with the custom validator itself:
public static ValidationResult ValidatePhoneNumbers(string number, ValidationContext validationContext)
{
MyModel myModel = (MyModel)validationContext.ObjectInstance;
return string.IsNullOrEmpty(myModel.HomePhone) && string.IsNullOrEmpty(myModel.WorkPhone) && string.IsNullOrEmpty(myModel.MobilePhone) && string.IsNullOrEmpty(myModel.OtherPhone)
? new ValidationResult("At least one phone number must be entered")
: ValidationResult.Success;
}
I have ensured that the backing store is set in the property setter before the validation is run, to ensure that the ValidatePhoneNumbers method is able to check the latest value.
When I try to save my model, I am executing UpdateBindingExpression on all the bindings in the View. Now this works correctly the first time that I try to save, and all four fields are highlighted as having an error.
If I try to save again however, all fields are marked as passed validation, and a breakpoint in ValidatePhoneNumbers is never hit. Why is this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确信,每次我花足够长的时间与 bug 作斗争并在 SO 上发帖时,我会在 20 秒后计算出实际答案......
if (_otherPhone == value) return;< /code> 导致验证无法运行 - 如果验证不依赖于其他属性,则此优化是有意义的。
没关系...
I'm sure that every time that I spend long enough fighting a bug to resort to posting on SO, I work out what the actual answer is 20 seconds later... The
if (_otherPhone == value) return;
was causing the validation not to run - this optimisation would make sense if the validation wasn't dependent on another property.Never mind...