1

I have a form with inputs that do not really belong inside any model. Things like "confirm password" and "I accept these conditions" etc.

What is the best practice for situations such as this?

I thought about the following possibilities:

  1. Create a temporary model inside the controller action with validation rules.
  2. Create a separate model for these inputs.
  3. Some other feature in cake 2.x specifically for this situation?

I've read many answer posts about this but either the answer is for v1.x of cake and might be outdated, or people propose to put all that stuff inside the model with the closest relationship to the current controller. So what is the best practice?

Thanks!

4

1 回答 1

4

我为此使用行为。

密码添加/编辑: https ://github.com/dereuromark/tools/blob/master/src/Model/Behavior/PasswordableBehavior.php (见https://www.dereuromark.de/2011/08/25/working- with-passwords-in-cakephp/ )

接受条件: https ://github.com/dereuromark/tools/blob/master/src/Model/Behavior/ConfirmableBehavior.php (见https://www.dereuromark.de/2011/07/05/introducing-two- cakephp-行为/ )

这使我可以使用 DRY 方法,而无需在我使用它们的不同模型中再次重复它。我只是动态添加它们($this->Behaviors->load())或通过$actsAs,并且可以使用扩展功能(类似于 PHP5.4 中的 Traits)。

您可以将密码验证内容放入单个应用程序的 APP 用户模型中。但是,一旦您维护多个应用程序,代码就必须在某些时候重复。这就是为什么我更喜欢可行为的方法。但在某些情况下,仅仅放弃各自模型中的验证既不是不可能也不是不可行的。只是不要创建临时模型或其他东西。这通常是错误的方法。

于 2013-07-01T14:29:12.717 回答