I'm getting the following error:
the given data failed to pass validation. /home/vagrant/Code/laravel/ptm/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php#89
When I look at the $data
, it's filled with:
array:1 [
"{"_token":"Z5fv3XpoNwoMdJPRP16I09bZeX7Pb6raH30K8n3b","name":"test","id":"","email":"test@example_com","password":"testing"}"
]
Here's the code:
public function create(Request $request)
{
$data = $request->all();
$this->validate($request, ['name' => 'required',
'email' => 'required',
'password' => 'required'
]);
try
{
$this->user->create($request);
}
catch (Exception $e)
{
return json_encode(array('success' => false, 'message' => 'Something went wrong, please try again later.'));
}
return json_encode(array('success' => true, 'message' => 'User successfully saved!'));
}
It's clearly saying that the validate()
method is throwing an exception. If you want it to not to throw an exception, update your App\Exceptions\Handler
class and add ValidationException
to the $dontReport
array:
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class, // <= Here
];
Read more:
Documentation how to migrate from Laravel 5.1 to 5.2
Or you can leave it as it is, and handle the exception in a new catch
block:
public function create(Request $request)
{
try {
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required'
]);
$this->user->create($request);
} catch (ValidationException $e) {
return response()->json([
'success' => false,
'message' => 'There were validation errors.',
], 400);
} catch (Exception $e) {
return response()->json([
'success' => false,
'message' => 'Something went wrong, please try again later.'
], 400);
}
return response()->json([
'success' => true,
'message' => 'User successfully saved!'
], 201);
}
You might check the blade template for the correct case on the variables. e.g. 'name', 'email' and 'password' are case sensitive and the blade template case of the variables must match the controller's case for the variables.