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

How to handle route model binding in Form Request #953

Closed
1 task done
james-astalty opened this issue Feb 19, 2025 · 3 comments
Closed
1 task done

How to handle route model binding in Form Request #953

james-astalty opened this issue Feb 19, 2025 · 3 comments

Comments

@james-astalty
Copy link

Scribe version

4.40.0

Your question

When you have a FormRequest that uses bound models as part of the request (for example the Rule::unique(...)->ignore(...) method, Scribe will throw an error because the model won't be there and the bound value is null;

class UpdateOrganisationRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['string', 'required', 'max:255'],
            'email' => [
                'bail',
                'string',
                'required',
                'max:255',
                'email',
                Rule::unique(Organisation::class, 'email')
                    ->ignore($this->organisationModel()->id),
            ],
        ];
    }

    private function organisationModel(): Organisation
    {
        /** @var Organisation $organisation */
        $organisation = $this->route('organisation');

        return $organisation;
    }
}
TypeError 

App\...\UpdateOrganisationRequest::organisationModel(): Return value must be of type Domain\Organisation, null returned

Of course when this isn't actually running an UPDATE request with a bound model, organisationModel will return a type error because $this->route('organisation') is actually null.

I know this isn't a bug of Scribe because the rules method has to run in order to extract the data but I was wondering if there is a suggested way to handle this?

One way I was thinking is being able to tell Scribe to ignore certain form requests using an attribute and then just manually providing the input data and rules.

Docs

@shalvah
Copy link
Contributor

shalvah commented Feb 20, 2025

You could also try customising how the FormRequest is instantiated.

@james-astalty
Copy link
Author

You could also try customising how the FormRequest is instantiated.

We'd have to do that for each request though wouldn't we? I'm sure others have run into this problem.

@shalvah
Copy link
Contributor

shalvah commented Feb 20, 2025

We'd have to do that for each request though wouldn't we?

I guess there should be a callable $default parameter. But Scribe's default is just new $class, so you can just check the class name and fall back to that.

One way I was thinking is being able to tell Scribe to ignore certain form requests using an attribute and then just manually providing the input data and rules.

If you wish to do this, in v5, you can configure a strategy to ignore some endpoints, and then add a static data strategy:

'bodyParameters' => [
  ...configureStrategy(
    Defaults::BODY_PARAMETERS_STRATEGIES,
    Strategies\BodyParameters\GetFromFormRequest::wrapWithSettings(except: [...])
  ),
  Strategies\StaticData::withSettings(
    only: [...],
    data: [
      'param' => [
        // properties,
      ]
  ]),
)

@shalvah shalvah closed this as completed Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants