I have an option list in my front end and the data is retrieved from the DB table, named countries and nicename column.
Trying to prevent the user request for value which is not in countries table and in nicename column. I have created custom validation, which works fine but it looks like it’s not too professional way of doing this control. I have seen somewhere that there is shorter way of doing this validation by in:table_name,column_name. But it did not work for me Maybe I am doing something wrong?
$rules = [
'shop_country' => [
'required',
function ($attribute, $value, $fail ) {
$found_country = '';
$countries = Country::get()->toArray();
foreach ($countries as $country) {
if ($country['nicename'] === $value) {
$found_country= $country['nicename'];
}
}
if($found_country === ''){
$fail('The country is not valid!');
}
}],
]
Any help would be highly appreciated.
>Solution :
You can define it as:
'shop_country' => ['required|exists:table_name,column']
