2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

株式会社やどかりAdvent Calendar 2023

Day 12

確認画面ありのフォームで入力に戻る際に引き継げていないold値を強制的に持たせる

Posted at

通常の戻るボタンの作成

// blade
<div class="col-sm-3">
    <button class="btn btn-block btn-secondary" type="submit" name='back' value="back">戻る</button>
</div>

// controller
if (!empty($request['back'])) {
  return redirect()->route('〇〇〇.×××.create')->withInput();
} 

上記の記述で登録フォームと確認画面のネーム属性が一緒であれば入力された内容を再び登録画面のinputに設置できます。

登録フォームと確認画面でnameが一部違った場合

// blade
<div class="col-sm-3">
    <button class="btn btn-block btn-secondary" type="submit" name='back' value="back">戻る</button>
</div>
// ↑ここまでは一緒↑ //

// controller
if (!empty($request['back'])) {
    //ここでrequestの配列に登録フォームで使われているname属性のkey値の値を追加する
    $request->merge([
        'field1' => $request['field1_confirm'],
        'field2' => $request['field2_confirm'],
        'field3' => $request['field3_confirm'],
    ]);
  return redirect()->route('〇〇〇.×××.create')->withInput();
} 

上記のようにする事で確認画面と登録画面が違った場合でもold値を引き継げます。

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?