laravel バリデーションメッセージが表示されない

laravel8,php8.2,dockerで作った飲食店ログアプリをAWSでデプロイしました。
ログ登録の際にカテゴリ、店名、カテゴリ、訪問状況、は入力必須なのですが、入力せずに登録ボタンを押してもバリデーションメッセージが出ずにリダイレクトされます。
また、ローカル環境ではバリデーションが表示されるのですが本番環境では表示されません。

原因と解決策を教えてください
お願いします

php

1@extends('layouts.app')2 3@section('title', 'ログ登録')4 5@section('content')6<div class="container">7 <div class="row justify-content-center">8 <div class="col-8">9 10 @if (session('status'))11 <div class="alert alert-success">12 {{ session('status') }}13 </div>14 @endif15 16 <div style="margin-bottom: 5px;"></div>17 18 <form id="log-form" method="POST" action="{{ route('add') }}" class="p-5" enctype="multipart/form-data">19 @csrf 20 21 {{-- 店名 --}}22 <div class="form-group">23 <label for="name">店名</label>24 <div style="margin-bottom: 5px;"></div>25 <input id="name" type="text" class="form-control {{ $errors->has('name') ? ' is-invalid' : '' }}" name="name"26 value="{{ old('name') }}" autocomplete="name" autofocus>27 @if ($errors->has('name'))28 <div class="invalid-feedback" role="alert">29 {{ $errors->first('name') }}30 </div>31 @endif32 </div>33 34 <div style="margin-bottom: 20px;"></div>35 36 {{-- カテゴリ --}}37 <div class="form-group">38 <label for="category">カテゴリ</label>39 <div style="margin-bottom: 5px;"></div>40 <select id="category" name="category" class="custom-select form-control @error('category') is-invalid @enderror">41 <option value="">選択してください</option>42 @foreach ($categories as $category)43 <option value="{{ $category->id }}" {{ old('category') == $category->id ? 'selected' : '' }}>44 {{ $category->name }}45 </option>46 @endforeach47 </select>48 @error('category')49 <div class="invalid-feedback">{{ $message }}</div>50 @enderror 51 </div>52 53 <div style="margin-bottom: 20px;"></div>54 55 {{-- 訪問状況 --}}56 <div class="form-group">57 <label for="visit_status">訪問状況</label>58 <div style="margin-bottom: 5px;"></div>59 60 <div class="form-check form-check-inline">61 @foreach ($visitStatuses as $visitStatus)62 <label>63 <input type="radio" name="visit_status" value="{{ $visitStatus->id }}">64 {{ $visitStatus->name }}65 </label>66 @endforeach67 </div>68 @if ($errors->has('visit_status'))69 <div class="invalid-feedback" role="alert">70 {{ $errors->first('visit_status') }}71 </div>72 @endif73 </div>74 75 <div style="margin-bottom: 20px;"></div>76 77 {{-- 点数 --}}78 <div class="form-group">79 <label for="score">点数</label>80 <div class="cont">81 <div class="stars">82 @for ($i = 5; $i >= 1; $i--)83 <input class="star star-{{ $i }}" id="star-{{ $i }}" type="radio" name="score_id"84 value="{{ $i }}" />85 <label class="star star-{{ $i }}" for="star-{{ $i }}"></label>86 @endfor87 </div>88 @if ($errors->has('score_id'))89 <div id="score-error" class="invalid-feedback" role="alert">90 {{ $errors->first('score_id') }}91 </div>92 @endif93 </div>94 </div>95 96 <div style="margin-bottom: 20px;"></div>97 98 {{-- 感想 --}}99 <div class="form-group">100 <label for="review">感想</label>101 <div style="margin-bottom: 5px;"></div>102 <textarea id="review" class="form-control" name="review">{{ old('review') }}</textarea>103 @if ($errors->has('review'))104 <div class="invalid-feedback" role="alert">105 {{ $errors->first('review') }}106 </div>107 @endif108 </div>109 110 <div style="margin-bottom: 20px;"></div>111 112 {{-- 画像 --}}113 <div class="form-group">114 <label for="images">画像 (最大5)</label>115 <div style="margin-bottom: 5px;"></div>116 <div class="d-flex flex-wrap">117 @if($log->images && count($log->images) > 0)118 @foreach($log->images as $image)119 <img src="{{ asset('storage/' . $image->path) }}" alt="Image" class="img-thumbnail m-2"120 style="max-width: 150px; max-height: 150px;">121 @endforeach122 @endif123 @for ($i = count($log->images ?? []); $i < 5; $i++)124 <input id="images{{ $i+1 }}" type="file" class="form-control @error('images.*') is-invalid @enderror"125 name="images[]" accept="image/*" multiple>126 @endfor127 </div>128 @error('images.*')129 <span class="invalid-feedback" role="alert">130 <strong>{{ $message }}</strong>131 </span>132 @enderror 133 </div>134 135 <div style="margin-bottom: 20px;"></div>136 137 <div class="form-group">138 <div class="d-grid gap-2">139 {{-- 登録ボタン --}}140 <button type="submit" class="btn btn-block">登録</button>141 </div>142 </div>143 144 </form>145 </div>146 </div>147</div>148 149<script>150 document.getElementById('log-form').addEventListener('submit', function(event) {151 var scoreRadios = document.getElementsByName('score_id');152 var scoreSelected = false;153 for (var i = 0; i < scoreRadios.length; i++) {154 if (scoreRadios[i].checked) {155 scoreSelected = true;156 break;157 }158 }159 if (!scoreSelected) {160 document.getElementById('score-error').style.display = 'block';161 event.preventDefault();162 } else {163 document.getElementById('score-error').style.display = 'none';164 }165 });166</script>167 168@endsection

