使用验证规则产生错误时如何设置默认值?

发布于 2024-12-28 14:11:02 字数 841 浏览 3 评论 0原文

我使用验证规则来显示自定义消息,并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵山崖 2025-01-04 14:11:02

技术上 - 您可以通过引用传递值:

public override ValidationResult Validate(ref object value, System.Globalization.CultureInfo cultureInfo)

哲学 - 您希望验证规则更改该值,还是只是通知该值不好?从用户的角度来看,如果我尝试输入一些值,而系统告诉我该值无效,但又不给我机会纠正它,只能重新开始,这可能会很烦人。

如果请求验证的代码想要将值设置为 null,那就是您的选择:

int? result;
string input = "5b"

ValidationResult response = ResponseValidation().Validate(input, CultureInfo.CurrentCulture);
if (!response.IsValid) 
    result = null;
else
    result = int.Parse(input, CultureInfo.CurrentCulture);

只需我的 2 美分...

Technically - you could pass the value by reference:

public override ValidationResult Validate(ref object value, System.Globalization.CultureInfo cultureInfo)

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:

int? result;
string input = "5b"

ValidationResult response = ResponseValidation().Validate(input, CultureInfo.CurrentCulture);
if (!response.IsValid) 
    result = null;
else
    result = int.Parse(input, CultureInfo.CurrentCulture);

Just my 2 cents...

恰似旧人归 2025-01-04 14:11:02

除非 value 是引用参数,否则你不能。如果您尝试分离逻辑,那么关于数据是否有效的决定应该与您在该决定之后想要执行的操作不同,例如清除输入字段。

例如:

if (!rule.Validate(val, CultureInfo.CurrentCulture).IsValid)
{
    //Clear the field... whatever else you want to do
}

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:

if (!rule.Validate(val, CultureInfo.CurrentCulture).IsValid)
{
    //Clear the field... whatever else you want to do
}
庆幸我还是我 2025-01-04 14:11:02

前面的答案提到了通过引用将值传递到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文