如何从 Silverlight 4 中的 DataForm.Validating() 事件中删除一个或多个字段?

发布于 2024-11-18 08:23:57 字数 1106 浏览 2 评论 0原文

我有一个数据表单,它绑定到一个对象,该对象的属性用 System.ObjectModel.DataAnnotation 属性装饰以进行验证。

我面临的问题是这个类的某些属性只是有条件需要的,不需要验证。例如,当应用程序的管理员决定编辑用户时, 他或她可以输入密码/密码确认/密码问题/密码答案。或者他/她可能完全跳过这些属性。

因此,如果管理员决定输入这 4 个字段中的任何一个,则它们都必须存在,并且必须应用所有这些字段的验证规则。但是,如果管理员只想更改名字、姓氏、电子邮件或任何其他任意属性,则不需要验证密码相关字段。

有没有办法将它们“排除”在验证过程之外?

这是我使用的对象的示例:

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.
   }
}

关于如何修复它的任何想法?

我正在考虑使用两个 DataForms...并将用户对象一分为二,但这涉及大量代码...

I have a data form that is bound to an object whose properties are decorated with System.ObjectModel.DataAnnotation attributes for validaton.

The problem I am facing is that some properties of this class are only conditionally needed and do not need to be validated. For example when an admin of the app decides to edit a user,
he or she may enter a password/password confirm/password question/password answer. Or he/she may entirely skip those properties.

So if the admin decides to enter any of those 4 fields, they all have to be present and the validation rules for all these fields have to be applied. However if the admin only wants to change the FirstName, LastName, Email, or whatever other arbitrary properties - the password related fields do not need to be validated.

Is there a way to "Exclude" them from the validation process?

this is a sample of the object I work with:

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; }
}

I have a DataForm called registrationForm in the Xaml and the error I get is in this code:

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

Any ideas on how to fix it?

I was thinking of using two DataForms... and split the user object in two, but that involves a lot of code...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

清晰传感 2024-11-25 08:23:57

我建议在 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

I would recommend to use the INotifyDataError interface on your RegistrationData object.

    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");
        }
    }

I recommend you to look at this link http://blogs.msdn.com/b/nagasatish/archive/2009/03/22/datagrid-validation.aspx to learn more about different validation types.

Also MSDN has a nice explanation on how to use it

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

瑕疵 2024-11-25 08:23:57

这个问题 让我想到了另一个解决方案。我现在使用 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 的解决方案相比,这是在客户端同步检查的。

This question brought me to another solution. I now use 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;
    }
}

I added a property IsNewUser which I set in the client when adding a new user. The custom validation method checks this property and executes the desired validation. I still have a RegularExpression Attribute on the password which will be validated as well.

In comparison to @Staindart's solution this is checked on the client synchronously.

落花浅忆 2024-11-25 08:23:57

最简单和最丑陋的方法是利用 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;
        }
    }

The simplest and ugliest way would be to tap into the DataForm.ValidatingItem event. Like so:

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