0

I currently have a few validation rules as follows:

\Route::post("/logError", "LogController@logError");

 //In LogController
 public function logError(Request $request) { 
    $rules = [
        "name" => "required", 
        "message" => "required",
        "level" => "in:info,debug,error"
    ];

    $this->validate($request,$rules);
    // Log the message or do whatever
});

This works fine for inputs like

[ "name"=>"Not found", "message" => "Element not found", "level" => "error" ]
[ "name"=>"Not found", "message" => "Element not found" ]` 

However it throws an exception for inputs like

[ "name"=>"Not found", "message" => "Element not found", "level" => "randomgibberish" ]

My question is, since "level" is optional, is there a built-in way to validate the input and just remove optional elements that are not valid from it instead of throwing a validation exception? If not would I have to override the controller validation method to achieve this?

2
  • Ignoring invalid input is a slippery slope. Consider a typo on the client side, sending 'eror' level. Suppressing level may keep the typo hidden for ages complicating maintenance. If it is still your intention, input sanitation should not be coupled with validation. I'd pass the rules and the result of getValidationErrors() to sanitiser, and modify the request there. Commented Feb 8, 2017 at 10:43
  • @AlexBlex I guess the problem was sanitizing input and validating input were entangled in my head (possibly because filter_var in PHP can do both). I'll try to untangle them from my head (and then my code). Commented Feb 8, 2017 at 12:06

1 Answer 1

1

The validator does not modify the request input data, you need to make some sort of middleware or function in your controller that checks for the input parameters and compares them with the ones you want Laravel to check.

So yes, I think you'll need to extend the validation method, or maybe try with an helper function that does only that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.