YuMp's avatar
Level 2

Livewire validation rules problem

Hello, I'm trying to create custom rules for each file field in config/livewire and using the createpost method, but I'm getting an error when trying to validate any file

The image failed to upload.

does anyone have an idea what could be wrong?

config/livewire

  'rules' => [
        'image' => ['mimes:png,jpg,jpeg|max:140000'],
        
        'gif' => ['mimes:gif|max:140000'],
        'video' =>['mimes:mp4|max:140000'],
    ],

class CreatePost extends Component /// ...

protected $rules = [ 'image' => 'nullable|image|mimes:jpeg,jpg,png|max:18048', 'video' => 'nullable|mimes:mp4|max:950048', 'gif' => 'nullable|image|mimes:gif|max:18048', 'title' => 'required|min:4|max:85', 'category' => 'required|integer|exists:categories,id', 'description' => 'required|min:4', 'name' => 'nullable',

];

public function createPost()
{
    if(auth()->check()){

       
        $this->validate([
            'image' => [
                'nullable',
                'image',
                'mimes:jpeg,jpg,png',
                'max:18048',
                function ($attribute, $value, $fail) {
                    $image = Image::make($value);
                    if (!$image->isAllowed()) {
                        $fail('Este arquivo de imagem não é permitido.');
                    }
                },
            ],
            'video' => [
                'nullable',
                'mimes:mp4,ogx,oga,ogv,ogg,webm',
                'max:950048',
                function ($attribute, $value, $fail) {
                    // Verificar se o vídeo é seguro
                },
            ],
            'gif' => [
                'nullable',
                'image',
                'mimes:gif',
                'max:18048',
                function ($attribute, $value, $fail) {
                    $image = Image::make($value);
                    if (!$image->isAllowed()) {
                        $fail('Este arquivo de GIF não é permitido.');
                    }
                },
            ],
            'title' => 'required|min:4|max:85',
            'category' => 'required|integer|exists:categories,id',
            'description' => 'required|min:4',
            'name' => 'nullable',
        ]);
        
        $clientIP = request()->ip();
       
        
        $post = Post::create([
        
       
        'user_id' => auth()->id(),
        'category_id' => $this->category,
        'status_id' => 1, 
        'title' => $this->title,
        'description' => $this->description,
        'image' => $this->image ? $this->image->store('images') : null,
        'video' => $this->video ? $this->video->store('videos') : null,
        'gif' => $this->gif ? $this->gif->store('gifs') : null,
        'user_ip' =>  $clientIP,

        
    ]);


    $post->save();
0 likes
2 replies
LaryAI's avatar
Level 58

The error message "The image failed to upload" suggests that the Livewire validation rules are not being met. One possible solution is to update the validation rules in the CreatePost component to match the rules defined in the config/livewire file. Here's an example:

protected $rules = [
    'image' => 'nullable|mimes:png,jpg,jpeg|max:140000',
    'gif' => 'nullable|mimes:gif|max:140000',
    'video' => 'nullable|mimes:mp4|max:140000',
    'title' => 'required|min:4|max:85',
    'category' => 'required|integer|exists:categories,id',
    'description' => 'required|min:4',
    'name' => 'nullable',
];

Then, update the createPost method to use the $rules property instead of defining the rules inline:

public function createPost()
{
    if (auth()->check()) {
        $this->validate();

        $clientIP = request()->ip();

        $post = Post::create([
            'user_id' => auth()->id(),
            'category_id' => $this->category,
            'status_id' => 1,
            'title' => $this->title,
            'description' => $this->description,
            'image' => $this->image ? $this->image->store('images') : null,
            'video' => $this->video ? $this->video->store('videos') : null,
            'gif' => $this->gif ? $this->gif->store('gifs') : null,
            'user_ip' => $clientIP,
        ]);

        $post->save();
    }
}

This should ensure that the Livewire validation rules are being applied correctly.

YuMp's avatar
Level 2

Solution --> in config/livewire 'rules' => ['image', 'mimes:png,jpg'], ['video', 'mimes:mp4'], ['gif', 'mimes:gif'],

Please or to participate in this conversation.