我创建了一个带有"validate"函数的库,用于验证字段,在该函数中,规则会自动从配置位置检索。为了保持干净,我不使用这个"validate"函数来返回验证器对象,相反,我更喜欢这样做,并想知道这样做是否可以?否则你会怎么做?
//微控制器
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();
}
}
你可以在TutsPlus上找到Jeffrey Way的精彩教程。再好不过了,伊姆霍。
用模型进行验证&事件侦听器
https://tutsplus.com/lesson/validating-with-models-and-event-listeners/
验证服务
https://tutsplus.com/lesson/validation-services/