laravel livewire upload image error validation - php

i have the same problem in this issue:
Laravel Livewire File Upload Not Validating and is returning a Livewire\TemporaryUploadedFile instance
i want upload my image path in database product, but i have some error
first in validate file image, missing field image or image required
same error in edit edit view,
but if i push second time the button il all ok and work
this is my controller:
public function store()
{
$data= $this->validate([
'name' => 'required',
// 'detail' => 'required',
//'giac' => 'required',
'classe' => 'required',
'photo' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:2024', // 1MB Max
]);
// $this->photo->store('documents');
$data['photo']= $this->photo->store('documents');
$filename = $this->photo->store('documents','public');
$data['photo'] = $filename;
$this->photo = $filename;
Product::updateOrCreate(['id' => $this->product_id], [
'name' => $this->name,
// 'detail' => $this->detail,
'giac' =>$this->giac,
'photo' =>$this->photo
]);
//$this->reset();
session()->flash('message',
$this->product_id ? 'Product Updated Successfully.' : 'Product Created Successfully.');
$this->closeModal();
$this->resetInputFields();
}
this is my blade file:
<div class="fixed z-10 inset-0 overflow-y-auto ease-out duration-400">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<div class="fixed inset-0 transition-opacity">
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<!-- This element is to trick the browser into centering the modal contents. -->
<span class="hidden sm:inline-block sm:align-middle sm:h-screen"></span>?
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full" role="dialog" aria-modal="true" aria-labelledby="modal-headline">
<form>
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="">
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Codice:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="minsan">
#error('minsan') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Giacenza:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="giac">
#error('giac') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Descrizione:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="name">
#error('name') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Classe:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="classe">
#error('classe') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<form wire:submit.prevent="uploadImage" enctype="multipart/form-data">
<input type="file" wire:model="photo">
#error('photo') <span class="text-red-500">{{ $message }}</span> #enderror
</form>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
<button wire:click.prevent="store()" type="button" class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-green-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-green-500 focus:outline-none focus:border-green-700 focus:shadow-outline-green transition ease-in-out duration-150 sm:text-sm sm:leading-5">
Salva
</button>
</span>
<span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto">
<button wire:click="closeModal()" type="button" class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5">
Cancel
</button>
</span>
</form>
</div>
</div>
</div>
</div>
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Livewire\Crud;
use App\Http\Livewire\Products;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/products', Products::class)->name('products.index');
Route::post('/products', Products::class)->name('products.index');

Related

laravel9, uploaded images are not displayed?

