Livewire custom messsage global for temporaryUrl

20 Views Asked by At

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]);
    }
}
1

There are 1 best solutions below

0
Qirel On

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 $rules array, and then you set the messages via the $messages array.

The notable changes here are $this->validate() having the arguments removed, and moved those to the protected properties $rules and $messages.

<?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;

    protected $rules = [
        'photo' => 'file|mimes:png,jpg|max:1024',
        'temporary_file_upload' => 'file|mimes:png,jpg|max:1024',
    ];
    
    protected $messages = [
        'photo.mimes' => "Arquivo inválido. Permitido somente .png ou .jpg", 
        'temporary_file_upload.file' => "Arquivo inválido. Permitido somente .png ou .jpg",
    ];

    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();
        
        $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]);
    }
}

Read the documentation for full information.