简体   繁体   English

Model-View-Presenter:验证UI

[英]Model-View-Presenter: Validate UI

I am looking for a best-practise approach on how to do UI validation in a model-view-presenter architecture. 我正在寻找一种关于如何在模型 - 视图 - 演示器架构中进行UI验证的最佳实践方法。

I need to validate some forms with a lot of controls. 我需要通过大量控件验证某些表单。 And to make it easy for the user, I store all errors or warnings with a reference to the control in a Log which is displayed to the user afterwards so that he can jump right away to the control he has to fix. 为了方便用户,我将所有错误或警告存储在日志中,并在之后显示给用户,以便他可以立即跳转到他必须修复的控件。 This is done in the view-part, which is actually wrong since validation should take place in the presenter in order to exchange the view. 这是在视图部分中完成的,这实际上是错误的,因为验证应该在演示者中进行以便交换视图。

The problem for me of doing this validation in the presenter is that it is not just checking if provided values are wrong, it also needs to check if radiobuttons have been checked which enables a textbox, which then has to contain some text for example. 我在演示者中进行此验证的问题是,它不仅仅是检查提供的值是否错误,还需要检查是否已经检查了radiobuttons,这是否启用了文本框,然后文本框必须包含一些文本。

I was thinking of using the BindingSource in the presenter since it's reflecting the UI changes and is visible to the presenter. 我正在考虑在演示者中使用BindingSource,因为它反映了UI的变化并且对于演示者是可见的。 But I am not sure if this is the right way to go (and I think it's kind of ugly)? 但我不确定这是否是正确的方法(我认为这有点难看)?

By the way: the validation takes not just place before I write to the database; 顺便说一句:在我写入数据库之前,验证不仅仅需要放置; it already takes place while the user is working on the forms. 它已经在用户处理表单时发生。

Could anyone think of a good way of doing this? 谁能想到这样做的好方法?

We finally found a solution. 我们终于找到了解决方案。 It will be done as I expected, using the DataSet in the presenter which is processed by different validator classes (one for each of our "pages"). 它将按照我的预期完成,使用演示者中的DataSet,由不同的验证器类(每个“页面”一个)处理。 The most difficult part is, when controls depend on each other (but in the end its just checking if values are set in the DataSet). 最困难的部分是,当控件相互依赖时(但最后它只是检查是否在DataSet中设置了值)。 Currently it is not possible to jump to the control to fix errors but this will be added later via Reflection by passing the name of the control to the LogEntries and the view can then figure out where this control is. 目前无法跳转到控件来修复错误,但是稍后会通过将控件的名称传递给LogEntries来反映,然后视图可以找出此控件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM