input type="date"を使って生年月日を受け取ろうとしたのですが、Androidユーザーからの評判が悪く(年の選択が大変)、年・月・日それぞれのinput type="number"を作ることにしました。受け取った'year', 'month', 'date'を'year-month-date'の形にしたいのですがどのような方法が良いでしょう。
バリデーションの際とDB書き込みの際に使うことに加えて、今後の機能追加で他のコントローラーから呼び出すことも考えています。
Middlewareを使おうとしたら"Target class [RegisterMiddleware] does not exist."
RegisterController
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\Http\Controllers\Controller; 6use App\Providers\RouteServiceProvider; 7use App\User; 8use Carbon\Carbon; 9use Illuminate\Foundation\Auth\RegistersUsers; 10use Illuminate\Support\Facades\Hash; 11use Illuminate\Support\Facades\Validator; 12 13class RegisterController extends Controller 14{ 15 /* 16 |-------------------------------------------------------------------------- 17 | Register Controller 18 |-------------------------------------------------------------------------- 19 | 20 | This controller handles the registration of new users as well as their 21 | validation and creation. By default this controller uses a trait to 22 | provide this functionality without requiring any additional code. 23 | 24 */ 25 26 use RegistersUsers; 27 28 /** 29 * Where to redirect users after registration. 30 * 31 * @var string 32 */ 33 protected $redirectTo = RouteServiceProvider::HOME; 34 35 /** 36 * Create a new controller instance. 37 * 38 * @return void 39 */ 40 public function __construct() 41 { 42 $this->middleware('guest'); 43 $this->middleware('RegisterMiddleware'); 44 } 45 46 /** 47 * Get a validator for an incoming registration request. 48 * 49 * @param array $data 50 * @return \Illuminate\Contracts\Validation\Validator 51 */ 52 protected function validator(array $data) 53 { 54 return Validator::make($data, [ 55 'uname' => ['required', 'string', 'max:255'], 56 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 57 'birthday' => ['required', 'date_format:"Y-m-d"'], 58 'password' => ['required', 'string', 'min:8', 'confirmed'], 59 ]); 60 } 61 62 /** 63 * Create a new user instance after a valid registration. 64 * 65 * @param array $data 66 * @return \App\User 67 */ 68 protected function create(array $data) 69 { 70 $ref_date = Carbon::createFromDate('1921-12-26'); 71 $birthday = Carbon::createFromDate($data['birthday']); 72 $interval = $ref_date->diffInDays($birthday); 73 $acode = $interval % 60; 74 75 return User::create([ 76 'uname' => $data['uname'], 77 'birthday' => $data['birthday'], 78 'acode' => $acode, 79 'comment' => $data['comment'], 80 'name_shown' => !isset($data['name_hidden']), 81 'type_shown' => $data['type_shown'], 82 'email' => $data['email'], 83 'password' => Hash::make($data['password']), 84 ]); 85 } 86}
RegisterMiddleware
1<?php 2 3namespace App\Http\Middleware; 4 5use Closure; 6 7class RegisterMiddleware 8{ 9 /** 10 * Handle an incoming request. 11 * 12 * @param \Illuminate\Http\Request $request 13 * @param \Closure $next 14 * @return mixed 15 */ 16 public function handle($request, Closure $next) 17 { 18 if ($data['bday-month'] < 10) { 19 $data['bday-month'] = 0 . $data['bday-month']; 20 } 21 if ($data['bday-day'] < 10) { 22 $data['bday-day'] = 0 . $data['bday-day']; 23 } 24 25 $data['birthday'] = $data['bday-year'] . '-' . $data['bday-month'] . '-' . $data['bday-day']; 26 27 return $next($request); 28 } 29} 30
FormRequestを使おうとしたが、呼び出せているのか?"The birthday field is required."のバリデーションエラー。
RegisterController
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\Http\Controllers\Controller; 6use App\Http\Requests\RegisterRequest; 7use App\Providers\RouteServiceProvider; 8use App\User; 9use Carbon\Carbon; 10use Illuminate\Foundation\Auth\RegistersUsers; 11use Illuminate\Support\Facades\Hash; 12use Illuminate\Support\Facades\Validator; 13 14class RegisterController extends Controller 15{ 16 /* 17 |-------------------------------------------------------------------------- 18 | Register Controller 19 |-------------------------------------------------------------------------- 20 | 21 | This controller handles the registration of new users as well as their 22 | validation and creation. By default this controller uses a trait to 23 | provide this functionality without requiring any additional code. 24 | 25 */ 26 27 use RegistersUsers; 28 29 /** 30 * Where to redirect users after registration. 31 * 32 * @var string 33 */ 34 protected $redirectTo = RouteServiceProvider::HOME; 35 36 /** 37 * Create a new controller instance. 38 * 39 * @return void 40 */ 41 public function __construct() 42 { 43 $this->middleware('guest'); 44// $this->middleware('RegisterMiddleware'); 45 } 46 47 /** 48 * Get a validator for an incoming registration request. 49 * 50 * @param array $data 51 * @return \Illuminate\Contracts\Validation\Validator 52 */ 53 protected function validator(array $data) 54 { 55 return Validator::make($data, RegisterRequest::$rules); 56 } 57 58 /** 59 * Create a new user instance after a valid registration. 60 * 61 * @param array $data 62 * @return \App\User 63 */ 64 protected function create(array $data) 65 { 66 $ref_date = Carbon::createFromDate('1921-12-26'); 67 $birthday = Carbon::createFromDate($data['birthday']); 68 $interval = $ref_date->diffInDays($birthday); 69 $acode = $interval % 60; 70 71 return User::create([ 72 'uname' => $data['uname'], 73 'birthday' => $data['birthday'], 74 'acode' => $acode, 75 'comment' => $data['comment'], 76 'name_shown' => !isset($data['name_hidden']), 77 'type_shown' => $data['type_shown'], 78 'email' => $data['email'], 79 'password' => Hash::make($data['password']), 80 ]); 81 } 82}
RegisterRequest
1<?php 2 3namespace App\Http\Requests; 4 5use Illuminate\Foundation\Http\FormRequest; 6 7class RegisterRequest extends FormRequest 8{ 9 /** 10 * Determine if the user is authorized to make this request. 11 * 12 * @return bool 13 */ 14 public function authorize() 15 { 16 return true; 17 } 18 19 public static $rules = [ 20 'uname' => ['required', 'string', 'max:255'], 21 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 22 'birthday' => ['required', 'date_format:"Y-m-d"'], 23 'password' => ['required', 'string', 'min:8', 'confirmed'], 24 ]; 25 26 27 /** 28 * Get the validation rules that apply to the request. 29 * 30 * @return array 31 */ 32 public function rules() 33 { 34 return self::$rules; 35 } 36}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/05 09:34
2020/05/22 03:03