last day (15 days later) » 

9:09 PM
0
Q: Laravel 5.2 Form Request Validation Not Showing Errors In View

Mike BarwickSo, this appears to be a big issue for people upgrading from 5.* to 5.2. I searched online for a while trying many different methods, but none would work. Appears it's all related to the new web middleware. I've done the usual troubleshooting I've read online, i.e. moving session and share errors...

 
I'm not 100%, but here's a few things; The exception thrown should be a ValidationException and not an HttpResponseException. If you open up App\Exceptions\Handler.php you'll see a $dontReport array at the top that has the ValidationException. if you must use your own custom HttpResponseException, then add that to the $dontReport array. Otherwise, use the baked in ValidationException class instead.
 
Bit confused by your suggestion. ValidationException is already under $dontReport...I'm using the default Form Request created from php artisan make:request
 
I get that, so where is this HttpResponseException being thrown from? That's not something that the validator throws. If you don't want that Exception to be displayed, then add it to the $dontReport array. That should be all you need.
 
No idea...that's my issue lol it appears to be a common issue with Laravel 5.2 upgrade. Session not passing errors to view. Frustrating.
 
Can I please see your full validation code (you can omit the rules for brevity sake)
 
9:09 PM
Post updated... :) Its retarded simple. Worked before 5.2 update.
 
I think there's something else going on here. That Exception isn't under the failedValidation on the trait in 5.2
This is what the ValidatesWhenResolvedTrait looks like in 5.2:
protected function failedValidation(Validator $validator)
{
throw new ValidationException($validator);
}
 
Something weird is definitely happening...no idea though. Driving me nuts.
Exactly...that's what being thrown...
nm...it's not
 
Actually, I found a GitHub topic about this issue: github.com/laravel/framework/issues/7178
Are you still trying to use the FormRequest? Because the Request you've shown uses the ValidatesWhenResolvedTrait so it shouldn't throw that error.
Also did you add the HttpResponseException to the $dontReport array?
 
Yeah, would prefer to use FormRequest
No I didn't...let me try that
 
Yeah I can see in the source code here that the FormRequest class does implement the ValidatesWhenResolved trait, but it override the failedValidation function and throws an HttpResponseException instead
 
9:16 PM
Adding HttpResponseException didn't help or solve
Hmm, so what's my options here? Shit out of luck?
 
No. I'm trying to re-create this locally so I can help figure out what's going on. :)
 
Awesome. Appreciate man!
 
I found the issue and the pull request for it to be fixed in Laravel 5.2 acknowledged by Taylor: github.com/laravel/framework/pull/13842/files#r65646381
So to resolve this, you can make your own FormRequest class, copy everything from that, and make the change outlined in the Git Repository
Then make everything extend that class - in 5.3 you won't have to do this, but this will resolve it for now.
 
Okay...bit confused on "making own FormRequest" class
and what happens when I upgrade to to L5.3? Would I need to remove this class?
 
It won't break anything, but you'll be given the option to stop extending your new form request class
Make your own "Request.php" file, call it "ValidatorRequest.php"
Copy all contents from Request.php into that file
Override the failedValidation method from the parent FormRequest class

/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Contracts\Validation\ValidationException
*/
protected function failedValidation(Validator $validator)
{
throw new ValidationException($validator);
}
Now have all of your requests extend this new ValidatorRequest class as opposed to the Request class.
 
9:29 PM
App/Http/request folder? Request.php is blank
Can you write out an answer? I'll accept :D
 
Wait. How is it blank?
 
Appreciate the help!!
 
That file should not be Blank in L5.2.1+
 
Ah, yes, that's fine.
I thought you meant completely empty...:)
Let's make sure this works before I post an answer.
 
9:31 PM
So essentially, I'm replacing this class
 
You can actually just extend Request.
And override the failedValidation method within
 
Example? This is new territory for me
 
Make a new file in the same directory called "ValidatorRequest.php".

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

Class ValidatorRequest extends FormRequest {
protected function failedValidation(Validator $validator)
{
throw new ValidationException($validator);
}
}
Sorry about the formatting...chat isn't so good at handling it. Now your requests will extend this class.
php artisan make:request ValidatesUserCreationRequests
 
Agh, okay. I got this. I misunderstood. One sec.
 
modify the file, instead of extends Request use extends ValidatorRequest. Make sure you typehint the dependency or manually include it, use App\Https\Requests\ValidatorRequest
 
9:39 PM
Now it's saying my FormRequest doesn't exist...
Class App\Http\Requests\CreateUpdateTaskRequest does not exist
 
Can you show me the contents of that file?
 
Can you do a composer dumpautolaod
autoload*
 
I should remove that use Request...but error still appears. Irrelevant
agh, right
 
And the ValidaotrRequest has use Illuminate\Foundation\Http\FormRequest; right? Also, be back momentarily
 
9:42 PM
I think use Illuminate\Contracts\Validation\Validator; needs to be included
Now...after I inject the validator class...I get this:
 
No, the trait should do that for you.
 
Ah yes, my own ignorance, there will be further use statments.
You'll also need: use Illuminate\Contracts\Validation\ValidationException;
And you be be able to use \Illuminate\Support\Facades\Validator not the Contract
 
Now back at original issue...minus different error...FML lol
*different exception passed
 
This Whoops display seems like it's the old one from Laravel 4.2
Are you sure this project is fully up to date with Laravel 5.2?
 
9:49 PM
Laravel Framework version 5.2.38
 
From your composer.lock ?
Super confused. This shouldn't be an issue. There's some underlying issue here.
If you want, you can add me to your Git project and I'll take a look into it further for you.
 
Yeah...big time confusing. I might install a fresh new Laravel 5.2 version and transfer my code over. gun to head
Grabbed version via terminal
Appreciate the help man....I think it's almost easier just to transfer code to a new build. I'm not a fan of hacks and usually causes more issues long term anyways
 
Honestly, that's what we ended up doing. Just wrapped up a 4.2 to 5.2 migration, took about 3 weeks to do it with 2 guys. However, our project is 2+ million lines of code, so it's expected that it would take that long. Smaller projects shouldn't be too bad.
 
Damn man!
We don't have THAT much..it's going to be annoying. But far less hassles (I hope). Wish me luck lol Cheers - and thanks again!
 
No worries. Good luck buddy.
 

last day (15 days later) »