@jagdish-j-ptl Why not just use the existing Rule::requiredIf rule?
Your custom rule is going to essentially result in N+1 problems, because you’re doing a database query for every field. You should just fetch all required fields in one query, and then add validation for the fields that require it:
class SomeFormRequest extends FormRequest
{
public function rules(): array
{
return [];
}
public function withValidator(Validator $validator): void
{
// Get all fields that are required
$attributes = RuleTable::query()->where('required', '=', true)->get();
// Convert each attribute to associative array of field name => rules
$rules = $attributes->mapWithKey(function ($attribute) {
return [
$attribute->field => ['required'],
];
})->toArray();
// Now add the rules to the validaor instance
$validator->addRules($rules);
}
}