/* i have one balde that i use for uploading the images "createEvent.blade.php" then i try to retrive it in 2 baldes "indexEvent.blade.php" and "events.blade.php" the images stored in "/storage/app/public/img" and i run "php artisan storage:link
" every thing is fine in my localhost the imges uploaded and displayed fine, but when i upload my website to the server it still uploade the imge but not displying it. i try to figur out that but nothing work. any one can help. */
the controller i use to store the imges and updated
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Event;
use App\Models\EventAttendies;
use URL;
use Session;
use Redirect;
use Input;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\EventAttendiesExport;
class EventController extends Controller
{
//admin saide
public function index()
{
$data['events'] = Event::orderBy('id','desc')->paginate(20);
return view('dashboard.admin.indexEvent', $data);
}
public function create()
{
return view('dashboard.admin.createEvent');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
'date' => 'required',
'place' => 'required',
'event_type' => 'required',
'status' => 'required',
]);
$path = $request->file('image')->store('public/img');
$event = new Event;
$event->title = $request->title;
$event->description = $request->description;
$event->date = $request->date;
$event->place = $request->place;
$event->event_type = $request->event_type;
$event->image = $path;
$event->status = $request->status;
$event->save();
return redirect()->route('events.index')
->with('success','Event has been created successfully.');
}
public function show(Event $event)
{
return view('dashboard.admin.indexEvent',compact('event'));
}
public function edit(Event $event)
{
return view('dashboard.admin.eventEdit',compact('event'));
}
public function update(Request $request, $id)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'date' => 'required',
'place' => 'required',
'event_type' => 'required',
'status' => 'required',
]);
$event = Event::find($id);
if($request->hasFile('image')){
$request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
]);
$path = $request->file('image')->store('public/img');
$event->image = $path;
}
$event->title = $request->title;
$event->description = $request->description;
$event->date = $request->date;
$event->place = $request->place;
$event->event_type = $request->event_type;
$event->status = $request->status;
$event->save();
return redirect()->route('events.index')
->with('success','Event updated successfully');
}
// Event Show "events.blade.php" user side
public function event()
{
$data['events'] = Event::orderBy('id','desc')->where('status','1')->get();
return view('events', $data);
}
}
here my from from admin side to uploade the images
<form action="{{ route('events.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="overflow-hidden sm:rounded-md">
<div class="px-4 py-5 sm:p-2">
<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-6">
<label for="title" class="block text-sm font-medium text-gray-700">Event Title</label>
<input type="text" name="title" id="title" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm">
</div>
<div class="col-span-6 sm:col-span-6">
<label for="description" class="block text-sm font-medium text-gray-700">Event Description</label>
<input type="text" name="description" id="description" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm">
</div>
<div class="col-span-6">
<label for="place" class="block text-sm font-medium text-gray-700">Event Place</label>
<input type="text" name="place" id="place" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm">
</div>
<div class="col-span-6 sm:col-span-4">
<label for="date" class="block text-sm font-medium text-gray-700">Event Date</label>
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
</div>
<input datepicker="" datepicker-autohide="" name="date" id="date" type="text" class=" border border-gray-300 text-gray-900 sm:text-sm rounded-lg block w-full pl-10 p-2.5 datepicker-input" placeholder="Select date">
</div>
<input class="hidden" name="event_type" value="Free" id="event_type">
<!--- <div class="col-span-6 sm:col-span-3">
<label for="event_type" class="block text-sm font-medium text-gray-700">Event Type</label>
<select id="event_type" name="event_type" class="mt-1 block w-full rounded-md border border-gray-300 bg-white py-2 px-3 shadow-sm focus:outline-none sm:text-sm">
<option value="free">Free</option>
<option value="paid">Paid</option>
</select>
</div> -->
<div class="col-span-6 sm:col-span-3">
<label for="status" class="block text-sm font-medium text-gray-700">Event Status</label>
<select id="status" name="status" class="mt-1 block w-full rounded-md border border-gray-300 bg-white py-2 px-3 shadow-sm focus:outline-none sm:text-sm">
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
<div class="col-span-6 sm:col-span-4">
<label for="date" class="block text-sm font-medium text-gray-700">Event Photo</label>
<input type="file" name="image" class="form-control" placeholder="Event Title">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:flex sm:flex-row-reverse sm:px-6">
<button type="submit" class="inline-flex w-full justify-center rounded-md border border-transparent bg-red-600 px-4 py-2 text-base font-medium text-white shadow-sm hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 sm:ml-3 sm:w-auto sm:text-sm">Add</button>
<button type="button" class="mt-3 inline-flex w-full justify-center rounded-md border border-gray-300 bg-white px-4 py-2 text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm" id="close-Addmodal">Cancel</button>
</form>
here i retrive the image in admin side
#foreach ($events as $event)
<tr class="border-b border-gray-200 hover:bg-gray-100">
<td class="py-3 px-6 text-left whitespace-nowrap">
<div class="flex items-center">
<span class="font-medium">{{ $event->title }}</span>
</div>
</td>
<td class="py-3 px-6 text-left">
<div class="flex items-center">
<span>{{ $event->description }}</span>
</div>
</td>
<td class="py-3 px-6 text-center">
<div class="flex items-center justify-center">
<span class="font-medium">{{ $event->date }}</span>
</div>
</td>
<td class="py-3 px-6 text-center">
<div class="flex items-center justify-center">
<img class="w-6 h-6 rounded-full border-gray-200 border -m-1 transform hover:scale-125" src="{{ Storage::url($event->image) }}" alt="{{ $event->place }}"/>
</div>
</td>
<td class="py-3 px-6 text-center">
#if(($event->status)==1)
<span class="bg-purple-200 text-purple-600 py-1 px-3 rounded-full text-xs">Active</span>
#else
<span class="bg-red-200 text-red-600 py-1 px-3 rounded-full text-xs">Pending</span>
#endif
<!--<span class="bg-green-200 text-green-600 py-1 px-3 rounded-full text-xs">Completed</span>-->
here i retrive the imge in user side
#foreach ($events as $event)
<div class="w-full #if($events->count()>=2) md:w-1/2 #endif px-4">
<div class="bg-white shadow-lg rounded-lg overflow-hidden mb-10">
<img class="eventsPic w-full" style="background-image:url({{ Storage::url($event->image) }});"/>
<div class="p-8 sm:p-9 md:p-7 xl:p-9 text-center">
<h3>
<a href="javascript:void(0)"
class=" font-semibold text-dark text-xl sm:text-[22px] md:text-xl lg:text-[22px] xl:text-xl 2xl:text-[22px] mb-2 block hover:text-primary " >
{{ $event->title }}
</a>
</h3>
<p class="text-base text-body-color leading-relaxed">
{{ $event->description }}
</p>
<p>{{ ucfirst($event->event_type) }}</p>
<p>{{ $event->place }}</p>
<p>{{ $event->date }}</p>
<a id="Regis" data-event-id="{{$event->id}}" data-title="{{ $event->title }}" data-event-type="{{ $event->event_type }}"
href="javascript:void(0)"
class="register-btn inline-block py-2 px-12 border rounded-full text-base text-body-color font-medium hover:border-red-700 hover:bg-red-700 hover:text-white transition">
{{__('sniperTrans.Register')}}
</a>
</div>
</div>
</div>
#endforeach
the problem was I deployed it via GitHub and i do not give the permission to write.

