Laravel 4 模型验证与控制器验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17401456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:03:58  来源:igfitidea点击:

Laravel 4 Model validation vs Controller validation

laravellaravel-4

提问by Slava V

It seems like official way to validate models in Laravel 4 is through Validatorin Controller? Can somebody point out why is it so?

这似乎是为了验证模型的正式方式Laravel 4是ValidatorController?有人能指出为什么会这样吗?

Wouldn't it make more sense to implement validation in Model?

在 中实现验证不是更有意义Model吗?

回答by Niklas Modess

I prefer the Ardentpackage for making validation of models as smooth and minimal as possible. To me it makes more sense to have the validation rules in the model as well.

我更喜欢使用Ardent包来使模型验证尽可能平滑和最小化。对我来说,在模型中也有验证规则更有意义。

It will return false when $model->save()is called and validation fails, then you can get the error messages through $model->errors()->all()for example.

$model->save()被调用并且验证失败时它将返回false ,然后您可以通过$model->errors()->all()例如获取错误消息。

回答by Nico Kaag

It does make sense to have validation in the models, but this validation should only be there to make sure you don't save any corrupt data.

在模型中进行验证确实有意义,但这种验证应该只用于确保您不保存任何损坏的数据。

The Validatoris in the Controllerbecause it's used to handle Input, and generate Output. If you would do the validation in the Modelthen you either have to return false, and show the user the most random of error messages about invalid data. You could also return some kine of array containing all the errors that are generated, but that's something a Model shouldn't do. Or you could throw an Exception, which is something that should be done when a model tries to consume invalid data, but it kills the application, which is not the wanted solution for a form validator.

ValidatorController因为它用于处理输入,并生成输出。如果您要在 中进行验证,Model那么您要么必须返回 false,并向用户显示有关无效数据的最随机的错误消息。您还可以返回一些包含生成的所有错误的数组,但这是模型不应该做的事情。或者你可以抛出一个异常,这是当模型尝试使用无效数据时应该做的事情,但它会杀死应用程序,这不是表单验证器想要的解决方案。

When doing the form validation in the Controller, you can do everything you want with the error messages, without changing the purpose of a Model.

在 Controller 中进行表单验证时,您可以使用错误消息执行您想要的任何操作,而无需更改模型的用途。

And in your model you can do a validation to make sure you didn't make a mistake, which will corrupt your database. Because if this happens the application should shut down.

在您的模型中,您可以进行验证以确保您没有犯错误,这会破坏您的数据库。因为如果发生这种情况,应用程序应该关闭。

So to put this in a real answer to your question: Validation in the model makes sense to avoid corrupt data, but if you want to give feedback to the user about invalid input, it should be in the controller.

因此,要真正回答您的问题:模型中的验证对于避免损坏数据很有意义,但是如果您想向用户提供有关无效输入的反馈,则应该在控制器中进行。

回答by Guy Incognito

I wrestled with this for a while and settled on handling most of my validation in a validation service, based something along the lines of this. I can then have different validation rules based on the context.

我为此纠结了一段时间,并决定在验证服务中处理我的大部分验证,基于this 的一些东西。然后我可以根据上下文使用不同的验证规则。

As Nico mentions, validation in the model is good to avoid corrupt data, but I prefer thin controllers so I pass the functionality that would sit in controller into the service. This also has the benefit of being able to reuse the validation in different controllers/methods.

正如 Nico 提到的,模型中的验证有助于避免损坏的数据,但我更喜欢瘦控制器,因此我将位于控制器中的功能传递给服务。这还有一个好处是能够在不同的控制器/方法中重用验证。

回答by Sabrina Leggett

Why I prefer In-Model Validation: I've worked with both styles and each have pluses and minuses, but I prefer in-model validation. In our current app I don't see in-controller validation as an option, since we're altering our data in so many places (dedicated forms, inline edit, bulk edit, bulk upload, api, etc). I've never really worked with validation services (though they might be an option) but I personally like to keep logic as close to the model as possible, that way I know another developer won't bypass it. I also don't like adding lots of extra files on top of the MVC and basic Libraries folder because it just seems like more you have to think about organizing properly.

为什么我更喜欢模型内验证:我使用过两种风格,每种风格都有优点和缺点,但我更喜欢模型内验证。在我们当前的应用程序中,我没有将控制器内验证视为一个选项,因为我们在很多地方(专用表单、内联编辑、批量编辑、批量上传、api 等)更改了我们的数据。我从未真正使用过验证服务(尽管它们可能是一种选择),但我个人喜欢使逻辑尽可能接近模型,这样我知道其他开发人员不会绕过它。我也不喜欢在 MVC 和基本库文件夹之上添加大量额外文件,因为看起来您必须更多地考虑正确组织。

Issues with In-Model Validation: These are some things you need to consider to make In-Model work well. SOME OF THESE ARE ALREADY TAKEN INTO CONSIDERATION BY PLUGINS. I think other frameworks (CakePHP) already deal with these, but Laravel doesn't really.

模型内验证的问题:这些是您需要考虑的一些事情,以使模型内工作良好。其中一些已被插件考虑在内。我认为其他框架 (CakePHP) 已经处理了这些,但 Laravel 并没有。

  1. Values that will be validated but not saved to the db (e.g., "accepted_agreement").
  2. Many to many or belongs to many relationships
  3. Setting conditional defaults (not critical but might want to think about at the same time)
  4. Various form scenarios - sometimes you might need different validation depending upon which form submits it. The form reference can be a purgeable attribute for validation maybe?
  5. How will you get back error messages (all in-model validation plugins handle this for you)
  6. Different validation rulesets. Draft creation vs "real" creation. (Most handle this for you)
  1. 将被验证但不保存到数据库的值(例如,“accepted_agreement”)。
  2. 多对多或属于多关系
  3. 设置条件默认值(不重要但可能同时考虑)
  4. 各种表单场景 - 有时您可能需要不同的验证,具体取决于提交的表单。表单引用可以是用于验证的可清除属性吗?
  5. 您将如何获取错误消息(所有模型内验证插件都会为您处理此问题)
  6. 不同的验证规则集。草稿创作与“真实”创作。(大多数为你处理这个)

Ultimately, for simple applications that don't have lots of ways of interacting with the model, I'd say controller validation maybe simpler, other then that I prefer in-model.

最终,对于没有很多与模型交互方式的简单应用程序,我会说控制器验证可能更简单,除此之外我更喜欢模型内。