How to customize message for temp file with livewire. I tried performing component validation and global configuration, but it had no effect. It continues to display only the original livewire message.
https://laravel-livewire.com/docs/2.x/file-uploads#global-validation
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
use Livewire\WithFileUploads;
class ImageProfile extends Component
{
use WithFileUploads;
public $userId;
public $user;
public $photo;
public $image;
public function mount(int $id){
$this->userId = $id;
$this->image = $this->getImageProfile();
$this->user = User::find($this->userId);
}
public function getImageProfile() {
return User::find($this->userId);
}
public function save()
{
$this->validate(
['photo' => 'file|mimes:png,jpg|max:1024',],
['photo.mimes' => "Arquivo inválido. Permitido somente .png ou .jpg"] ,
['temporary_file_upload' => 'file|mimes:png,jpg|max:1024'],
['temporary_file_upload.file' => "Arquivo inválido. Permitido somente .png ou .jpg"] ,
);
$this->user->profile_photo_path = $this->photo->store("users/".$this->id);
$this->user->save();
$this->clear();
}
public function clear() {
$this->photo = null;
$this->image = $this->getImageProfile();
}
public function render()
{
return view('livewire.image-profile', ['image' => $this->image]);
}
}
Adding
['temporary_file_upload.file' => "Arquivo inválido. Permitido somente .png ou .jpg"]as part of the validation-rule, means it should be treated as a rule - not the message.Instead, I recommend you abstract the rules into its own
$rulesarray, and then you set the messages via the$messagesarray.The notable changes here are
$this->validate()having the arguments removed, and moved those to the protected properties$rulesand$messages.Read the documentation for full information.