Input Field Required

29 views Asked by At

im making simple data table CRUD. the create must able to have file input, in this context is image. but im encountering this problem where i think the input name isn't recognized by the validate request in controller bcs i did upload an image which only 50KB in size.

here's the input form

<form id="addTask" action="{{ route('create-data') }}" method="POST" autocomplete="off" enctype="multipart/form-data">
                        @csrf
                        <div class="container-fluid">                        
                            <div class="row">
@if (Auth::user()->role->name === 'Operator')
                                    <div class="col-sm-5 col-md-6">                                                                           
                                        <input type="hidden" class="form-control datepicker " id="datepicker" name="input_date" />
                                        <div class="mb-3">
                                            <label for="project" class="align-items-start">Project:</label>
                                            <input type="text" class="form-control w-100" name="nama_project">
                                        </div>                                  
                                        <div class="mb-3">
                                            <label for="detail" class="align-items-start">Detail Project:</label>
                                            <input type="text" class="form-control w-100" name="detail">
                                        </div>                                  
                                        <div class="mb-3">
                                            <label for="eta" class="align-items-start">ETA:</label>
                                            <div class="input-group date w-100" id="datepickerContainer">
                                                <input type="text" class="form-control datepicker" id="etapicker" name="eta_project"/>
                                                <span class="input-group-append">
                                                    <span class="input-group-text bg-light d-block">
                                                        <i class="fa fa-calendar"></i>
                                                    </span>
                                                </span>
                                            </div>
                                        </div>
                                        <div class="mb-3">
                                            <label for="requestor" class="align-items-start">Request Name:</label>
                                            <input type="text" class="form-control w-100" name="requestor">
                                        </div>                                    
                                    </div>
                                    <div class="col-sm-5 offset-sm-2 col-md-6 offset-md-0">                                
                                        <div class="mb-3">
                                            <label for="category" class="align-items-start">Category:</label>
                                            <select class="form-control w-100" id="category_project" name="category_project">
                                                <option selected>Pilih Kategori</option>
                                                <option value="Infrastructure">Infrastructure</option>
                                                <option value="Maintenance">Maintenance</option>
                                                <option value="Tool Store">Tool Store</option>
                                            </select>
                                        </div>
                                        <div class="mb-3" >                                        
                                            <label for="status">Status</label>                                                            
                                                <select class="form-control w-100" id="status" name="status">
                                                <option selected>Select</option>
                                                <option value="Open">Open</option>
                                                <option value="On Progress">On Progress</option>
                                                <option value="Done">Done</option>
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <label for="photos">Image:</label>
                                            <input type="file" name="photos_img" id="photos">
                                        </div>                                                                                     
                                    </div>
                                @endif         
                                
                            </div>
                        <div class="modal-footer">
                            <button type="submit" name="submit" class="btn btn-primary" >Submit</button>
                        </div>
                    </form>

and this is my controller

public function store(Request $request)
     {
         $validatedData = $request->validate([
             'eta_project' => 'required|date_format:m/d/Y',
             'requestor' => 'required',
             'pic_project' => 'nullable',
             'nama_project' => 'required',
             'detail' => 'required',
             'category_project' => 'required',
             'status' => 'nullable',
             'description_project' => 'nullable',
             'photos_img' => 'image|mimes:jpeg,png,jpg,gif|max:2048|required'
         ]);
     
         $validatedData['input_date'] = now()->toDateString(); // Assuming 'input_date' is of type DATE
         // Format the dates using Carbon
         $validatedData['eta_project'] = Carbon::createFromFormat('m/d/Y', $validatedData['eta_project']);
     
         $imageName = 'none'; // Initialize imageName with null
     
         if ($request->hasFile('photos_img')) {
             // Get the file from the request
             $image = $request->file('photos_img');
             // Generate a unique name for the image file
             $imageName = time().'.'.$image->getClientOriginalExtension();
             // Store the image file in the specified directory
             $image->storeAs('images', $imageName, 'public'); // Store the image in the public/images directory
             $validatedData['photos_img'] = $imageName; // Assign imageName to 'image' field
         }
        
         // Create a new record in the database
         $model = Project::create($validatedData);
     
         return response()->json(['status' => 'success', 'data' => $model]);
     }

i even try to see if the error was happened while requesting but it seems already wrong since the input field name. i also struggling to understand the storing of the image

0

There are 0 answers