РЕПЕТИТОР математика физика информатика
Для школьников и студентов. Подтягивание пробелов. ЦЭ, ЦТ, ОГЭ, ЕГЭ.
Идет набор на ЛЕТО. Жмите для подробностей:)
214 of 274 menu

Правило max

Правило max используется в Livewire для ограничения максимального размера загружаемого файла. Оно применяется вместе с трейтом WithFileUploads и указывается в качестве правила валидации для свойства компонента, содержащего загруженный файл. Значение указывается в килобайтах.

Синтаксис

use Livewire\WithFileUploads; use Livewire\Component; class UploadComponent extends Component { use WithFileUploads; public $file; public function save() { $this->validate([ 'file' => 'max:1024', ]); } }

Пример

Давайте создадим компонент для загрузки файла с ограничением размера в 1024 килобайта:

<?php namespace App\Livewire; use Livewire\Component; use Livewire\WithFileUploads; class UploadComponent extends Component { use WithFileUploads; public $file; public $message = ''; public function save() { $this->validate([ 'file' => 'max:1024', ]); $path = $this->file->store('uploads'); $this->message = 'File saved successfully: ' . $path; } public function render() { return view('livewire.upload-component'); } } ?>
<div> <form wire:submit.prevent="save"> <input type="file" wire:model="file"> @error('file') <span style="color: red;">{{ $message }}</span> @enderror <button type="submit">Upload</button> </form> @if($message) <p>{{ $message }}</p> @endif </div>

Пример

Давайте ограничим размер загружаемого изображения до 2048 килобайт:

<?php namespace App\Livewire; use Livewire\Component; use Livewire\WithFileUploads; class ImageUploadComponent extends Component { use WithFileUploads; public $image; public $message = ''; public function upload() { $this->validate([ 'image' => 'image|max:2048', ]); $path = $this->image->store('images'); $this->message = 'Image saved: ' . $path; } public function render() { return view('livewire.image-upload-component'); } } ?>
<div> <form wire:submit.prevent="upload"> <input type="file" wire:model="image" accept="image/*"> @error('image') <span style="color: red;">{{ $message }}</span> @enderror <button type="submit">Save image</button> </form> @if($message) <p>{{ $message }}</p> @endif </div>

Пример

Давайте загрузим файл с ограничением размера в 512 килобайт и проверим результат:

<?php namespace App\Livewire; use Livewire\Component; use Livewire\WithFileUploads; class DocumentComponent extends Component { use WithFileUploads; public $document; public $status = ''; public function submit() { $this->validate([ 'document' => 'mimes:pdf,doc,docx|max:512', ]); $name = $this->document->getClientOriginalName(); $path = $this->document->storeAs('documents', $name); $this->status = 'Document uploaded: ' . $path; } public function render() { return view('livewire.document-component'); } } ?>
<div> <form wire:submit.prevent="submit"> <input type="file" wire:model="document"> @error('document') <span style="color: red;">{{ $message }}</span> @enderror <button type="submit">Upload document</button> </form> @if($status) <p>{{ $status }}</p> @endif </div>

Смотрите также

  • правило file,
    которое проверяет, что загруженный файл является валидным
  • правило image,
    которое проверяет, что файл является изображением
  • правило mimes,
    которое ограничивает типы загружаемых файлов
  • трейт WithFileUploads,
    который добавляет поддержку загрузки файлов в компонент
Мы используем cookie для работы сайта, аналитики и персонализации. Обработка данных происходит согласно Политике конфиденциальности.
принять все настроить отклонить