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

Route Model Binding Form Request Validation Error #44

Closed
seanthepottingshed opened this issue Aug 12, 2015 · 5 comments
Closed

Route Model Binding Form Request Validation Error #44

seanthepottingshed opened this issue Aug 12, 2015 · 5 comments
Labels
Milestone

Comments

@seanthepottingshed
Copy link

Excellent package, should be part of the Laravel core in my opinion, but there you go. However, I can't seem to get this to work using Route Model binding for edit form. I am using the Eloquent-Sluggable package in order to reference via slug rather than id:

https://github.com/cviebrock/eloquent-sluggable

Here is an example using Menu as an example:

app/Http/RouteServiceProvider.php

public function boot(Router $router)
{
        $router->bind('menus', function($slug)
        {
           return Menu::findOrThrowException($slug);
        });
}

app/Http/Providers/UpdateMenuRequest.php

public function rules()
{
    return [
        'title' => 'required|unique:menus,title,' . $this->route('menus')->id,
       ];   
}

menus/edit.php

{!! JsValidator::formRequest('App\Http\Requests\UpdateMenuRequest', '#menu') !!}

I get the following error:

ErrorException in UpdateMenuRequest.php line 27:
Trying to get property of non-object (View: /home/vagrant/Code/TpsCMS/resources/views/admin/menus/edit.blade.php)

Line 27 being...

'title' => 'required|unique:menus,title,' . $this->route('menus')->id,

Could you pleas advise me further?

@torrentalle
Copy link
Member

I can't make work your example without JsValidation
Did you try if your validation work without using JsValidation?

@torrentalle
Copy link
Member

Ok, I found the error.
Before Javascript Validations are generated, Request should be passed to FormRequest.
I'm looking how do that

@torrentalle torrentalle mentioned this issue Aug 13, 2015
torrentalle added a commit that referenced this issue Aug 13, 2015
 Route Model Binding Form Request Validation Error #44
@torrentalle torrentalle added this to the 1.1.1 milestone Aug 13, 2015
@torrentalle torrentalle reopened this Aug 13, 2015
@torrentalle
Copy link
Member

I the master branch this issue is resolved

@seanthepottingshed
Copy link
Author

torrentalle you are a legend. I was looking through the source yesterday but couldn't figure out how to do it. In answer to your earlier question the UpdateMenuFormRequest FormRequest validation works as intended with the form model binding.

@torrentalle
Copy link
Member

Republish Javascript files after composer update to apply the patch

composer update
php  artisan vendor:publish --provider="Proengsoft\JsValidation\JsValidationServiceProvider" --tag="public" --force

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

No branches or pull requests

2 participants