Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel標準バリデーションでできること

hmb_0k
March 11, 2024

 Laravel標準バリデーションでできること

フォームを実装していくなかで避けて通れないバリデーション
その中でも記述量が増えがちな相関バリデーションを
Laravel 標準のバリデーションルールを用いて実装する方法を
ご紹介します

hmb_0k

March 11, 2024
Tweet

Other Decks in Programming

Transcript

  1. すべての人が子育てを楽しいと思える社会 乳児期 幼児期 学童期 妊娠・出産 産後うつ 保活が大変 いやいや期 学童不足 小一の壁

    子育てには課題がたくさん・・・ 育児と子育て の両立が大変 保活(保育園探し)をサポート 保育園の準備をサポート
  2. public function rules(): array { return [ // ]; }

    rules() にルールを書いていきます
  3. 'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'

    => ['nullable', 'email'] }, メールアドレス 通知タイプがメールの場合入力必須
  4. 'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'

    => ['nullable', 'email'] }, メールアドレス それ以外の場合未入力可
  5. 'mail' => match ($this->input('info_type')) { 'mail' => ['required', 'email'], 'default'

    => ['nullable', 'email'] }, メールアドレス どちらの場合でも入力形式のチェック
  6. 住所 'address' => match ($this->input('info_type')) { 'postcard' => ['required', 'string',

    'max:255'], 'default' => ['nullable', 'string', 'max:255'] },
  7. 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'] }, ]; }
  8. 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'], ]; } シュッ
  9. 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'], ]; } 何をしたのかというと
  10. 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'], ]; } ここ!
  11. 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'], ]; }