z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.13k stars 2.81k forks source link

Form Validation double value verification #5733

Closed carlospinhod closed 1 year ago

carlospinhod commented 1 year ago

Description:

Hi, i need to validate if a value is unique but i have to filter the "unique search" with other field for example: if the username is unique for this club_id. hope you can help me! ps: the ideia is to validate before submiting, and the club id is a variable i get from php

Steps To Reproduce:

table example: (user_id, username, club_id)

$form->text('username')->updateRules(['required', "unique:user_table,username,{{id}}"]);

alexoleynik0 commented 1 year ago

This should help - link to SO.

Basically 'unique:user_table,username,NULL,id,clud_id,' . $form->model()->club_id or something similar.

Or use more verbose OOP-based approach as described in this section of Laravel docs (last example).

'username' => [Rule::unique('user_table')->where(fn (Builder $query) => $query->where('club_id', $club_id))]

carlospinhod commented 1 year ago

@alexoleynik0 thank you for your hints, i have solved my problem using something like this

$form->number('id','ID')->rules(['required' => Rule::unique('user_table')->where(fn(Builder $query) => $query->where('id', '!=', $form->model()->id)->where('club_id', $club_id))])->required();