如何从 Silverlight 4 中的 DataForm.Validating() 事件中删除一个或多个字段?
我有一个数据表单,它绑定到一个对象,该对象的属性用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议在 RegistrationData 对象上使用 INotifyDataError 接口。
我建议您查看此链接 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.
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
这个问题 让我想到了另一个解决方案。我现在使用 CustomValidation:
我添加了一个属性 IsNewUser,该属性是在添加新用户时在客户端中设置的。自定义验证方法检查此属性并执行所需的验证。我的密码上仍然有一个正则表达式属性,该属性也将被验证。
与 @Staindart 的解决方案相比,这是在客户端同步检查的。
This question brought me to another solution. I now use CustomValidation:
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.
最简单和最丑陋的方法是利用 DataForm.ValidatingItem 事件。就像这样:
The simplest and ugliest way would be to tap into the DataForm.ValidatingItem event. Like so: