Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Laravel標準バリデーションでできること
Search
hmb_0k
March 11, 2024
Programming
2
700
Laravel標準バリデーションでできること
フォームを実装していくなかで避けて通れないバリデーション
その中でも記述量が増えがちな相関バリデーションを
Laravel 標準のバリデーションルールを用いて実装する方法を
ご紹介します
hmb_0k
March 11, 2024
Tweet
Share
Other Decks in Programming
See All in Programming
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
120
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
190
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
780
ARA Ansible for the teams
kksat
0
150
color-scheme: light dark; を完全に理解する
uhyo
3
310
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
160
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
390
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
590
法律の脱レガシーに学ぶフロントエンド刷新
oguemon
5
740
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
560
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
昭和の職場からアジャイルの世界へ
kumagoro95
1
380
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
410
Code Reviewing Like a Champion
maltzj
521
39k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Unsuck your backbone
ammeep
669
57k
GraphQLとの向き合い方2022年版
quramy
44
13k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Transcript
Laravel標準バリデーションでで きること まだカスタムバリデーションを作るには早いかもしれない ... BABY JOB株式会社 岡 宏信 PHPerKaigi2024 2024.03.08
自己紹介 名前: 岡 宏信 所属:BABY JOB株式会社 エンジニア歴:5年目 趣味:弾き語り
すべての人が子育てを楽しいと思える社会 乳児期 幼児期 学童期 妊娠・出産 産後うつ 保活が大変 いやいや期 学童不足 小一の壁
子育てには課題がたくさん・・・ 育児と子育て の両立が大変 保活(保育園探し)をサポート 保育園の準備をサポート
標準のルールで相関バリデーションを実装 フォームを実装していくなかで避けて通れないバリデーション その中でも記述量が増えがちな相関バリデーションを Laravel 標準のバリデーションルールを用いて実装する方法を ご紹介します
実行環境 PHP 8.2.3 Laravel 10.29.0
こんなフォームがあったとします
通知タイプは選択必須
メールが選ばれているときは ここが 必須にな る
SMSが選ばれているときは ここが 必須にな る
はがきが選ばれているときは ここが 必須にな る
選択されていない場合は各入力欄未入力可
各入力欄なにかしら入力形式のチェックをする
php artisan make:request InfoRequest まずフォームリクエスト作成
public function rules(): array { return [ // ]; }
rules() にルールを書いていきます
• 選択必須 • メール、SMS、はがき以 外は受け付けない 通知タイプ
'info_type' => ['required', 'in:mail,sms,postcard'] 通知タイプ
'info_type' => ['required', 'in:mail,sms,postcard'] 通知タイプ 選択必須
'info_type' => ['required', 'in:mail,sms,postcard'] 通知タイプ メール、SMS、はがき以外は受け付けない
メールアドレス • メール選択時は入力必 須 • 入力形式のチェック
'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'
=> ['nullable', 'email'] }, メールアドレス
'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'
=> ['nullable', 'email'] }, メールアドレス 通知タイプがメールの場合入力必須
'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'
=> ['nullable', 'email'] }, メールアドレス それ以外の場合未入力可
'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'
=> ['nullable', 'email'] }, メールアドレス どちらの場合でも入力形式のチェック
電話番号 • SMS選択時は入力必 須 • 入力形式のチェック
電話番号 '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'] },
全部合わせると...
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'] }, ]; }
もっとシュッと書けます
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'], ]; } シュッ
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'], ]; } 何をしたのかというと
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'], ]; } ここ!
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'], ]; }
required_if とは? 特定のフィールドの値に基づいてフィールドが必須になる場合に使用で きます
'field' => 'required_if:anotherfield,value' required_if の使い方 anotherfield が value の値を持つ場合にのみ、 field
が必須になる
required_if 以外にも • required_unless • required_with • required_without などある程度のパターンをカバーできそうなルールが標準で用意され ています
ご清聴ありがとうございました