Laravel自定义验证(Validate)规则最简单方法

1、Validate

 ['integer','max:30',function($attribute,$value,$fail){
		        if ($value <= 10) {
		            $fail(':attribute must be at least 10.');
		        }
            }]
        ];
        $messages = [
        ];
        return self::compileErrMsg($data, $rules, $messages);
    }


    private static function compileErrMsg($data, $rules, $messages){
        $validator = Validator::make($data, $rules, $messages);
        if($validator->fails()){
            $result = [];
            foreach(json_decode(json_encode($validator->errors()),true) as $k => $v){
                $result[$k] = $v[0];
            }
            return $result;
            die;
        }
        return false;
    }

}

2、Controller

only(['tags']);
        if($validate_error = Test::search($input)){
            return $validate_error;
        }
        return [];
	}


}

 

 

 

 

你可能感兴趣的:(PHP,laravel)