Laravel Livewire 验证 - required_without_all 和 required_without 不工作
Laravel Livewire Validation - required_without_all and required_without dont work
我有一个 livewire 组件:
class Cards extends Component
{
use WithFileUploads;
public $question;
public $answer;
public $questionImage;
public $questionImageOrigin;
public $answerImage;
public $answerImageOrigin;
public $questionAudio;
public $questionAudioOrigin;
public $answerAudio;
public $answerAudioOrigin;
public function render()
{
return view('livewire.cards');
}
现在我想创建并验证一张卡片:
public function createCard()
{
$this->validate([
'answerImage' => 'nullable|image|max:3000',
'questionImage' => 'nullable|image|max:3000',
'question' => 'required_without_all: questionImage, questionAudio',
'answer' => 'required_without: answerImage, answerAudio',
'questionAudio' => 'nullable|file|mimeTypes:audio/mpeg|max:3000',
'answerAudio' => 'nullable|file|mimeTypes:audio/mpeg|max:3000'
]);
但这不起作用。
当我插入内容(图像或音频)时出现错误:
The answer field is required when answer image / answer audio is not present.
所以 - 我猜这是行不通的。还是我错了?
也许我必须使用另一种语法?
但是我在文档中什么也没找到。
请帮忙。
您已在规则中添加 space
'answer' => 'required_without: answerImage, answerAudio',
改成这样
'answer' => 'required_without:answerImage,answerAudio',
和
'question' => 'required_without_all: questionImage, questionAudio',
至此
'question' => 'required_without_all:questionImage,questionAudio',
我有一个 livewire 组件:
class Cards extends Component
{
use WithFileUploads;
public $question;
public $answer;
public $questionImage;
public $questionImageOrigin;
public $answerImage;
public $answerImageOrigin;
public $questionAudio;
public $questionAudioOrigin;
public $answerAudio;
public $answerAudioOrigin;
public function render()
{
return view('livewire.cards');
}
现在我想创建并验证一张卡片:
public function createCard()
{
$this->validate([
'answerImage' => 'nullable|image|max:3000',
'questionImage' => 'nullable|image|max:3000',
'question' => 'required_without_all: questionImage, questionAudio',
'answer' => 'required_without: answerImage, answerAudio',
'questionAudio' => 'nullable|file|mimeTypes:audio/mpeg|max:3000',
'answerAudio' => 'nullable|file|mimeTypes:audio/mpeg|max:3000'
]);
但这不起作用。 当我插入内容(图像或音频)时出现错误:
The answer field is required when answer image / answer audio is not present.
所以 - 我猜这是行不通的。还是我错了? 也许我必须使用另一种语法? 但是我在文档中什么也没找到。
请帮忙。
您已在规则中添加 space
'answer' => 'required_without: answerImage, answerAudio',
改成这样
'answer' => 'required_without:answerImage,answerAudio',
和
'question' => 'required_without_all: questionImage, questionAudio',
至此
'question' => 'required_without_all:questionImage,questionAudio',