-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate an encoded route key #9
Comments
Hello, @ivanvermeyen. Package you provided seems useful. Good to have it as separate package as you've done it. So, it's validating if model exists using encoded key and overwriting this id in request with decoded one? If I will execute: $validated = validate([
'article' => new RouteKeyExists(Article::class)->replace(),
]); This will have actual Article id?
So much magic :} |
Indeed, but you'll need extra parentheses: $validated = validate([
'article' => (new RouteKeyExists(Article::class))->replace(),
]); I was in doubt if I should add an optional second constructor parameter to enable $validated = validate([
'article' => new RouteKeyExists(Article::class, 'article'), // store the original ID in request('article')
]); The real logic for checking if it exists is in the models if ( ! $model = $this->model->resolveRouteBinding($value)) {
return false; // validation fails...
} And then some checks if I should replace or add the original ID to the request. |
One more possible solution is static factory: $validated = validate([
'article' => RouteKeyExists::model(Article::class)->replace(),
]); Or name it as you like: $validated = validate([
'article' => RouteKeyExists::replace(Article::class),
]); |
Thanks for the input! That's also an interesting approach. Looks cleaner than newing up the rule manually. I've added the static constructor method RouteKeyExists::model(Article::class)->replace() |
Yeah, this one looks good. We could add suggestion for Laravel Optimus users to readme or composer.json that there is optional way to validate if encoded models are exists. But first of all I need to start using them myself. I've never done it in any production projects. |
Hi,
Just FYI: I've put together a validation rule that uses the
resolveRouteBinding()
method on a model to check if a record exists in the database. Sort of like Laravel'sexists
rule but then model based.https://github.com/codezero-be/laravel-route-key-exists
This works very well with the
OptimusEncodedRouteKey
trait. 👍Maybe this can be of use to anyone.
The text was updated successfully, but these errors were encountered: