Files
cssd-back/app/Rules/ValidEnabledLabels.php
T

30 lines
755 B
PHP

<?php
namespace App\Rules;
use App\Models\Label;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class ValidEnabledLabels implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$formattedVal = pluckValues($value,'id');
$validIds = Label::whereIn('id', $formattedVal)
->where('enabled', true)
->pluck('id')
->toArray();
if(count(array_diff($formattedVal, $validIds)) !== 0)
{
$fail('the :attribute must be enabled.');
}
}
}