unique validation

I need to change the value before comparing it to the database. Example: https://google.nl -> google.nl
7 Replies
BlackShadow
BlackShadowOPβ€’2y ago
I know i can do the following:
->rules([function () {
return function (string $attribute, $value, Closure $fail) {
if (Source::where('url', parse_url($value)['host'])->exists()) {
$fail("The specified URL is already being utilized.");
}
};
},
->rules([function () {
return function (string $attribute, $value, Closure $fail) {
if (Source::where('url', parse_url($value)['host'])->exists()) {
$fail("The specified URL is already being utilized.");
}
};
},
But was hoping it can be done with the unique() instead and also make it possible on the edit page ignoreable
RickDB
RickDBβ€’2y ago
afterStateUpdated? and do validation in there
BlackShadow
BlackShadowOPβ€’2y ago
Problem is that if i update it on the edit page it will also say: "The specified URL is already being utilized."
LeandroFerreira
LeandroFerreiraβ€’2y ago
->rules([function ($record) {
return function (string $attribute, $value, Closure $fail) use ($record) {

$value = parse_url($value)['host'];

$exists = Source::whereUrl($value)
->when(filled($record), function (Builder $query) use ($record) {
return $query->where('id', '<>', $record->id);
})->exists();

if ($exists) {
$fail("The specified URL is already being utilized.");
}
};
}])
->rules([function ($record) {
return function (string $attribute, $value, Closure $fail) use ($record) {

$value = parse_url($value)['host'];

$exists = Source::whereUrl($value)
->when(filled($record), function (Builder $query) use ($record) {
return $query->where('id', '<>', $record->id);
})->exists();

if ($exists) {
$fail("The specified URL is already being utilized.");
}
};
}])
BlackShadow
BlackShadowOPβ€’2y ago
Oh wow, thanks! Good to know you can get 'record' like that πŸ™‚
LeandroFerreira
LeandroFerreiraβ€’2y ago
Could be better, but it should work I think πŸ‘
BlackShadow
BlackShadowOPβ€’2y ago
Yea it does
Want results from more Discord servers?
Add your server