如何从Silverlight 4中的DataForm.Validating()事件中删除一个或多个字段?
我有一个绑定到对象的数据表单,该对象的属性用Validation的如何从Silverlight 4中的DataForm.Validating()事件中删除一个或多个字段?,silverlight,silverlight-4.0,silverlight-toolkit,dataform,validation,Silverlight,Silverlight 4.0,Silverlight Toolkit,Dataform,Validation,我有一个绑定到对象的数据表单,该对象的属性用Validation的System.ObjectModel.DataAnnotation属性修饰 我面临的问题是,这个类的某些属性只是有条件地需要的,不需要验证。例如,当应用程序管理员决定编辑用户时, 他或她可以输入密码/密码确认/密码问题/密码答案。或者他/她可能会完全跳过这些属性 因此,如果管理员决定输入这4个字段中的任何一个,它们都必须存在,并且必须应用所有这些字段的验证规则。但是,如果管理员只想更改FirstName、LastName、Emai
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的数据表单,我得到的错误在以下代码中:
private void RegistrationButton_Click(object sender, RoutedEventArgs e)
{
if( this.registerForm.ValidateItem() )
{
//Does not pass validaton if the password properties are not filled in.
}
}
有没有办法解决这个问题
我在考虑使用两个数据表单。。。并将用户对象一分为二,但这涉及大量代码…我建议在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");
}
}
我建议您查看此链接以了解有关不同验证类型的更多信息
MSDN对如何使用它也有很好的解释
给我带来了另一个解决方案。我现在使用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,在添加新用户时在客户端中设置了该属性。自定义验证方法检查此属性并执行所需的验证。我仍然在密码上有一个RegularExpression属性,它也将被验证
与@Staindart的解决方案相比,这是在客户端同步检查的。最简单、最丑陋的方法是点击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;
}
}
请问您是如何进行验证的?更确切地说,您是否使用任何预定义的接口来进行错误通知?我这样问是因为您似乎没有使用注册数据类,所以它发生在哪里?您使用的是唯一的dataannotation吗?@Stainedart:是的,我们使用Silverlight数据表单和批注属性。对于新用户,我想输入密码,当我编辑用户密码时,不应验证密码字段。在我的情况下,我很乐意找到一个解决方案来禁用验证。@slfan如果您使用与数据注释不同的方式查看我下面的答案,将允许您具有所需的验证灵活性。谢谢您的输入。我知道INotifyDataError接口。要写的代码很多。我只想暂时禁用一个验证属性。