dounao5856 2014-02-19 23:08
浏览 44

验证技术 - 开放讨论

So searched the internet and stackoverflow to find anything related that could have been discussed before or any article, i was unable to find any, so i found myself creating the topic.

I am working on a project where i make a lot of ajax request on POST. Therfor validation plays an important role as it MUST be.

So i created some Validation functions as listed below

   //only numeric (integer) regexp [1-9]
   Validation::isInteger($a);
   //numeric and alphabetic (integer&string) regexp [a-z1-9]
   Validation::isAlphaNum($c);
   //(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday) Regexp
   Validation::isDay($d);
   //Array numeric (integer) Regexp
   Validation::isArrayNum($sortTo);

I used it in my code like this, But this seems so ugly! in Model:

 if(Validation::isInteger($a))
 {
   if(Validation::isAlphaNum($c))
   {
     if(Validation::isDay($d))
     {
       if(Validation::isArrayNum($sortTo))
       {
          //do process here!
 }}}}

Then i thought about doing validation like this maybe; Not sure if its safe! in Model:

   if(!Validation::isInteger($a) &&
      !Validation::isAlphaNum($c) &&            
      !Validation::isDay($d) && 
      !Validation::isArrayNum($sortTo)) exit;

      //do process here anyway without "if" closure if you dont pass validation above you won't see me!

My questions are:

  1. Should i validate data in Controller or model? Does it matter?
  2. Is my second approach safe?
  3. How do you validate your data? (whats your design of code)
  4. What other methods/techniques i can use to prettify this uglyness.

NOTE: i am not too concerned about throwing errors due to validation fails.

EDIT:(23.02.2014) However i found a video that has great explanations about Validation by phpacademy in youtube.com if you are having same kind of problems with validation or want to have an idea i really recommend you watch it. http://www.youtube.com/watch?v=rWon2iC-cQ0&index=13&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc

  • 写回答

1条回答 默认 最新

  • duanmei2459 2014-02-19 23:51
    关注

    Many frameworks introduce the concept of model validation as its own class or module. For example you have a model and you have validation rules against that model. The task of validating a model based on rules can live within a high level validation class. The rules themselves can live within their own classes as well. Frameworks such as Symfony2 allow validation rules on models via config or annotations. It would be well worth the time to research how Symfony2 uses validation.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么