Giter Site home page Giter Site logo

Comments (5)

ivanvermeyen avatar ivanvermeyen commented on May 22, 2024 1

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()

from laravel-optimus.

antonkomarev avatar antonkomarev commented on May 22, 2024

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 :}

from laravel-optimus.

ivanvermeyen avatar ivanvermeyen commented on May 22, 2024

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.

from laravel-optimus.

antonkomarev avatar antonkomarev commented on May 22, 2024

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),
]);

from laravel-optimus.

antonkomarev avatar antonkomarev commented on May 22, 2024

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.

from laravel-optimus.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.