0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

laravel 他のフィールドの値によって必須としたいときのバリデーション

Posted at

やりたいこと


typeが1の場合のみdetailを入力必須にしたい
または入力禁止にしたい

{
        "type": 1,
        "detail": "XXX"
}

結論

FormRequest
public function rules(): array
    {
        return [
           "type"  => ['required', 'integer', new Enum(Type::class)],
        // 入力必須にしたいとき
           "detail"  => ['string', 'nullable', Rule::requiredIf(条件)]
        // 入力禁止にしたいとき
           "detail"  => ['string', 'nullable', Rule::prohibitedIf(条件)]
        ];
    }

FormRequest
"detail"  => ['string', 'nullable', Rule::prohibitedIf($this->type !== 1)]

$this->type !== 1 の1はenumとか使うけど

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?