Laravel 5.5自定义Request的required验证规则失效求助
Hey there, let's troubleshoot this Laravel 5.5 validation issue together! That 302 redirect when using the required rule is super frustrating, especially since it works fine without it and your PHPUnit tests pass with valid data. Here are the most common fixes to check out:
1. Check for CSRF Token Issues
Laravel 5.5 automatically validates CSRF tokens for POST/PUT/DELETE requests. If your request isn't including the CSRF token correctly, validation might fail silently and trigger a 302 redirect.
- If using Blade templates, ensure your form includes the
@csrfdirective. For non-Blade requests, manually pass the_tokenparameter with the value fromcsrf_token(). - For API requests, you can exclude the route from CSRF protection by adding it to the
$exceptarray inapp/Http/Middleware/VerifyCsrfToken.php.
2. Inspect Validation Error Messages
The 302 redirect is likely sending you back to the form with error messages, but you might not be seeing them.
- Add an error display block to your view to confirm if
userIdis failing the required check:@if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif - Alternatively, temporarily override the
failedValidationmethod in your custom Request class to dump errors directly:
This will show you exactly what's failing, which might revealprotected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator) { dd($validator->errors()); }userIdisn't being passed correctly.
3. Verify Parameter Name Spelling
Double-check that the parameter name in your validation rule matches exactly what's being sent. Laravel is case-sensitive here—if your rule uses 'userId' => 'required' but the request sends user_id (snake_case), validation will fail.
4. Confirm the Request Receives the Parameter
Temporarily dump the full request data to confirm userId is present before validation runs. You can add this to your Request's authorize method:
public function authorize() { dd(request()->all()); // Shows all incoming request data return true; }
If userId doesn't appear here, the issue is that your request isn't sending the parameter correctly—even though your PHPUnit test does. This could be due to a form field typo or middleware modifying the request.
5. Check Route and Request Method Mismatch
Ensure your route uses the correct HTTP method (POST/PUT vs GET). If your Request is meant for POST but the route is defined as GET, parameters might be in the query string instead of the request body (which the required rule checks by default). For GET requests, confirm userId is in the query string.
6. Rule Out Middleware Interference
Custom or third-party middleware can sometimes modify request data before it reaches the validation layer. Disable non-essential middleware temporarily to see if the issue resolves.
7. Compare Test vs Real Request Data
Your PHPUnit test passes, but the real request fails—this means the test is sending data differently than the actual scenario.
- Check how you're passing
userIdin tests (e.g.,$this->post('/route', ['userId' => 123])) vs the real request. Maybe the form uses a nested array likeuser[userId]instead of a top-leveluserId, or there's a field name typo in the form.
Once you identify which of these is causing the problem, fixing it should get your validation working as expected. Let me know if you need deeper dive into any of these steps!
内容的提问来源于stack exchange,提问作者Tom Headifen

