Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 725 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lumen FatalThrowableError on validation

#1
I've been struggling with this for a while now.
Here's the code I've got.

> public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|max:100'
]);
if ($validator->fails()) {
//do something
}
}

The problem is that I get a FatalThrowableError right in my face with the following message:

> Call to a member function parameter() on array

I can't find what I'm doing wrong. I'd appreciate some help here.
And also, I've had this validation before which worked:

$this->validate($request, [
'name' => 'required|unique:developers|max:100'
]);
But the thing with this one is, I had no idea how to catch when the validation failed. Is it possible to catch the validation fail when using it this way?

Using version: "laravel/lumen-framework": "5.2.*"
Reply

#2
A `FatalThrowableError` exception is low level exception that is thrown typically by the symfony debug `ErrorHandler`. In lumen the queue worker, PhpEngine, console kernel and routing pipeline uses it as well.

Make sure of the following

1. That you have copied `.env.example` to `.env`
2. If you are using Facades, make sure that you enabled it inside `bootstrap/app.php` by uncommenting the line.

`$app->withFacades();`

Inside Lumen 5.2.8 either of the following would work.

The following will actually return a valid JSON object with the errors. You did not elaborate on your use case why that is not sufficient. What is nice with using the `validate` call like this is it actually returns a `422` http status code, which implies an unprocessed entity.

<!-- language: php -->

$app->get('/', function (Request $request) {
$this->validate($request, [
'name' => 'required'
]);
});

Using the facade works as well, albeit is returns a `200` status code.

<!-- language: php -->

$app->get('/', function (Request $request) {
$validator = Validator::make($request->only(['name']), [
'name' => 'required'
]);

if ($validator->fails()) {
return ['error' => 'Something went wrong'];
}
});

If you still do not come right with the `Validator::make` you can catch the default Validation exception using. It feels a little hacky.

<!-- language: php -->

$app->get('/', function (Request $request) {
try {
$this->validate($request, [
'name' => 'required'
]);
} catch (\Illuminate\Validation\ValidationException $e) {
// do whatever else you need todo for your use case
return ['error' => 'We caught the exception'];
}
});
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through