Laravel Livewire验证-required_without_all和required_without不起作用

时间:2020-07-30 19:23:21

标签: php laravel laravel-validation laravel-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.

所以-我想这不起作用。还是我错了? 也许我必须使用另一种语法? 但是我在文档中什么都没找到。

请帮助。

1 个答案:

答案 0 :(得分:2)

您已在规则中添加了空间

'answer' => 'required_without: answerImage, answerAudio',

将其更改为此

'answer' => 'required_without:answerImage,answerAudio',

'question' => 'required_without_all: questionImage, questionAudio',

对此

'question' => 'required_without_all:questionImage,questionAudio',