OOP表单验证,无需使用多种返回类型 [英] OOP form validation without using multiple return types

查看:46
本文介绍了OOP表单验证,无需使用多种返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以登录的程序.我有一个User类,当他们登录时,他们的所有属性(权限,用户名,真实姓名等)都保存为类属性.但是,由于需要验证所有数据,因此我难以确定最好的方法是让用户更新自己的信息.

I have a program where users can log in. I have a User class and when they log in, all of their attributes (permissions, username, real name, etc) are saved as class properties. However I am having difficulty wrapping my mind around what the best way is to have the user update their own info due to the fact that all data needs to be validated.

当前,当用户尝试更新自己的信息时,例如他们的电子邮件地址,我知道:

Currently, when a user tries to update their own information, e.g. their email address, I do:

// Set new email
$try = $user->setEmail($_POST['new_email']);
if ($try !== true) {
    $errors[] = $try;
}
...
if (isset($errors) {
    // Tell user what went wrong
} else {
    $user->saveValuesToDB();
}

它可以正常工作,但对我来说却很难看,因为User->setEmail()需要具有混合的返回类型,因为电子邮件可能由于多种原因而无效(找不到域,格式无效,提供的空字符串,已经使用由其他用户等),并且需要将确切原因回传给该用户,但据我所知,通常不建议混合使用回报.

It works fine, but seems rather ugly to me since User->setEmail() needs to have a mixed return type because the email can be invalid for a multiple number of reasons (domain not found, invalid format, empty string provided, already used by another user, etc) and the exact reason needs to be passed back to the user, but as I understand mixed returns are generally discouraged.

我想到的另一种方法是只更新所有属性,而不执行任何形式的验证,然后在最后执行User->commitAllChanges(),以执行所有验证,这将从所有setter方法并将其简化为仅commit方法,但是我真的也不喜欢这样做,因为我认为实际上设置的任何属性都应该是有效的,并且仍然不能完全解决问题.

Another way I thought of doing it is just updating all of the properties without performing any sort of validation and then doing User->commitAllChanges() at the end which would perform all of the validations, which would take the multiple returns out of all the setter method and reduce it down to only the commit method, but I really don't like this either because I feel that any properties actually being set should be valid and it still doesn't completely get rid of the problem.

我还可以使用哪些其他方法来允许用户设置对象属性并验证它们,并将任何错误发回给用户?还是我现在在做什么?

What are some other method(s) that I can use to allow the user to set object properties and also validate them, sending any error back to the user? Or is what I'm doing now fine?

推荐答案

我想到的另一种方法是更新所有 属性,而无需执行任何类型的验证,然后执行 最后是User-> commitAllChanges(),它将执行所有 验证,这样可以从所有 setter方法并将其减少为仅commit方法

Another way I thought of doing it is just updating all of the properties without performing any sort of validation and then doing User->commitAllChanges() at the end which would perform all of the validations, which would take the multiple returns out of all the setter method and reduce it down to only the commit method

通常,这是一个好方法.您还可以将整个过程分解为较小的部分,以使其用途更加广泛.

This is generally a good approach. You can also break down the whole process into smaller pieces so that it's more versatile.

例如,模型可能具有一个validate方法,该方法返回一个布尔值(一切正常,还是有一个或多个错误?),以及一个getValidationErrors方法,该方法返回一个保存所有信息的数组您关心的问题(可能需要是多维的).它可能有一个commit方法,该方法会自动调用validate,并且仅在根本没有错误的情况下才保留更改.

For example, the model might have a validate method that returns a single boolean (is everything OK, or is there one or more errors?), and a getValidationErrors method that returns an array which holds all the information you care about (would probably need to be multidimensional). It could have a commit method that automatically calls validate and only persists the changes if there are no errors at all.

但是我真的不喜欢这样,因为我觉得任何特性 实际上被设置应该是有效的,但仍然没有完全得到 摆脱问题.

but I really don't like this either because I feel that any properties actually being set should be valid and it still doesn't completely get rid of the problem.

不会被设置"的属性是IMO 方式,比人们最初想象的要麻烦得多.主要问题是,如果要设置属性(通过返回一些错误代码,或者更好地是,通过引发异常),则您必须使用保护性"代码包装对它的所有访问权限 .这很快就变得非常乏味.

Properties "that just won't be set" are IMO way more trouble than one might initially think. The major problem is that if a property objects to being set (either by returning some error code or, much better, by throwing an exception) then you have to wrap all access to it with "protective" code. This gets very tedious very fast.

实际上,通过助手方法将值大规模分配给所有属性然后查看结果是更加方便的(例如,通过调用上述的validate方法).

In practice, it's much more convenient to massively assign values to all of the properties in one go through a helper method and then see what the result was (e.g. by calling a validate method as above).

此外,请不要忘记,在实践中,您想在数据输入字段旁边显示验证错误,并且这些字段已连接为预先填充有模型属性的值.因此,一方面,这些属性需要有效,另一方面,它们需要与用户输入的内容完全匹配(否则,用户将想要杀死您).因此,最好放宽必须始终有效"的约束,以使用实用程序-尽管如今使用AJAX表单和客户端验证的情况越来越多,但这已变得不那么重要了.

Also, do not forget that in practice you 'd want to display validation errors next to the data input fields, and those fields are wired to be pre-populated with the values of your model's properties. So on the one hand the properties need to be valid, and on the other hand they need to match exactly what the user entered (otherwise the users will want to kill you). It's therefore better to relax the "must always be valid" constraint in favor of utility -- although these days with AJAX forms and client-side validation everywhere, this is becoming less relevant.

这篇关于OOP表单验证,无需使用多种返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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