php

12<?php3 4namespace App\Http\Controllers;5 6use App\Http\Requests\AddRequest;7use App\Models\Log;8use App\Models\Category;9use App\Models\VisitStatus;10use App\Models\Score;11use App\Models\Image;12use Illuminate\Support\Facades\Auth;13use Illuminate\Support\Facades\Storage;14 15class AddController extends Controller16{17 18 public function addLog(AddRequest $request)19 {20 $user = Auth::user();21 22 $log = Log::create([23 'user_id' => $user->id,24 'name' => $request->input('name'),25 'category_id' => $request->input('category'),26 'visit_status_id' => $request->input('visit_status'),27 'score_id' => $request->input('score_id'),28 'review' => $request->input('review'),29 ]);30 31 if ($request->hasFile('images')) {32 foreach ($request->file('images') as $image) {33 $path = $image->store('log_images', 'public');34 $imageModel = $log->images()->create(['path' => $path]);35 $imageModel->log_id = $log->id;36 $imageModel->save();37 }38 }39 40 $log->load('images');41 42 foreach ($log->images as $image) {43 Image::create([44 'log_id' => $log->id,45 'path' => $image->path,46 ]);47 }48 49 $request->session()->flash('status', 'ログを登録しました!');50 51 $logs = Log::orderBy('updated_at', 'desc')->get();52 $categories = Category::orderBy('sort_no')->get();53 $scores = Score::all();54 $visitStatuses = VisitStatus::all();55 $log = new Log();56 57 return view('add', compact('categories', 'visitStatuses', 'scores', 'log'));58 }59}60

php

1<?php 3namespace App\Http\Requests; 5use Illuminate\Foundation\Http\FormRequest; 7class AddRequest extends FormRequest8{9 12 13 14 public function authorize()15 {16 return true;17 } 19 22 23 24 public function rules()25 {26 return [27 'name' => ['required', 'string', 'max:255'],28 'category' => ['required', 'integer', 'exists:categories,id'],29 'visit_status' => ['required', 'string', 'exists:visit_statuses,id'],30 'score_id' => ['required', 'integer', 'exists:scores,id'],31 'review' => ['nullable', 'string'],32 'image' => ['nullable', 'file', 'image'],33 ];34 } 36 39 40 41 public function attributes()42 {43 return [44 'name' => '店名',45 'category' => 'カテゴリ',46 'visit_status' => '訪問状況',47 'score_id' => '点数',48 'review' => '感想',49 'image' => '画像',50 ];51 }52}

コメントを投稿

0 コメント