add validation for enabled labels, rename enable (to enabled) in labels table, and update the enabled attribute of corresponding labels to false when cycle saved

This commit is contained in:
Moradi
2025-03-13 12:45:00 +03:30
parent 034000bcef
commit 1d98af0113
6 changed files with 82 additions and 5 deletions
+29
View File
@@ -0,0 +1,29 @@
<?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.');
}
}
}