在验证失败时禁用保存按钮 [英] Disable save button when validation fails

查看:103
本文介绍了在验证失败时禁用保存按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你可能可以从标题看,我要问的东西已被问过很多次。但仍然,阅读所有这些其他问题后,我无法找到一个体面的解决我的问题。

As you can likely see from the title, I am about to ask something which has been asked many times before. But still, after reading all these other questions, I cannot find a decent solution to my problem.

我有一个基本的验证模型类:

I have a model class with basic validation:

partial class Player : IDataErrorInfo
{
    public bool CanSave { get; set; }

    public string this[string columnName]
    {
        get 
        { 
            string result = null;
            if (columnName == "Firstname")
            {
                if (String.IsNullOrWhiteSpace(Firstname))
                {
                    result = "Geef een voornaam in";
                }
            }
            if (columnName == "Lastname")
            {
                if (String.IsNullOrWhiteSpace(Lastname))
                {
                    result = "Geef een familienaam in";
                }
            }
            if (columnName == "Email")
            {
                try
                {
                    MailAddress email = new MailAddress(Email);
                }
                catch (FormatException)
                {
                    result = "Geef een geldig e-mailadres in";
                }
            }
            if (columnName == "Birthdate")
            {
                if (Birthdate.Value.Date >= DateTime.Now.Date)
                {
                    result = "Geef een geldige geboortedatum in";
                }
            }

            CanSave = true; // this line is wrong
            return result;
        }
    }

    public string Error { get { throw new NotImplementedException();} }
}

这验证每次所做的属性更改(所以每次用户键入的文本字符):

This validation is done everytime the property changes (so everytime the user types a character in the textbox):

<TextBox Text="{Binding CurrentPlayer.Firstname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="137" IsEnabled="{Binding Editing}" Grid.Row="1"/>

这完美的作品。验证发生(即的PropertyChanged 代码在虚拟机上CurrentPlayer属性,它是玩家的对象完成绑定)。

This works perfect. The validation occurs (the PropertyChanged code for the binding is done in the VM on the CurrentPlayer property, which is an object of Player).

我想现在要做的就是禁用验证失败保存按钮。

What I would like to do now is disable the save button when the validation fails.

首先,最简单的解决方案似乎是在被发现这个线程:结果
启用禁用验证过程中保存按钮使用IDataErrorInfo的

First of all, the easiest solutions seems to be found in this thread:
Enable Disable save button during Validation using IDataErrorInfo


  1. 如果我想跟进接受的解决方案,我不得不写我的
    :验证码两次,因为我不能简单地使用索引。写作
    双码绝对不是我想要的,所以这不是一个解决方案
    我的问题。

  2. 在该线程的第二个答案听起来第一非常有前途的,
    ,但问题是,我有一个必须是
    验证多个字段。这样一来,一切都依赖于最后检查属性
    (所以如果该字段填写正确, CanSave 将是真实的,甚至
    虽然有其他领域这仍然是无效的)。

  1. If I want to follow the accepted solution, I'd have to write my validation code twice, as I cannot simply use the indexer. Writing double code is absolutely not what I want, so that's not a solution to my problem.
  2. The second answer on that thread sounded very promising as first, but the problem is that I have multiple fields that have to be validated. That way, everything relies on the last checked property (so if that field is filled in correctly, CanSave will be true, even though there are other fields which are still invalid).

还有一个解决方案,我发现是使用的ErrorCount 属性。但正如我在每个属性的变化验证(所以在每个输入的字符),这是不可能太 - 我怎么会知道什么时候该增加/减少的ErrorCount

One more solution I've found is using an ErrorCount property. But as I'm validating at each property change (and so at each typed character), this isn't possible too - how could I know when to increase/decrease the ErrorCount?

什么是解决这个问题的最好方法是什么?

What would be the best way to solve this problem?

感谢

推荐答案

我已经在实施的上面我的评论,在C#这就是所谓的的词典在我使用的匿名方法以做验证:

I've implemented the map approach shown in my comment above, in C# this is called a Dictionary in which I am using anonymous methods to do the validation:

partial class Player : IDataErrorInfo
{
    private delegate string Validation(string value);
    private Dictionary<string, Validation> columnValidations;
    public List<string> Errors;

    public Player()
    {
        columnValidations = new Dictionary<string, Validation>();
        columnValidations["Firstname"] = delegate (string value) {
            return String.IsNullOrWhiteSpace(Firstname) ? "Geef een voornaam in" : null;
        }; // Add the others...

        errors = new List<string>();
    }

    public bool CanSave { get { return Errors.Count == 0; } }

    public string this[string columnName]
    {
        get { return this.GetProperty(columnName); } 

        set
        { 
            var error = columnValidations[columnName](value);

            if (String.IsNullOrWhiteSpace(error))
                errors.Add(error);
            else
                this.SetProperty(columnName, value);
        }
    }
}

这篇关于在验证失败时禁用保存按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