Laravel 5.6 ERR_TOO_MANY_REDIRECTS on GET request - laravel

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.

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?

Related

Param with blank value triggers (!isset) validation in Laravel

Here is my script:-
if (!isset($request->security_token))
{
// Provide security token
$error = TRUE;
$err_message[] = Lang::get('caption_validation_error.ser_valid_security_token.value');
$err_validation[] = Lang::get('caption_validation_error.ser_valid_security_token.value').' [security_token]';
}
It means, if a param is not "sent", then the validation will be triggered. However, if a param with "blank" value is sent, it must not trigger the validation.
However, when I am hitting the api through POSTMAN app, the security_token with blank value enters the !isset validation.
What am I doing wrong?
This is not that obvious why it's like this because it seems quite strange at first glance. However if you know a bit more about Laravel and you look at app/Http/Kernel.php file you will see in there:
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
which as you can find out from name converts empty strings to null. So in your case when you are sending empty parameter it's considered as null so !isset($null) will be true.
So you have 2 options:
remove this middleware from array - however it will affect the whole application so it might not be the best way if you are not sure about it
Assuming you want to trigger this validation only if parameter is not sent at all instead of
if (!isset($request->security_token))
you can use for example
if (!$request->has('security_token'))
Obviously it's not exactly the same - if you now sent this token and set it to null it won't be still executed but I believe when you now know what's happening with this empty string you can now adjust it exactly to your needs.

Laravel validator vs requests

Hello,
I want to understand how to handle data validation with Laravel 5. I see that this can be done using or the validator, or the request files. The thing is that there are many points I didn't get.
What is the difference between using a request file for validation or the validator class ?
If I have validation conditions, and I want to use them only if the concerned field was submitted, how can I do that ? If I use the "required" keyword, it won't work because it will fail when the field is not submitted. If I don't use it, it will accept empty strings...
Thanks ahead !
1. Theoretically there is no difference between Controller validation and Validation using FormRequest. Normally you should use FormRequest. This will keep your controller clean and Minimal. But some time it is sensible to use Validator within controller, e.g you know there is going to be just one field to validate, then it would be overkill to use FormRequest. So it is a matter of preferance.
2. You don't have to use 'required' if the field is not required. Other validation for that field will still run if that field is submitted. If not submitted nothing will happen.
.......
'money' => 'numeric',
.......
Above Rule will make sure that money field is numeric only if it is submitted. If no submitted no validation error will be thrown.
I hope this helps.
Request classes are the better way to validate requests, because
they help to extract this functionality from the constructor method,
which should be as clean as possible.
Use 'sometimes' validator. http://laravel.com/docs/5.1/validation#conditionally-adding-rules

Laravel 5 - Deal with controller validation and RESTful clients like Postman

I am using laravel's RESTful resource routes and controllers.
I testing my api with PostMan and/or RestClient(firefox)
Everything was fine before I added laravel's controller validation. Now it respond with very strange responses like: status code 0, or even executes the code with not valid data. Or even shows strange results taken from the database (which are not included to the controller at all).
That's creepy.
For example:
public function store(Request $request) {
$this->validate($request, [
'room_id' => 'required|integer',
'body' => 'required'
]);
exit; // Stop the process after the validation...
// ... Logic to STORE the MESSAGE in the database ...
return response(null, 204);
}
This store function must only validate the data and respond with an error if validation fails,
But when I execute it from PostMan, It returns response with list of all rooms which belongs to this user. This is creepy, I cannot realize why this is happening.
When I use the jQuery.ajax() method with the same request options, it works fine. It validates the data and stores the message in the database.
Question : Is there a way to deal with postman?
Question : Is PostMan dangerous? Since the server respond with database info (which is not included to the controller responses).
If you take a closer look at laravel's validation document it says that when data is submitted via HTML form,the validate method would redirect you to a previous page and the error would be flashed into the session, if the validation fails. Although, you will be able to access these error messages in your view using the $error variable. Therefore you will need to submit the data via ajax in-order to get the JSON error message.
When validatioin failed, Laravel needs to know you are sending ajax request or you explicitly want json respond, otherwise it redirects you to previous page.
You can check my answer here
https://stackoverflow.com/a/38478362/6246592

cakephp url validation

One of my models has a 'url' field. I am using the default url validation rule on it. When trying to add a specific url it is not validated.
The url causing validation to fail is http://careers2.hiredesk.net/ViewJobs/JobDetail.asp?Comp=I3&TP_ID=1&PROJ_ID={BDA01FCD-5703-4D40-9197-CCF688633951}
The { character is causing the validation to fail. What workarounds do I have here?
Do you pass your given URL through a URL Encode Function first? See the linked page for an example.

asp.net mvc 3 jquery adding validation message manually

I've been search for quite a while and haven't been able to find an answer to this.
I am using asp.net MVC 3 with unobtrusive validation. My model is bound with data annotations for simple validation (required fields, regex, etc..). However, I have more complex validation that occurs on the server. I'm doing an ajax post which returns me validation add'l messages that come from my domain model. All I want to do is put those validation messages on the form in the place of the existing ones. I don't want to use partial views since all I've really got are messages coming back and there isn't a need to refresh the whole view. Also, I'm not adding new rules or new inputs to the form so $.validator.unobtrusive.parse won't work. These are just messages I want to put on the form. The $.post call returns a list of message with which field is/was affected and the validation message.
Here's kind of what I'm looking to do
$.post(url, { someData}, function (data) {
for (message in data.Messages) {
$("#form").validate().addMessage(message.Field, message.Text);
}
});
Thanks for your help
Per request, here's a sample of the returning JSON, it's fairly simple.
{"id":0,"messages":["Level":0,"Message":"Style is required","Name":"Style"}],"operationResult":false}
messages is a list of objects that contain the severity level, the property the error belonged to and the error message. I would use the name in the messages object to match where it want on the form.
I had exactly the same requirement, I actually found the following method.
var validator = $("form").validate();
validator.showErrors({field : "Error Message"})

Resources