我正在嘗試輸入允許用戶輸入評論和/或影像的帖子,但是我的 livewire 組件似乎要求包含影像,因為如果沒有上傳影像,它似乎不會通過,如果有上傳的影像然后表單將通過。
這是 livewire 組件的外觀,形式如下:
class PostForm extends Component
{
use WithFileUploads;
public $image;
public $comments;
public $post;
public function submit(){
//dd($this);
$this->validate([
'image' => ['mimes:jpeg,bmp,png','required_without:comments'], // Only allow .jpg, .bmp and .png file types.
'comments' => 'required_without:image',
]);
$post = new Post();
$post->user()->associate(Auth::user());
$post->comments = $this->comments;
//dd(isset($this->image));
if (isset($this->image)==true) {
$this->image->store('images', 'public');
$post->image_path = $this->image->hashName();
}
$post->save();
return redirect()->route('posts.index')
->with('success', 'post added');
}
public function render()
{
return view('livewire.post-form');
}
這是它在 livewire postform 刀片視圖中的樣子:
<div>
{{-- If your happiness depends on money, you will never be happy with yourself. --}}
<form wire:submit.prevent="submit" enctype="multipart/form-data">
@csrf
<div class=" my-10">
<label for="comments">Comments:</label>
<textarea wire:model="comments" id="comments" row="5" class=" p-2 bg-gray-200 @error('comments') is-invalid @enderror"></textarea>
@error('comments')
<div class="alert alert-danger">{{$message}}</div>
@enderror
</div>
<div class="form-group">
<input wire:model="image" id="image" type="file" >
</div>
<button wire:click="submit" type="submit" class="btn btn-blue">Create</button>
</form>
uj5u.com熱心網友回復:
nullable由于 $image 默認指定為 null,因此必須傳遞更多驗證標準。
$this->validate([
'image' => ['nullable','mimes:jpeg,bmp,png','required_without:comments'], // Only allow .jpg, .bmp and .png file types.
'comments' => 'required_without:image',
]);
之后,如果圖片為空或用戶沒有選擇它,則驗證器不會驗證影像??。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/391964.html
