1

I have a custom Request class which deals with the validation of a form. This form uses 'GET' and will filter down all the results the User can see on the page.

My rule for the start date:

'date_start' => 'nullable|date|required_with:date_end',

is causing a message:

ERR_TOO_MANY_REDIRECTS

My controller looks like this:

public function index (ApprovedSubmissionsFilterRequest $request)
{

    ...

I believe that this is because when the validation fails, it sends a GET request back to the index method, which once more fails the validation and redirects back to the index method etc. etc.

How do I avoid this loop? I do not want to use a POST request instead of a GET.

Here is my route:

Route::get('formSubmission', 'FormSubmissionController@index')
 ->name('formSubmission.index');

Thank you.

NOTE (edit):

Not all validation errors cause this - it only seems to be the required_with that is causing the issue. Somebody has mentioned it here previously.

party-ring
  • 1,761
  • 1
  • 16
  • 38
  • 2
    nothing to do with validation, it has to do with the URL you are trying to access when it has an error – abr May 03 '19 at 11:56
  • You might need to customize the handler, see: https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Exceptions/Handler.php#L248. Or customize the response given by the ValidationException: https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Exceptions/Handler.php#L230 – Adam Rodriguez May 03 '19 at 20:46

1 Answers1

0

I tried your code in my project, and cannot reproduce the problem. So do you really use the correct validation rule, because from the docs, the required_with takes an effect only if the other field that you are trying to validate exists in the request. So in your case date_start should not be present in the request and date_end should exist in order for this validation to take place:

required_with:foo,bar,...

The field under validation must be present and not empty only if any of the other specified fields are present.

  • Also from the github issue that you have mentioned, you can debug in the exception handler what happens when ValidationException is thrown

  • Your last note, have you tried with all validation rules except that one if it passes?

Community
  • 1
  • 1
nakov
  • 13,938
  • 12
  • 60
  • 110
  • Yeah I was trying to force the message to come up by only entering 1 out of the 2 dates. The other validation rules work successfully (e.g. if I change `nullable` to `required`, it will redirect back with the error message) which is why I am most confused. I will take a look in the exception handler but it seems like strange behaviour to me. Thanks for getting back to me. – party-ring May 03 '19 at 12:52
  • i would recommend creating a custom Rule class and writing your own logic! – Achraf Khouadja May 03 '19 at 14:12