我可以改进现有的验证器,当另一个(数组)属性包含特定值时验证请求中的属性是否存在?

问题描述 投票:0回答:1

我有这个刀片模板,用于我正在网络应用程序中创建的表单:

<?php

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

use function Livewire\Volt\{rules, state};

state([
    'additionalDanceGenres' => [],
    'otherAdditionalDanceGenre' => '',
]);

rules(
    [
        'additionalDanceGenres' => 'array',
        //'otherAdditionalDanceGenre' => 'required_if:additionalDanceGenres.*,other|string',
    ]
);

$store = function () {
    $validated = $this->validate();

    $otherAdditionalDanceGenreValidator = Validator::make($this->all(), [
        'otherAdditionalDanceGenre' => Rule::requiredIf(in_array('other', $this->additionalDanceGenres)),
    ]);

    if ($otherAdditionalDanceGenreValidator->fails()) {
        $this->addError('otherAdditionalDanceGenre', 'The other additional dance genres are required.');
    }
    dd($this);
    // do something with the validated request
}

?>

<div>
    <form wire:submit="store">
        @php
        $danceGenres = array(
        array("id" => "ballet", "name" => "Ballet"),
        array("id" => "other", "name" => "Other")
        );
        @endphp

        <x-input-label for="additionalDanceGenre">Please select any additional dance genres</x-input-label>
        <x-input-group>
            @foreach ($danceGenres as $genre)
            <span>
                <input type="checkbox" wire:model.live="additionalDanceGenres" name="additionalDanceGenre" value="{{  $genre['id'] }}" id="{{$genre['id']}}" />
                <x-input-label for="{{$genre['id']}}">{{ $genre['name'] }}</x-input-label>
            </span>
            @endforeach
        </x-input-group>

        @if(in_array('other', $additionalDanceGenres))
        <x-input-group>
            <x-input-label for="otherAdditionalDanceGenre">Your additional dance genres</x-input-label>
            <x-text-input wire:model="otherAdditionalDanceGenre" id="otherAdditionalDanceGenre" name="otherAdditionalDanceGenre" />
        </x-input-group>
        @endif


        @if ($errors->any())
        <x-input-error :messages="$errors->get('additionalDanceGenres')" />
        <x-input-error :messages="$errors->get('otherAdditionalDanceGenre')" />
        <hr class="my-2">
        @endif

        <x-primary-button>{{ __('Submit') }}</x-primary-button>
    </form>
</div>

这个效果很好;当

otherAdditionalDanceGenre
位于
other
时,我会得到对
additionalDanceGenres
的适当验证。我尝试使用规则字符串(
rules
中的注释掉值;Laravel 新手,不确定这些规则的确切名称),但我在那里的东西不起作用。

我想将调用传递给

Rule::requiredIf
数组中的
rules

rules(
    [
        'additionalDanceGenres' => 'array',
        'otherAdditionalDanceGenre' => Rule::requiredIf(in_array('other', $this->additionalDanceGenres)),
    ]
);

但这会导致

Using $this when not in object context
错误。

有没有一种方法可以完成此验证而无需编写自定义验证器?

php laravel laravel-blade laravel-livewire
1个回答
0
投票

您可以创建一个规则并在内部使用它,这样您就不必重新发明自行车

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class AvailableGenre implements Rule
{
    protected array $additionalDanceGenres = ['other'];


    public function passes($attribute, $value)
    {
        return in_array('other', $this->additionalDanceGenres);
    }

  
    public function message()
    {
        return 'The :attribute must be valid');
    }
}

然后使用它

$otherAdditionalDanceGenreValidator = Validator::make($this->all(), [
    'otherAdditionalDanceGenre' =>  [new \App\Rules\AvailableGenre()],
]);

© www.soinside.com 2019 - 2024. All rights reserved.