Error on input validation even though input exists

I have an application where an user can change his name via a pop-up.
This is the method that handles the name change:
public function changeVorname(Request $request) {
$this->validate($request, [
'vorname' => 'required|max:255',
]);
Portal::query()->where('email', $request->email)->update(['vorname' => $request->neuer_vorname]);
return redirect()->back()->with('message', 'Vorname erfolgreich geändert');
}
This is the blade file with the modal:
<div class="w-1/2 mb-4 pb-3 text-lg">
<p>Vorname:</p>
<input type="text" name="vorname" id="vorname"
value="{{ \Illuminate\Support\Facades\Auth::guard('portal')->user()->vorname }}"
class="flex text-center bg-gray-100 border-gray-500 shadow-2xl
border-opacity-50 border-2 w-full p-4 rounded-lg #error('vorname') border-red-500 #enderror"
readonly>
#error('vorname')
<div class="text-red-500 mt-2 text-sm">
{{ 'Der Vorname darf keine Zahlen enthalten und nicht leer sein' }}
</div>
#enderror
<div class="text-right">
<button id="change_vorname" class="md:pl-2" name="change_vorname">
Vorname bearbeiten
</button>
</div>
</div>
<div id="change_vorname_modal"
class="modal fixed ml-96 top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white hidden">
<div class="mt-3 text-center text-xl">
Neuer Vorname
</div>
<div class="items-center px-4 py-3">
<label for="neuer_vorname" class="sr-only">Neuer Vorname</label>
<form
action="{{ route('change_vorname', \Illuminate\Support\Facades\Auth::guard('portal')->user()->email) }}"
method="post">
#csrf
<input type="text" name="neuer_vorname" id="neuer_vorname"
placeholder="Neuer Vorname" value=""
class="flex text-center text-sm mb-2 bg-gray-100 border-gray-500 shadow-2xl border-opacity-50 border-2 w-full p-4 rounded-lg">
<button type="submit" id="ok_btn" class="mb-4 pb-3 w-full text-white px-4 py-3 rounded text-base font-medium
bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100
shadow-2xl border-2 w-full p-4 rounded-lg">
Vorname ändern
</button>
</form>
</div>
<div class="items-center px-4 py-3">
<button id="back" class="mb-4 pb-3 w-full text-white px-4 py-3 rounded text-base font-medium
bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100
shadow-2xl border-2 w-full p-4 rounded-lg">
zurück
</button>
</div>
</div>
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
<script>
var vorname_modal = document.getElementById("change_vorname_modal");
var vorname_btn = document.getElementById("change_vorname");
var back_btn = document.getElementById("back");
vorname_btn.onclick = function () {
vorname_modal.style.display = "block";
}
back_btn.onclick = function () {
vorname_modal.style.display = "none";
}
window.onclick = function (event) {
if (event.target == modal) {
vorname_modal.style.display = "none";
}
}
</script>
The issue is when I have the required in the request validation I always get the error, when I delete the required everything works perfectly. I need it because a user should not be able to rename his name to an empty one.
You have a mistake on name of new name input. its not same as validators key
change your validator like this
$this->validate($request, [
'neuer_vorname' => 'required|max:255',
]);
it will work

