4

System.ObjectModel.DataAnnotationプロパティが検証用の属性で装飾されているオブジェクトにバインドされたデータフォームがあります。

私が直面している問題は、このクラスの一部のプロパティが条件付きでのみ必要であり、検証する必要がないことです。たとえば、アプリの管理者がユーザーを編集する場合、パスワード/パスワードの確認/パスワードの質問/パスワードの回答を入力できます。または、それらのプロパティを完全にスキップすることもできます。

したがって、管理者がこれら 4 つのフィールドのいずれかを入力することにした場合、それらはすべて存在している必要があり、これらすべてのフィールドの検証ルールを適用する必要があります。ただし、管理者が FirstName、LastName、Email、またはその他の任意のプロパティのみを変更したい場合は、パスワード関連のフィールドを検証する必要はありません。

それらを検証プロセスから「除外」する方法はありますか?

これは私が扱うオブジェクトのサンプルです:

public class RegistrationData
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string Email { get; set; }
   public string Username { get; set; }
   public string Password { get; set; }
   public string PasswordConfirm { get; set; }
   public string PasswordQuestion { get; set; }
   public string PasswordAnswer { get; set; }
}

Xaml に registrationForm という DataForm があり、エラーは次のコードにあります。

private void RegistrationButton_Click(object sender, RoutedEventArgs e)
{
   if( this.registerForm.ValidateItem() )
   {
       //Does not pass validaton if the password properties are not filled in.
   }
}

それを修正する方法についてのアイデアはありますか?

私は2つのDataFormsを使用することを考えていました...そしてユーザーオブジェクトを2つに分割しましたが、それには多くのコードが含まれます...

4

3 に答える 3

1

この質問は私に別の解決策をもたらしました。私は今CustomValidationを使用しています:

[CustomValidation(typeof(RegistrationDataValidation), "ValidatePassword")]
public class RegistrationData  
{  
    public bool IsNewUser { get; set; }
     ... // other registration properties
}  

public static class RegistrationDataValidation
{
    public static ValidationResult ValidatePassword(MembershipServiceUser user, ValidationContext context)
    {
        if (user.IsNewUser && string.IsNullOrEmpty(user.Password))
        {
            return new ValidationResult("Password required");
        }
        return ValidationResult.Success;
    }
}

新しいユーザーを追加するときにクライアントで設定したプロパティ IsNewUser を追加しました。カスタム検証メソッドは、このプロパティをチェックし、目的の検証を実行します。パスワードにはまだ正規表現属性があり、これも検証されます。

@Staindart のソリューションと比較して、これはクライアントで同期的にチェックされます。

于 2012-05-22T08:29:32.380 に答える
1

RegistrationData オブジェクトで INotifyDataError インターフェイスを使用することをお勧めします。

    public string LabelWrapper
    {
        get
        {
            return this.Label;
        }
        set
        {
            ValidateRequired("LabelWrapper", value, "Label required");
            ValidateRegularExpression("LabelWrapper", value, @"^[\w-_ ]+$", "Characters allowed (a-z,A-Z,0-9,-,_, )");
            this.Label = value;
            this.RaisePropertyChanged("LabelWrapper");
        }
    }

    public string DependentLabelWrapper
    {
        get
        {
            return this.DependentLabel;
        }
        set
        {
            if(LabelWrapper != null){
                ValidateRequired("DependentLabelWrapper", value, "Label required");
                ValidateRegularExpression("LabelWrapper", value, @"^[\w-_ ]+$", "Characters allowed (a-z,A-Z,0-9,-,_, )");
             }
            this.DependentLabel = value;
            this.RaisePropertyChanged("DependentLabelWrapper");
        }
    }

このリンクhttp://blogs.msdn.com/b/nagasatish/archive/2009/03/22/datagrid-validation.aspxを参照して、さまざまな検証タイプの詳細を確認することをお勧めします。

また、MSDN には、その使用方法に関する優れた説明があります。

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx

于 2012-05-17T17:56:48.243 に答える
0

最も単純で醜い方法は、DataForm.ValidatingItem イベントを利用することです。そのようです:

    void dfEditForm_ValidatingItem(object sender, System.ComponentModel.CancelEventArgs e)
    {
        foreach (ValidationSummaryItem item in dfEditForm.ValidationSummary.Errors)
        {
            if (item.Sources.Where(W => W.PropertyName != "myIgnoredPropertyName").Count() > 0)
                e.Cancel = true;
        }
    }
于 2012-06-19T07:34:55.660 に答える