将 Silverlight DataForm.DataField 标记为无效?
好吧,这应该是一件非常简单的事情,我要么错过了一些东西,要么做错了。
事实:
Silverlight 4,使用工具包并使用 DataForms/DataFields,但不使用验证摘要。
使用模板数据表单,简单的新建/编辑用户表单,其中包含用户名、名字等常用字段,以及密码和确认密码。
使用模板数据表单,我想进行验证,以便如果他们正在编辑现有用户并且密码字段为空,则让它通过。否则,请确保密码相同。 (这已经可以在我的模型上使用类级别自定义验证器了。)
如果验证在#3 中失败,则通过在密码字段上放置常用线索来提醒用户。 (红色边框,如果您单击该字段,则会出现消息)
我现在可以完成#3 之前的所有操作。但如果发生错误,则该错误将显示为表单错误,而不是字段错误(这是有道理的,因为验证是针对类而不是属性)。我找不到标记我的字段以显示错误的方法。由于我没有使用验证摘要(出于设计原因),因此错误永远不会向用户显示。
有没有其他人这样做过,或者他们可以向我发送一个带有不使用验证摘要的密码字段的数据表单示例,以便我可以看看他们是如何解决这个问题的?
我当前看到的两个选项是自己手动标记数据字段。或者,我将构建自己的显示完整形式错误的方法,该方法不会像验证摘要那样重复错误。 (IE,我需要一个仅显示表单错误 - 不显示字段错误的验证摘要。)
谢谢
Ok, this should be a really simple thing to do, and I'm either missing something, or going about it wrong.
Facts:
Silverlight 4, using the Toolkit and using DataForms / DataFields, but NOT using a validation summary.
Using a Templated Dataform, simple new / edit user form with usual fields like username, firstname, etc, plus Password and Confirm Password.
I want to validate so that if they are editing an existing user, and the password fields are blank, let it go through. Otherwise, make sure the passwords are the same. (This works already using a class level custom validator on my model.)
If the validation fails in #3, then alert the user by putting the usual clues on the Password field. (red border, message if you click in the field)
I can do everything now up through #3. But if the error occurs, then the error is shown as a form error, not a field error (which makes sense since the validation is on the class, not the property). I can not find a way to mark up my field to show the error. Since I'm not using a validation summary (for design reasons) the error just never shows up for the user.
Has anyone else done this, or can they send me to an example of a data form with password fields that does not use a Validation Summary so I can see how they solved it?
My two current options that I see are to manually mark the data field myself. OR, I'll build my own method of display full form errors that doesn't repeat errors like the validation summary does. (IE, I need a validation summary that ONLY shows form errors - not field errors.)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我解决了这个问题。以下是一些提示:
在我的 xaml 的 .cs 代码中,我通过对对象调用 TryValidate 来验证对象。如果您不使用内置的 DataForm 命令按钮,这不是一个好主意。如果使用您自己的自定义按钮,请务必调用 DataForm.ValidateItem() 方法,以确保所有错误都正确输出到 ValidationSummary。 (就我而言,由于我没有表单进行验证,因此没有任何内容进入验证摘要。)
您可以告诉 ValidationSummary 仅显示对象级别错误。 (属性错误仍然显示,但仅在字段上。)要像这样分配:
myDataform.ValidationSummary.Filter = ValidationSummaryFilters.ObjectErrors;
有关详细信息,请参阅 ValidationSummaryFilters。
这对我有用。我的对象验证将显示在验证摘要中,而属性错误仅显示在属性上。现在,我只需按照我想要的方式设置验证摘要的样式,使其更好地适合我的设计。
Ok, I solved this problem. Here are some tips:
In my xaml's .cs code, I was validating my object by calling TryValidate on my object. This isn't a good idea if you are NOT using the built in DataForm command buttons. If using your own custom buttons then be sure to call the DataForm.ValidateItem() method to make sure all of the errors get outputted to the ValidationSummary properly. (in my case, nothing was ever making it to the validationsummary since I wasn't have the form do the validation.)
You can tell the ValidationSummary to only show object level errors. (Property errors still show up, but only on the field.) To do this assign like so:
myDataform.ValidationSummary.Filter = ValidationSummaryFilters.ObjectErrors;
See the ValidationSummaryFilters for more info.
This worked for me. My object validation would show up in the validation summary and the property errors only on the properties. Now I just need to style the validation summary the way I want to make it work better for my design.