【问题标题】:create static variable to hold Validator in Laravel在 Laravel 中创建静态变量来保存 Validator
【发布时间】:2013-07-23 00:21:48
【问题描述】:

我创建了一个带有“验证”函数的库,用于验证我的字段,在该函数中,规则会自动从配置位置检索。为了保持清洁,我不使用这个“验证”函数来返回验证器对象,而是我更喜欢这样做,并且想知道这样做是否可以?否则你会怎么做?

// 我的控制器

if(!My_val::validate($input))
  return $this->response(My_val::$val->messages()->first());

// 我的图书馆

class My_val {

  public static $val;

  public function __construct() {
    $val= null;
  }

public static function validate($data) {
    // commented out section ---- here the rules are extracted from a config file
    self::$val= Validator::make($data);
    return self::$val->passes();
  }
}

【问题讨论】:

    标签: php static laravel laravel-4


    【解决方案1】:

    您可以在 TutsPlus 找到 Jeffrey Way 的精彩教程。 不能做得更好,恕我直言。

    使用模型和事件侦听器进行验证
    https://tutsplus.com/lesson/validating-with-models-and-event-listeners/

    验证服务
    https://tutsplus.com/lesson/validation-services/

    【讨论】:

      猜你喜欢
      • 2020-03-29
      • 2011-11-20
      • 1970-01-01
      • 2021-08-23
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多