Slide 1

Slide 1 text

Laravel標準バリデーションでで きること まだカスタムバリデーションを作るには早いかもしれない ... BABY JOB株式会社 岡 宏信 PHPerKaigi2024 2024.03.08

Slide 2

Slide 2 text

自己紹介 名前: 岡 宏信 所属:BABY JOB株式会社 エンジニア歴:5年目 趣味:弾き語り

Slide 3

Slide 3 text

すべての人が子育てを楽しいと思える社会 乳児期 幼児期 学童期 妊娠・出産 産後うつ 保活が大変 いやいや期 学童不足 小一の壁 子育てには課題がたくさん・・・ 育児と子育て の両立が大変 保活(保育園探し)をサポート 保育園の準備をサポート

Slide 4

Slide 4 text

標準のルールで相関バリデーションを実装 フォームを実装していくなかで避けて通れないバリデーション その中でも記述量が増えがちな相関バリデーションを Laravel 標準のバリデーションルールを用いて実装する方法を ご紹介します

Slide 5

Slide 5 text

実行環境 PHP 8.2.3 Laravel 10.29.0

Slide 6

Slide 6 text

こんなフォームがあったとします

Slide 7

Slide 7 text

通知タイプは選択必須

Slide 8

Slide 8 text

メールが選ばれているときは ここが 必須にな る

Slide 9

Slide 9 text

SMSが選ばれているときは ここが 必須にな る

Slide 10

Slide 10 text

はがきが選ばれているときは ここが 必須にな る

Slide 11

Slide 11 text

選択されていない場合は各入力欄未入力可

Slide 12

Slide 12 text

各入力欄なにかしら入力形式のチェックをする

Slide 13

Slide 13 text

php artisan make:request InfoRequest まずフォームリクエスト作成

Slide 14

Slide 14 text

public function rules(): array { return [ // ]; } rules() にルールを書いていきます

Slide 15

Slide 15 text

● 選択必須 ● メール、SMS、はがき以 外は受け付けない 通知タイプ

Slide 16

Slide 16 text

'info_type' => ['required', 'in:mail,sms,postcard'] 通知タイプ

Slide 17

Slide 17 text

'info_type' => ['required', 'in:mail,sms,postcard'] 通知タイプ 選択必須

Slide 18

Slide 18 text

'info_type' => ['required', 'in:mail,sms,postcard'] 通知タイプ メール、SMS、はがき以外は受け付けない

Slide 19

Slide 19 text

メールアドレス ● メール選択時は入力必 須 ● 入力形式のチェック

Slide 20

Slide 20 text

'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default' => ['nullable', 'email'] }, メールアドレス

Slide 21

Slide 21 text

'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default' => ['nullable', 'email'] }, メールアドレス 通知タイプがメールの場合入力必須

Slide 22

Slide 22 text

'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default' => ['nullable', 'email'] }, メールアドレス それ以外の場合未入力可

Slide 23

Slide 23 text

'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default' => ['nullable', 'email'] }, メールアドレス どちらの場合でも入力形式のチェック

Slide 24

Slide 24 text

電話番号 ● SMS選択時は入力必 須 ● 入力形式のチェック

Slide 25

Slide 25 text

電話番号 'tel' => match ($this->input('info_type')) { 'sms' => ['required', 'regex:/\A0\d{9,10}\z/'], 'default' => ['nullable', 'regex:/\A0\d{9,10}\z/'] },

Slide 26

Slide 26 text

住所 ● はがき選択時は入力 必須 ● 入力形式のチェック

Slide 27

Slide 27 text

住所 'address' => match ($this->input('info_type')) { 'postcard' => ['required', 'string', 'max:255'], 'default' => ['nullable', 'string', 'max:255'] },

Slide 28

Slide 28 text

全部合わせると...

Slide 29

Slide 29 text

public function rules(): array { return [ 'info_type' => ['required', 'in:mail,sms,postcard'], 'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default' => ['nullable', 'email'] }, 'tel' => match ($this->input('info_type')) { 'sms' => ['required', 'regex:/\A0\d{9,10}\z/'], 'default' => ['nullable', 'regex:/\A0\d{9,10}\z/'] }, 'address' => match ($this->input('info_type')) { 'postcard' => ['required', 'string', 'max:255'], 'default' => ['nullable', 'string', 'max:255'] }, ]; }

Slide 30

Slide 30 text

もっとシュッと書けます

Slide 31

Slide 31 text

public function rules(): array { return [ 'info_type' => ['required', 'in:mail,sms,postcard'], 'mail' => ['required_if:info_type,mail', 'nullable', 'email'], 'tel' => ['required_if:info_type,sms', 'nullable', 'regex:/\A0\d{9,10}\z/'], 'address' => ['required_if:info_type,postcard', 'nullable', 'string', 'max:255'], ]; } シュッ

Slide 32

Slide 32 text

public function rules(): array { return [ 'info_type' => ['required', 'in:mail,sms,postcard'], 'mail' => ['required_if:info_type,mail', 'nullable', 'email'], 'tel' => ['required_if:info_type,sms', 'nullable', 'regex:/\A0\d{9,10}\z/'], 'address' => ['required_if:info_type,postcard', 'nullable', 'string', 'max:255'], ]; } 何をしたのかというと

Slide 33

Slide 33 text

public function rules(): array { return [ 'info_type' => ['required', 'in:mail,sms,postcard'], 'mail' => ['required_if:info_type,mail', 'nullable', 'email'], 'tel' => ['required_if:info_type,sms', 'nullable', 'regex:/\A0\d{9,10}\z/'], 'address' => ['required_if:info_type,postcard', 'nullable', 'string', 'max:255'], ]; } ここ!

Slide 34

Slide 34 text

required_if を使いました public function rules(): array { return [ 'info_type' => ['required', 'in:mail,sms,postcard'], 'mail' => ['required_if:info_type,mail', 'nullable', 'email'], 'tel' => ['required_if:info_type,sms', 'nullable', 'regex:/\A0\d{9,10}\z/'], 'address' => ['required_if:info_type,postcard', 'nullable', 'string', 'max:255'], ]; }

Slide 35

Slide 35 text

required_if とは? 特定のフィールドの値に基づいてフィールドが必須になる場合に使用で きます

Slide 36

Slide 36 text

'field' => 'required_if:anotherfield,value' required_if の使い方 anotherfield が value の値を持つ場合にのみ、 field が必須になる

Slide 37

Slide 37 text

required_if 以外にも ● required_unless ● required_with ● required_without などある程度のパターンをカバーできそうなルールが標準で用意され ています

Slide 38

Slide 38 text

ご清聴ありがとうございました