数组中多列的唯一实时验证

时间:2021-03-24 08:21:55

标签: php laravel validation unique

对于我的小项目,我需要在数据库中保存多个玩家。 到目前为止一切正常,但我的独特实时验证有一个大问题,即我的规则在我发表评论后就不起作用。

首先,我只在单个变量中保存了一个没有数组的 Player,并且该规则运行良好。一旦我使用了这段代码(您将在下面看到),我的规则就不再起作用,但我必须将它们保存在一个数组中。如果我将变量设置在上面它会起作用,但实时验证显然不起作用。 也许有人知道如何修复它 - 非常感谢!

编辑:我也试过这样的:

'allPlayers.*.lastname' => 'required|unique:players,lastname'

重要的是,一个玩家的名字和姓氏以及头衔(性别)和生日的组合必须是独一无二的

'''

public $players;        


public $player = [
    'title' => '',
    'firstname' => '',
    'lastname' => '',
    'birthday' => '',
    'department' => '',
];
public $allPlayers = [];


public function mount()
{
    $this->allPlayers[] =  $this->player;
}

protected function rules()
{
    return
        [
            'allPlayers.*.title'                         => 'required',
            'allPlayers.*.firstname'                     => 'required|string|max:40',
            'allPlayers.*.lastname'                      => ['required', Rule::unique('players')->where(function ($query) {$query->where('lastname', $this->lastname)
                                                                                                                                   ->where('firstname', $this->firstname)
                                                                                                                                   ->where('birthday', $this->birthday)
                                                                                                                                   ->where('title', $this->title); })],                                                                                                                     
            'allPlayers.*.birthday'                      => 'required|date',
            'allPlayers.*.department'                    => 'required',
        ];
}



public function updated($propertyName)
{
    $this->validateOnly($propertyName);   
}


public function store()
{

    $validatedData = $this->validate();
    dd($validatedData);
    Player::insert($validatedData['allPlayers']);



   
    session()->flash('message', 'Spieler erfolgreich angelegt');
}

'''

0 个答案:

没有答案