使用验证规则产生错误时如何设置默认值?
我使用验证规则来显示自定义消息,并使用 INotifyDataError 来显示业务规则。
我的代码的这一部分:
public class ResponseValidation : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
int? response;
bool noIllegalChars = TryParseStruct<int>(value.ToString(), out response);
if (!noIllegalChars)
{
return new ValidationResult(false, "Input is not in a correct format.");
}
else
{
value = null;
return new ValidationResult(true, null);
}
}
...
}
验证针对的是 Nullable 数据类型,我的意思是该值可以为 null 或整数,但不能是像“5b”这样的错误输入。
问题是当它产生此错误(noIllegalChars = true)时,如何将属性值设置为 null?
编辑:我这样做的原因是因为,当用户将文本框留空(该值将为“”)时,从技术上讲,对于该属性来说,它是一个空值,但它试图将“”设置为该属性。
I'm using Validation Rules to show custom messages, and INotifyDataError for the business rules.
This one part of my code:
public class ResponseValidation : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
int? response;
bool noIllegalChars = TryParseStruct<int>(value.ToString(), out response);
if (!noIllegalChars)
{
return new ValidationResult(false, "Input is not in a correct format.");
}
else
{
value = null;
return new ValidationResult(true, null);
}
}
...
}
The validation is for a Nullable datatype, I mean the value can be null or an integer, but not an incorrect input like "5b".
The question is when it produces this error (noIllegalChars = true), how can I set to the property the value null?
EDIT: The reason which I'm doing this is because, when the user left the textbox empty (the value will be "") and technically, for the property that's a null value, but it's trying to set "" to the property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
技术上 - 您可以通过引用传递值:
哲学 - 您希望验证规则更改该值,还是只是通知该值不好?从用户的角度来看,如果我尝试输入一些值,而系统告诉我该值无效,但又不给我机会纠正它,只能重新开始,这可能会很烦人。
如果请求验证的代码想要将值设置为 null,那就是您的选择:
只需我的 2 美分...
Technically - you could pass the value by reference:
Philosophically - do you want your validation rule changing the value, or just notifying that the value is bad? From a user's point of view it could be annoying if I try to input some value and the system tells me it's not valid, but doesn't give me the opportunity to correct it, only to start over.
If the code asking for validation wants to set the value to null that's your choice:
Just my 2 cents...
除非 value 是引用参数,否则你不能。如果您尝试分离逻辑,那么关于数据是否有效的决定应该与您在该决定之后想要执行的操作不同,例如清除输入字段。
例如:
You can't unless value is a ref param. If you're trying to separate logic though, the decision as to whether or not the data is valid should be different from what you want to do AFTER that decision, such as clearing the input field.
For example:
前面的答案提到了通过引用将值传递到 Validate 函数中 - 正如他们提到的那样,这不仅在语义上不正确,而且如果您创建一个重载的 Validate 函数来执行此操作,则绑定子系统将不会调用如果您在 XAML 中声明验证规则,则会出现此情况。当然可以通过编程方式调用它,但是如果您开始使用只能通过这种方式访问的代码创建 ValidationRules,那么您就是搬起石头砸自己的脚。
更合适的操作是在字段上使用转换器,如果传入空字符串,则返回默认的
int?
。The previous answers have mentioned passing the value into the Validate function byref - not only is this is semantically incorrect as they mention, but if you create an overloaded
Validate
function that does this then the binding subsystem will not call it if you declare the validation rule within the XAML. Sure it can be called programmatically, but you are shooting yourself in the foot if you start creating ValidationRules with code that can only be reached in that way.A more appropriate action to take is to use a converter on the field that returns a default
int?
if an empty string is passed in.