在哪里验证和测试用户输入日期,在控制器或模型中?

时间:2011-06-29 21:21:06

标签: model-view-controller codeigniter

清理/验证/验证用户输入数据的位置?在控制器或型号中?

3 个答案:

答案 0 :(得分:1)

我会使用该模型,以便您的验证可以重复使用。模型应该处理数据,控制器应该将其引导到需要的位置。

答案 1 :(得分:1)

在控制器中。

以这种方式查看:您的表单将使用$ _POST变量中的表单数据发布到控制器函数。您验证控制器的该功能中的数据并执行一些数据库插入或更新。然后,您将成功消息显示为视图,或者出现错误时显示失败消息。

请参阅CodeIgniter用户指南here中的表单验证教程。

答案 2 :(得分:0)

进行验证的代码必须在模型中,但对此代码的调用必须在控制器中。

这样的事情:

class MyAwesomeUserModel {
   public function isValid()
   {
       //some code to validate the user
   }
}

class MyUserController {
   public function myUserAction()
   {
      //some code to insert the input of the user in the model
      if($userModel->isValid()){
         //do nice things with the data and send some message/data to the view
      } else {
         //send 'nice' error messages to the view
      }
   }
}

这只是我使用的方式,可能不是最好的方式,但它最适合我的应用程序。这才是最重要的,你应该寻找最适合你应用的东西