質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

2610閲覧

Inertia.jsでフォーム送信後にモーダルポップが表示される

退会済みユーザー

退会済みユーザー

総合スコア0

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2021/07/08 12:30

編集2021/07/08 13:03

Inertia.jsでフォーム送信を行い、処理が成功した後に以下のようなモーダルポップが表示されます。
モーダルの中にはコントローラー側で返されたJSONが記述されています。
モーダルの表示を防ぐ方法はありますか?

Laravel Breezeを備えたLaravel8を使用しています。

イメージ説明

submit()でフォーム送信

Javascript

1 import NavBar from '@/Components/NavBar'; 2 import SelectBox from '@/Components/SelectBox'; 3 import BreezeInput from '@/Components/Input'; 4 import BreezeLabel from '@/Components/Label' 5 import BreezeButton from '@/Components/Button'; 6 import BreezeValidationErrors from '@/Components/ValidationErrors' 7 8 export default { 9 props: { 10 auth : Object, 11 status : Number, 12 message : String, 13 errors: Object, 14 }, 15 16 data() { 17 return { 18 19 category : { 20 1 : '質問・意見', 21 2 : '記事の修正依頼', 22 3 : 'バグ報告', 23 4 : 'その他' 24 }, 25 26 form : this.$inertia.form({ 27 mailAddress : '', 28 category : 1, 29 body : '' 30 }), 31 } 32 }, 33 34 components : { 35 NavBar, 36 SelectBox, 37 BreezeInput, 38 BreezeButton, 39 BreezeLabel, 40 BreezeValidationErrors 41 }, 42 43 methods : { 44 45 submit () { 46 this.form.post(this.route('contact.post')); 47 }, 48 }, 49 50 }

post()で処理

PHP

1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Illuminate\Support\Facades\DB; 7use Illuminate\Support\Facades\Notification; 8use Illuminate\Support\Facades\Redirect; 9use App\Models\User; 10use Inertia\Inertia; 11use DateTime; 12 13use App\Notifications\Contact\Mail; 14use App\Http\Requests\Contact\PostRequest; 15 16class ContactController extends Controller 17{ 18 /** 19 * ホーム画面表示 20 * 21 * @return \Illuminate\View\View 22 */ 23 public function show () 24 { 25 return Inertia::render('Contact'); 26 } 27 28 /** 29 * 投稿処理 30 * 31 * @param App\Http\Requests\Contact\PostRequest $request 32 * @return \Illuminate\Http\Response 33 */ 34 public function post (PostRequest $request) 35 { 36 37 Notification::send(User::find(1), new Mail()); 38 39 DB::table('contacts')->insert([ 40 'category' => $request->input('category'), 41 'mail_address' => $request->input('mailAddress'), 42 'body' => $request->input('body'), 43 'created_at' => new DateTime() 44 ]); 45 46 return [ 47 'message' => 'success', 48 'code' => 200 49 ]; 50 51 } 52} 53

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Inertiaでの送信をthis.$inertia.post()に変更し、
JSONをそのまま返すのではなく、リダイレクトさせることで、モーダルポップ表示を防ぐことができました。

Javascript

1submit () { 2 this.$inertia.post(this.route('contact.post'), { 3 mailAddress: this.form.mailAddress, 4 category: this.form.category, 5 body : this.form.body 6 },{ 7 onSuccess : (response) => { 8 this.form.reset() 9 }, 10 }); 11 }

PHP

1/** 2 * 投稿処理 3 * 4 * @param App\Http\Requests\Contact\PostRequest $request 5 * @return \Illuminate\Http\Response 6 */ 7 public function post (PostRequest $request) 8 { 9 10 Notification::send(User::find(1), new Mail()); 11 12 DB::table('contacts')->insert([ 13 'category' => $request->input('category'), 14 'mail_address' => $request->input('mailAddress'), 15 'body' => $request->input('body'), 16 'created_at' => new DateTime() 17 ]); 18 19 return Redirect::route('contact')->with([ 20 'status' => 200, 21 'message' => 'Thnaks.', 22 ]); 23 24 }

投稿2021/07/09 07:22

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/07/09 08:05

上記で解決したと思ったのですが、リダイレクト時にwith()で受け渡したデータをVue側で受け取ることがうまくいきませんでした。 諦めて、Inertiaを介さずにフォーム送信を行いたいと思います。 解決するために参考にしたページのリンクを貼ります。 僕ではどうも理解できませんでしたが、参考になるかもしれません https://github.com/inertiajs/inertia-laravel/issues/170 https://laracasts.com/discuss/channels/laravel/how-to-submit-form-using-jetstreaminertia
shin27001

2022/01/18 19:50

Laravelは、あまり詳しくありませんが、(Inertiaは初めてさわりました^_^;;) ソースを追っていくと、下記コントローラに記述がありますよ! /vendor/laravel/fortify/src/Http/Controllers/ProfileInformationController.php リダイレクトせずに、new JsonResponse('', 200)を返せばよいのではないでしょうか。 return $request->wantsJson() ? new JsonResponse('', 200) : back()->with('status', 'profile-information-updated');
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問