Laravel livewire 表单不允许仅从评论中输入,需要两者或图像本身
Laravel livewire form won't allow only input from comments, requires both or image on its own
我正在尝试输入允许用户输入评论的帖子 and/or 图片,但是我的 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 blade 视图中的样子:
<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>
必须传递更多 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',
]);
之后,如果图片为null或者用户没有选择,则验证器不对图片进行验证。
我正在尝试输入允许用户输入评论的帖子 and/or 图片,但是我的 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 blade 视图中的样子:
<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>
必须传递更多 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',
]);
之后,如果图片为null或者用户没有选择,则验证器不对图片进行验证。