有验证用户输入的最佳实践方法吗? [英] Is there a best practice way to validate user input?

查看:57
本文介绍了有验证用户输入的最佳实践方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有最佳实践来验证用户输入?

实际问题:

用户在窗口中提供某些输入.完成这些输入后,他可以单击创建".现在,应该显示一个弹出消息,并给出所有无效输入.如果没有无效输入,则继续.

A user gives certain inputs in a window. When he is done with those inputs, he can click 'create'. Now, a pop up message should be shown with all invalid input given. If no invalid input, then just continue.

我可以在Form类中轻松地做到这一点.但是我记得在set属性中验证输入的一些最佳实践方法.问题是,如果我通过这种方式进行验证,那么我已经创建了该类的实例(否则,将无法设置属性;)).那应该不会发生,除非输入有效,否则无法创建该类的实例.

I could easily do this in the Form class. But I remember some best practice way of validating the input in the set properties. Problem is that I already created an instance of that class (or otherwise, can't set properties ;) ) if I validate this way. That should not happen, no instance of the class may be created unless input is valid.

我打算创建一个ErrorMessages类,该类包含一个列表,我可以在其中放置所有errorMessages.每次给出无效输入时,都会向errorMessages列表中添加一条新消息.因此,如果用户单击创建"按钮,则会显示列表中的所有消息.这是处理事情的好方法吗?

I was planning to create a ErrorMessages class that contains a list where I can put all errorMessages. Every time an invalid input is given, a new message is added to the errorMessages list. So if user click's 'create' button, all messages in the list are shown. Is this a good way of handling things?

那么有没有最佳实践方法?有提供这种解决方案的设计模式吗?

So is there a best practice way? Any design patterns that provide such solution?

编辑:这是一项学校任务.因此具有不合逻辑的要求.单击创建"时,我必须显示所有无效输入.我想在Form类之外执行此操作. (因此,即使没有GUI也可以进行验证,这时我什至还没有创建GUI).首先确保我的功能正常;).我想保持我的代码干净,抽象和面向对象.那么我应该如何显示我的错误消息?

This is a school task. So with illogical requirements. I HAVE to show all invalid inputs when I click 'create'. I would like to do this out of Form class. (So validation works even without GUI, I did't even create the GUI yet at this point). First making sure my functionality works correctly ;). I want to keep my code clean, abstract and OOP. So how should I show my error messages?

推荐答案

我打算创建一个ErrorMessages类,该类包含一个列表,我可以在其中放置所有errorMessages.每次给出无效输入时,都会向errorMessages列表中添加一条新消息.因此,如果用户单击创建"按钮,则会显示列表中的所有消息.这是处理事情的好方法吗?

I was planning to create a ErrorMessages class that contains a list where I can put all errorMessages. Every time an invalid input is given, a new message is added to the errorMessages list. So if user click's 'create' button, all messages in the list are shown. Is this a good way of handling things?

主观上,我认为最好提供即时反馈,使用户输入的值无效.这样,他们可以立即返回并对其进行修复.

Subjectively, I think it would be better to provide instant feedback that the value the user entered is invalid. That way, they can immediately go back and fix it.

我的意思是,考虑一下.您提出的方法将在字面上给他们提供大量的问题清单,这不是非常用户友好的.此外,他们将如何记住所有这些问题,以便能够一次解决这些问题? (提示:不是.)

I mean, think about it. The approach you propose would literally give them a giant list of problems at the end, which is not very user-friendly. Besides, how are they going to remember all of those problems to be able to go back and fix them one at a time? (Hint: they're not.)

相反,我建议使用 ErrorProvider以在相应控件旁边显示任何错误.我在回答此处.

Instead, I recommend using the ErrorProvider class to display any errors right next to the appropriate control. I talked a little bit more about this approach in my answer here and here.

当然,您仍然需要确保在最终提交(单击确定/提交"按钮)后所有输入均有效,但这只是检查是否存在任何错误的简单情况.

Of course, you'll still need to make sure upon final submission (clicking the OK/Submit button) that all the input is valid, but then that's just a simple case of checking for the presence of any errors.

我可以在Form类中轻松地做到这一点.但是我记得在set属性中验证输入的一些最佳实践方法.

I could easily do this in the Form class. But I remember some best practice way of validating the input in the set properties.

是的,这里的想法是封装. Form类应该只知道表单内容.不需要知道哪种输入对您所有不同的控件都无效.

Yes, the idea here is encapsulation. The Form class should only know about form stuff. It shouldn't be required to know what kind of input is/is not valid for all of your different controls.

相反,此验证逻辑应放在其他位置,例如在存储数据的类中.该类将公开公共属性以获取和设置数据,在setter方法内部,它将验证数据.

Instead, this validation logic should be placed elsewhere, such as in a class that stores your data. That class would expose public properties to get and set the data, and inside of the setter method, it would verify the data.

这意味着您的Form所要做的就是在数据类上调用setter方法.表单不需要知道如何验证数据,甚至不需要知道数据的含义,因为数据类可以处理所有这些.

That means that all your Form has to do is call a setter method on your data class. The Form needs to know nothing about how to validate the data, or even what the data means, because the data class handles all of that.

这不应该发生,除非输入有效,否则无法创建该类的实例.

That should not happen, no instance of the class may be created unless input is valid.

如果确实是这种情况,则需要为该类提供一个 constructor ,以接受其所需的所有数据作为参数.然后,构造函数的主体将验证指定的数据,并在其中任何无效的情况下引发异常.该异常将阻止创建该类,从而确保不存在包含无效数据的类实例.

If this is indeed the case, you will need to provide a constructor for the class that accepts as parameters all of the data it needs. The body of the constructor will then validate the specified data and throw an exception if any of it is invalid. The exception will prevent the class from being created, ensuring that no instance of a class that contains invalid data ever exists.

这样的类可能根本没有setter方法,只有getter.

Such a class would probably not have setter methods at all—only getters.

但是,这在C#领域是一种不寻常的要求(但是在C ++中可能很常见).通常,将验证代码放在设置器中可以很好地工作.

However, this is kind of an unusual requirement in the world of C# (however common it may be in C++). Generally, placing your validation code inside of the setters works just fine.

我的财产有一些私人塞特犬.因此,它们仅在我的数据类的构造函数中设置.现在的问题是,这似乎使我的验证不容易

My properties have some private setters. So they only get set in the constructor of my data class. Problem is now that this seems to make my validation not eassy

那为什么会改变任何东西?您仍然可以在私有设置器中处理验证.如果验证失败,则抛出异常.因为构造函数不处理异常,所以它将继续从该方法冒泡到尝试实例化该对象的代码.如果该代码要处理异常(例如向用户显示错误消息),则可以这样做.

Why would that change anything? You still handle the validation inside of the private setters. If validation fails, you throw an exception. Because the constructor doesn't handle the exception, it continues bubbling up out of that method to the code that attempted to instantiate the object. If that code wants to handle the exception (e.g., to display an error message to the user), it can do so.

当然,在输入无效的情况下引发异常不一定是最佳实践".原因是通常应为异常情况保留例外,并且用户可能会搞砸并为您提供无效数据.但是:

Granted, throwing an exception in the case of invalid input is not necessarily a "best practice". The reason is that exceptions should generally be reserved for unexpected conditions, and users screwing up and providing you with invalid data is, well, to be expected. However:

  1. 这是构造函数内部用于数据验证的唯一选项,因为构造函数无法返回值.
  2. 在UI代码中,异常处理的成本基本上可以忽略不计,因为现代计算机处理异常的速度比用户在屏幕上看到的速度更快.

这篇关于有验证用户输入的最佳实践方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