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
420
Laravel標準バリデーションでできること
フォームを実装していくなかで避けて通れないバリデーション
その中でも記述量が増えがちな相関バリデーションを
Laravel 標準のバリデーションルールを用いて実装する方法を
ご紹介します
hmb_0k
March 11, 2024
Tweet
Share
Other Decks in Programming
See All in Programming
PostmanでAPIの動作確認が楽になった話
h455h1
0
170
Snowflakeで眠ったデータを起こそう!
estie
0
120
Code Reviews
bkuhlmann
4
890
Random\Randomizer クラスで日常のあれこれを解決しよう! / Random\Randomizer class solves familiar trouble
cocoeyes02
0
250
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
430
Fragment Composition of GraphQL
quramy
7
1.1k
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
7
950
Apache Hive 4 on Treasure Data
ryukobayashi
0
360
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
350
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
470
Ruby Function Composition
bkuhlmann
1
330
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
422
63k
Navigating Team Friction
lara
178
13k
How to Ace a Technical Interview
jacobian
272
22k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
Debugging Ruby Performance
tmm1
70
11k
Designing Experiences People Love
moore
136
23k
Build The Right Thing And Hit Your Dates
maggiecrowley
24
2k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
The Cost Of JavaScript in 2023
addyosmani
16
3.9k
The Power of CSS Pseudo Elements
geoffreycrofte
60
5k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
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 などある程度のパターンをカバーできそうなルールが標準で用意され ています
ご清聴ありがとうございました