1
0

【Laravel】FormRequestでバリデーション前に入力値を加工

Posted at

環境

Laravel10
Windows11

目的

Postmanでform-dataを使用し、Requestを送信。受け取った値を加工したい。

・string型をint型へ変換
・array配列の中身をint型へ変換

公式ドキュメント

prepareForValidation()

prepareForValidation()を使用することで、バリデーション前にデータを加工できる。

TestRequest.php

protected function prepareForValidation()
{
    $this->merge([
        'subject_id' => (int) $this->subject,
        'class_ids' => array_map('intval', $this->class_ids),
        'public_scope' => (bool) $this->public_scope
    ]);
}

参考記事

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0