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
『品質』という言葉が嫌いな理由
korimu
0
170
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
130
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
590
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
830
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
3
1.7k
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
140
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
660
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
40
15k
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
8
1.8k
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
5
390
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
150
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
8
1.1k
Featured
See All Featured
Scaling GitHub
holman
459
140k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.8k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Into the Great Unknown - MozCon
thekraken
35
1.6k
Optimizing for Happiness
mojombo
376
70k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Designing Experiences People Love
moore
140
23k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.4k
For a Future-Friendly Web
brad_frost
176
9.5k
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 などある程度のパターンをカバーできそうなルールが標準で用意され ています
ご清聴ありがとうございました