开发者

How to remove one or more fields from the DataForm.Validating() event in Silverlight 4?

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 a开发者_Go百科nd 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...


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