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
SIMD Parallel Programming with the Vector API
josepaumard
0
180
Hanami and htmx
bkuhlmann
0
210
Node.js v22 で変わること
yosuke_furukawa
PRO
9
3.5k
⼤規模⾔語モデルの拡張(RAG)が 終わったかも知れない件について
nearme_tech
23
15k
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
420
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
27
8.3k
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
150
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
340
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
220
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
130
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
"config" ってなんだ? / What is "config"?
okashoi
0
240
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
44
9.7k
Rebuilding a faster, lazier Slack
samanthasiow
73
8.2k
Web Components: a chance to create the future
zenorocha
305
41k
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Statistics for Hackers
jakevdp
789
220k
A Philosophy of Restraint
colly
197
16k
Code Reviewing Like a Champion
maltzj
514
39k
Agile that works and the tools we love
rasmusluckow
325
20k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
17
1.4k
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 などある程度のパターンをカバーできそうなルールが標準で用意され ています
ご清聴ありがとうございました