Validation attribute Exists
don't respect where
parameter
#133
-
Thanks for this powerful package! Example Data class use Spatie\LaravelData\Attributes\Validation\Exists;
class Test extends \Spatie\LaravelData\Data
{
public static function rules()
{
return [
'location_id' => [
new Exists(
AnyModel::class,
'id',
where: function ($query) {
// any condition
return $query->where('project_id', 1);
}
),
],
];
}
} What's happeningParameter Method When returning a string, the where condition is lost. The documentation clearly shows that the line is not suitable for such a case Here is the condition if (! is_object($rule) ||
$rule instanceof RuleContract ||
($rule instanceof Exists && $rule->queryCallbacks()) ||
($rule instanceof Unique && $rule->queryCallbacks())) {
return $rule;
} I still don't understand how to fix it, but I think it needs to be fixed. For now, I'm using the native rule use Illuminate\Validation\Rule;
class Test extends \Spatie\LaravelData\Data
{
public static function rules()
{
return [
'location_id' => [
Rule::exists(AnyModel::class, 'id')->where(fn($query) => $query->where('project_id', 1))
],
];
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Normally this should be fixed in v2 since we won't try to convert rules into strings anymore. |
Beta Was this translation helpful? Give feedback.
-
It still doesn't work in v2. Laravel converts the rule to a string and the where condition is lost |
Beta Was this translation helpful? Give feedback.
-
This should be fixed with the next release (81bae97) |
Beta Was this translation helpful? Give feedback.
This should be fixed with the next release (81bae97)