Skip to content
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

Open
ivanvermeyen opened this issue Oct 25, 2017 · 5 comments
Open

Validate an encoded route key #9

ivanvermeyen opened this issue Oct 25, 2017 · 5 comments

Comments

@ivanvermeyen
Copy link
Contributor

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's exists 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.

@antonkomarev
Copy link
Member

antonkomarev commented Oct 26, 2017

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?

dd($validated['article']);

So much magic :}

@ivanvermeyen
Copy link
Contributor Author

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 replace or add. I think it's slightly less clear, but maybe prettier to look at without those extra parentheses? I think I will add this for flexibility...

$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 resolveRouteBinding() method, like the one in your trait, so it's not tightly coupled to a single use case. :) All I'm doing is this in essence:

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.

@antonkomarev
Copy link
Member

One more possible solution is static factory:

$validated = validate([
    'article' => RouteKeyExists::model(Article::class)->replace(),
]);

Or name it as you like: make or anything else. Or even this:

$validated = validate([
    'article' => RouteKeyExists::replace(Article::class),
]);

@ivanvermeyen
Copy link
Contributor Author

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 model(). 👍

RouteKeyExists::model(Article::class)->replace()

@antonkomarev
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants