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
昭和の職場からアジャイルの世界へ
kumagoro95
1
410
Ruby on cygwin 2025-02
fd0
0
150
GAEログのコスト削減
mot_techtalk
0
120
ML.NETで始める機械学習
ymd65536
0
210
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
210
Introduction to kotlinx.rpc
arawn
0
740
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
150
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
130
CloudNativePGを布教したい
nnaka2992
0
100
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
140
CI改善もDatadogとともに
taumu
0
180
GitHub Actions × RAGでコードレビューの検証の結果
sho_000
0
280
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Site-Speed That Sticks
csswizardry
4
390
A better future with KSS
kneath
238
17k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.3k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
980
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
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 などある程度のパターンをカバーできそうなルールが標準で用意され ています
ご清聴ありがとうございました