如何将“自定义请求”验证类的响应重定向到包含错误和一些自定义数据的自定义页面。
下面的代码中的响应方法不起作用。
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class employeeStore extends FormRequest {
public function authorize() {
return true;
}
public function rules() {
return [
'contacts' => 'bail|required|array|min:2',
'contacts.*.contact_no' => 'bail|required|regex:/[7-9][0-9]{9}/',
'contacts.*.is_preferred' => 'in:true,false',
];
}
public function messages() {
return [
'contacts.min' => 'provide at-least one alternate mobile no',
'contacts.*.contact_no.required' => 'this field is required',
'contacts.*.contact_no.regex' => 'mobile number entered is not valid',
];
}
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson())
{
return new JsonResponse($errors, 422);
}
return $this->redirector->to('profile')
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
}