验证域对象设置器中的表单输入吗? [英] Validate form input in Domain Objects setters?

查看:57
本文介绍了验证域对象设置器中的表单输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我开始学习MVC以来,我一直在控制器中验证我的表单数据,这是我在浏览CodeIgniters代码时养成的习惯,但是我了解到它执行某些操作的方式并不是最好的,只是完成工作.

Since I got into learning about MVC I have always validated my form data in my controllers which is a habit I picked up while skimming through CodeIgniters code but I have learned that its way of doing certain operations is not the best, it just gets the job done.

所有表单数据都应由域对象验证吗?如果可以的话,应该像这样在二传手中完成

Should all form data be validated by the domain objects? And if so should it be done in the setters like so

public function setFirstName($firstName) {

    // Check if the field was required
    if(!$firstName) {
        throw new InvalidArgumentException('The "First name" field is required');
    }

    // Check the length of the data
    // Check the format 
    // Etc etc
}

例如,如果我正在处理基本用户注册,则我的User类没有$confirmPassword属性,因此我不会这样做

Also, for example, if I am handling a basic user registration my User class does not have a $confirmPassword property so I would not be doing

$user->setConfirmPassword($confirmPassword);.

检查输入的两个密码是否相等的一种方法是设置$password并执行类似的操作

One way of checking if the two passwords entered are equal would be to set the $password and do something like

$user->setPassword($password);
if(!$user->matchPassword($confirmPassword)) {
    throw new PasswordsNotEqualException('Some message');
}

这会在我认为的服务层中完成吗?

and this would be done in the Service layer I would think?

任何能帮助我朝正确方向发展的建议都是很好的.谢谢.

Any advice to help me in the correct direction would be great. Thanks.

推荐答案

所有表单数据都应由域对象验证吗?如果是这样 应该在这样的二传手中完成

Should all form data be validated by the domain objects? And if so should it be done in the setters like so

IMO,您应该只允许创建有效的对象,而对此进行归档的最佳方法是在创建对象的方法中进行检查.

IMO you should only let creation of valid objects, and the best way to archieve this is to make those checks in the method that creates an object.

假定不能更改用户的名字,则在创建用户时将对其进行验证.这样,您就不必再使用设置器了,因为您将不再需要它.

Assuming that the first name of an user cannot be changed, you would validate that upon the user creation. This way, you forget about the setter because you won't need it anymore.

在某些情况下,您希望更改属性,并且您也需要对其进行验证(因为那样的话,更改可能导致将有效对象变成无效对象).

There may be cases when you want a property to be changed, and you would need to validate them too (because that change could lead form a valid object to an invalid object, if that's the case).

检查输入的两个密码是否相等的一种方法是 设置$ password并执行类似的操作...

One way of checking if the two passwords entered are equal would be to set the $password and do something like...

您可以用相同的方式处理此问题:拥有一个Password对象,该对象可以在创建密码时检查密码和确认密码.一个您拥有有效的Password实例,就可以使用它,知道它已通过您指定的所有验证.

You can handle this one the same way: have a Password object that checks both the password and confirmation upon its creation. One you have a valid Password instance, you can use it knowing that it passed all the validations you specified.

参考

这些设计原则(从一开始就完整而有效的对象,等等)来自于Hernan Wilkinson的巴塔哥尼亚背后的设计原则".请确保检查演示幻灯片.

Those design principles (complete and valid objects from the start, etc) are from Hernan Wilkinson's "Design Principles Behind Patagonia".Be sure to check the ESUG 2010 Video and the presentation slides.

我最近回答了另一个有关验证属性的问题,我认为您可能会派上用场: https://stackoverflow.com/a /14867390/146124

I've recently answered another question about validating properties I think you may come in handy: https://stackoverflow.com/a/14867390/146124

干杯!

这篇关于验证域对象设置器中的表单输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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