How do I properly display checkbox values on Laravel edit blade?

Facing an annoying issue with checkboxes in a form I have created in Laravel.
I have assigned a value to each checkbox and when submitting to the database implode all the values together into an array.
The data will submit successfully to the database, but when I try to edit a submission, only the text based data shows up while the checkboxes will not re-populate.
Click here to see a preview of the issue.
I have tried a ton of this from StackoverFlow to to avail so wondering if anyone has an idea?
Happy to share code sample below
Complete Edit Blade
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight"> Edit Assessment </h2>
</x-slot>
<div>
<div class="max-w-4xl mx-auto py-10 sm:px-6 lg:px-8">
<div class="mt-5 md:mt-0 md:col-span-2">
<form method="post" action="{{ route('assessments.update', $assessment->id) }}"> #csrf #method('PUT') <div class="shadow overflow-hidden sm:rounded-md">
<div class="px-4 py-5 bg-white sm:p-6">
<label for="learner" class="block font-medium text-sm text-gray-700">Learner</label>
<input type="text" name="learner" id="learner" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('learner', $assessment->learner) }}" /> #error('learner') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="type" class="block font-medium text-sm text-gray-700">Type</label>
<input type="text" name="type" id="type" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('type', $assessment->type) }}" /> #error('type') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="assessor" class="block font-medium text-sm text-gray-700">Assessor</label>
<input type="text" name="assessor" id="assessor" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('assessor', $assessment->assessor) }}" /> #error('assessor') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="inquiry" class="block font-medium text-sm text-gray-700">Inquiry</label>
<input type="text" name="inquiry" id="inquiry" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('inquiry', $assessment->inquiry) }}" /> #error('inquiry') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="competency" class="block font-medium text-sm text-gray-700">Competency</label>
<input type="text" name="competency" id="competency" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('competency', $assessment->competency) }}" /> #error('competency') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="contexts" class="block font-medium text-sm text-gray-700">Contexts</label>
<input type="text" name="contexts" id="contexts" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('contexts', $assessment->contexts) }}" /> #error('contexts') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="artifact" class="block font-medium text-sm text-gray-700">Artifact</label>
<input type="text" name="artifact" id="artifact" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('artifact', $assessment->artifact) }}" /> #error('artifact') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="link" class="block font-medium text-sm text-gray-700">Link</label>
<input type="text" name="link" id="link" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('link', $assessment->link) }}" /> #error('link') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<label for="notes" class="block font-medium text-sm text-gray-700">Notes</label>
<input type="text" name="notes" id="notes" type="text" class="form-input rounded-md shadow-sm mt-1 block w-full" value="{{ old('notes', $assessment->notes) }}" /> #error('notes') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="px-4 py-5 bg-white sm:p-6">
<x-jet-label for="rating"> Rating <div class="flex items-center form-check-inline justify-center">
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="1" value="1" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="2" value="2" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="3" value="3" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="4" value="4" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="5" value="5" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="6" value="6" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="7" value="7" />
<div class="ml-2"></div>
<x-jet-checkbox name="rating[]" id="8" value="8" />
<div class="ml-2"></div>
</div>
</x-jet-label> #error('rating') <div class="items-center justify-center">
<p class="text-justify text-sm text-red-600">{{ $message }}</p>
</div> #enderror
</div> #error('rating') <p class="text-sm text-red-600">{{ $message }}</p> #enderror
</div>
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6">
<button class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:shadow-outline-gray disabled:opacity-25 transition ease-in-out duration-150"> Edit </button>
</div>
</div>
</form>
</div>
</div>
</div>undefined
</x-app-layout>
Assessments Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\StoreAssessmentRequest;
use App\Http\Requests\UpdateAssessmentRequest;
use App\Models\Assessment;
class AssessmentsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$assessment = Assessment::all();
return view('assessments.index', compact('assessment'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('assessments.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(StoreAssessmentRequest $request)
{
$data = $request->validated();
$data['rating'] = implode(", ",$data['rating']);
Assessment::create($data);
//Assessment::create($request->validated());
return redirect()->route('assessments.index');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(Assessment $assessment)
{
return view('assessments.show', compact('assessment'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit(Assessment $assessment)
{
return view('assessments.edit', compact('assessment'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(UpdateAssessmentRequest $request, Assessment $assessment)
{
$data = $request->validated();
$data['rating'] = implode(", ",$data['rating']);
$assessment->update($data);
return redirect()->route('assessments.index');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy(Assessment $assessment)
{
$assessment->delete();
return redirect()->route('assessments.index');
}
}
UpdateAssessmentRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateAssessmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'type' => 'required',
'learner' => 'required',
'assessor' => 'required',
'competency' => 'required',
'contexts' => 'required',
'artifact' => 'required',
'inquiry' => 'required',
'link' => 'required',
'notes' => 'required',
'rating' => 'required',
];
}
}

Livewire file upload validation always fails the first time but works the second time

Weird issue with my Livewire: the validation, when I send a file, always fails on the first time. If I have the criteria that it must be a pdf, I will put the PDF, submit the form, get a message telling me I have the wrong format, I won't change anything, resubmit and it will go through with no issue.
Does anyone knows why it might be happening?
<?php
namespace App\Http\Livewire\Branch\Documents;
use App\Models\BranchDocument;
use Livewire\Component;
use Livewire\WithFileUploads;
class Upload extends Component
{
use WithFileUploads;
public BranchDocument $document;
public $file;
public $saved = false;
public function render()
{
return view('livewire.branch.documents.upload');
}
public function save() {
$this->validate([
'file' => 'mimes:jpg,bmp,png,pdf|max:10240', // 1MB Max
]);
$this->document->file = $this->file;
$this->saved = true;
}
}
<div x-data="{ open: false }">
<li class="#if(!$saved) hover:bg-gray-100 py-2 px-2 rounded cursor-pointer #endif" #click="open = true">
<div class="relative">
<div class="relative flex space-x-3">
<div class="min-w-0 flex-1 flex justify-between space-x-4 items-center">
<div>
<p class="text-sm text-porange">{{ $document->document_type->title }}#if($saved)<span class="text-sm text-gray-600"> - {{ __('Document sent!') }}</span>#endif</p>
<p class="text-xs text-gray-600">{{ $document->created_at->diffForHumans() }}</p>
</div>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" >
#if(!$saved)
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
#else
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
#endif
</svg>
</div>
</div>
</div>
</li>
#if(!$saved)
<div x-show="open" class="fixed z-10 inset-0 overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<!--
Background overlay, show/hide based on modal state.
Entering: "ease-out duration-300"
From: "opacity-0"
To: "opacity-100"
Leaving: "ease-in duration-200"
From: "opacity-100"
To: "opacity-0"
-->
<div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true"></div>
<!-- This element is to trick the browser into centering the modal contents. -->
<span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">​</span>
<!--
Modal panel, show/hide based on modal state.
Entering: "ease-out duration-300"
From: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
To: "opacity-100 translate-y-0 sm:scale-100"
Leaving: "ease-in duration-200"
From: "opacity-100 translate-y-0 sm:scale-100"
To: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
-->
<div #click.away="open = false" class="inline-block align-bottom bg-white rounded-lg px-4 py-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full sm:p-6">
<div>
<div class="text-center">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-title">
{{ __('Upload ":title"', ['title' => $document->document_type->title])}}
</h3>
</div>
</div>
<form wire:submit.prevent="save">
<input type="file" wire:model="file" class="file-input-business block mx-auto mt-4"/>
#error('file') <span class="error">{{ $message }}</span> #enderror
<div class="mt-5 sm:mt-6 sm:grid sm:grid-cols-2 sm:gap-3 sm:grid-flow-row-dense">
<button type="submit" type="button" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-porange text-base font-medium text-white hover:bg-porange-darker focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-porange sm:col-start-2 sm:text-sm">
{{ __('Send')}}
</button>
<button #click="open = false" type="button" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-porange sm:mt-0 sm:col-start-1 sm:text-sm">
{{ __('Cancel')}}
</button>
</div>
</form>
</div>
</div>
</div>
#endif
</div>
Thank you!
Is the file uploaded before you hit submit? If not then the file is null hence the wrong format error. Check its value in the save method before validation. You can also use wire.loading on a div to determine if the file is still uploading.
You may also run into issues with built in artisan development server (the one you start with php artisan serve command). It has different limitations to a 'real' server - upload file size, formats, etc.

Not able to produce action upon clicking the button

I am using the below code from Tailblocks Link under CTA section , upon clicking the button I am not able to get to new page. In the below code form tags are added by me. please guide how can I resolve it?
<form ction="index.php" method="post">
<section class="text-gray-600 body-font">
<div class="container px-5 py-24 mx-auto flex flex-wrap items-center">
<div class="lg:w-3/5 md:w-1/2 md:pr-16 lg:pr-0 pr-0">
<h1 class="title-font font-medium text-3xl text-gray-900">Slow-carb next level shoindcgoitch ethical authentic, poko scenester</h1>
<p class="leading-relaxed mt-4">Poke slow-carb mixtape knausgaard, typewriter street art gentrify hammock starladder roathse. Craies vegan tousled etsy austin.</p>
</div>
<div class="lg:w-2/6 md:w-1/2 bg-gray-100 rounded-lg p-8 flex flex-col md:ml-auto w-full mt-10 md:mt-0">
<h2 class="text-gray-900 text-lg font-medium title-font mb-5">Sign Up</h2>
<div class="relative mb-4">
<label for="full-name" class="leading-7 text-sm text-gray-600">Full Name</label>
<input type="text" id="full-name" name="full-name" class="w-full bg-white rounded border border-gray-300 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out">
</div>
<div class="relative mb-4">
<label for="email" class="leading-7 text-sm text-gray-600">Email</label>
<input type="email" id="email" name="email" class="w-full bg-white rounded border border-gray-300 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out">
</div>
<button class="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg">Button</button>
<p class="text-xs text-gray-500 mt-3">Literally you probably haven't heard of them jean shorts.</p>
</div>
</div>
</section>
</form>
You form action attribute is having a typo.
<form action="index.php" method="post">
Also, instead of button you need to select the input type as submit.
<input type='submit' class="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg" value="Button">
else add some JS to submit the form
<script>
var forms = document.getElementsByTagName('form');
for(var i = 0; i < forms.length; i += 1) {
forms[i].addEventListener('submit', function(e) {
e.preventDefault();
}, true);
}
</script>
You have a typo error in action attribute in your form tag.
<form action="index.php" method="post">
Also you don't add the type attribute to your button tag.
<button type="submit" class="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg">Button</button>
I hope this will fix your problem.

Categories