1、新增FormRequest基類
<?php
namespace App\Rules;
use Illuminate\Http\Request as IlluminateRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Container\Container;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidatesWhenResolvedTrait;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Validation\ValidationException;
class FormRequest extends IlluminateRequest implements ValidatesWhenResolved
{
use ValidatesWhenResolvedTrait;
/**
* The container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Get the validator instance for the request.
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function getValidatorInstance()
{
$factory = $this->container->make(ValidationFactory::class);
if (method_exists($this, 'validator')) {
return $this->container->call([$this, 'validator'], compact('factory'));
}
return $factory->make(
$this->validationData(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes()
);
}
/**
* Get data to be validated from the request.
*
* @return array
*/
protected function validationData()
{
return $this->all();
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Http\Exception\HttpResponseException
*/
protected function failedValidation(Validator $validator)
{
throw new ValidationException($validator);
}
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
return new JsonResponse($errors, 200);
}
/**
* Set the container implementation.
*
* @param \Illuminate\Container\Container $container
* @return $this
*/
public function setContainer(Container $container)
{
$this->container = $container;
return $this;
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages(){
return [];
}
public function rules(){
return [];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [];
}
}
2、新增表單驗證服務提供者
<?php
namespace App\Providers;
use App\Rules\FormRequest;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Laravel\Lumen\Application;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Http\Request as IlluminateRequest;
class FormRequestServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) {
$resolved->validateResolved();
});
$this->app->resolving(FormRequest::class, function (FormRequest $request, Application $app) {
$this->initializeRequest($request, $app['request']);
$request->setContainer($app);
});
}
public function register()
{
}
protected function initializeRequest(FormRequest $form, IlluminateRequest $current)
{
$files = $current->files->all();
$files = is_array($files) ? array_filter($files) : $files;
$form->initialize(
$current->query->all(),
$current->request->all(),
$current->attributes->all(),
$current->cookies->all(),
$files,
$current->server->all(),
$current->getContent()
);
$form->setJson($current->json());
if ($session = $current->getSession()) {
$form->setLaravelSession($session);
}
$form->setUserResolver($current->getUserResolver());
$form->setRouteResolver($current->getRouteResolver());
}
}
3、bootstrap/app.php中註冊服務提供者
$app->register(\App\Providers\FormRequestServiceProvider::class);
使用示例:
# Controller
public function store(TestStore $request){
}
# Rules
class TestStore extends FormRequest{
public function authorize(){
return true;
}
public fucntion rules()
{
return [
'title'=>'required'
]
}
public function messages(){
return [
'title.required'=>'標題不能為空'
]
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結