ValidateNormScope.php
1 <?php 2 3 namespace App\Rules; 4 5 use App\Models\Norm; 6 use Illuminate\Contracts\Validation\InvokableRule; 7 8 class ValidateNormScope implements InvokableRule 9 { 10 public function __construct(public ?Norm $norm) 11 { 12 } 13 14 /** 15 * Run the validation rule. 16 * 17 * @param string $attribute 18 * @param mixed $value 19 * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail 20 * @return void 21 */ 22 public function __invoke($attribute, $value, $fail) 23 { 24 if (!$this->norm) { 25 $fail('Norm field is required'); 26 return; 27 } 28 29 $validScopes = $this->norm->scopes->pluck('name'); 30 if (!$validScopes->contains($value)) { 31 $fail('Scope ' . $value . ' does not exist for norm ' . $this->norm->name); 32 } 33 } 34 }