Quick thought! Could you use json_decode and run that through a validator?
Jun 25, 2015
14
Level 10
How to Validate JSON Input using Requests?
I'm creating an controller that should receive an JSON Input, and I wan't to validate it contents using Validator Rules and Requests...
It's possible out of the box, or I will need to extend the FormRequest class?
Thank's in advance,
Level 10
Just to help who may need this:
To accept an Json as input for an Request, you can replace FormRequest by this JsonRequest class that I made:
<?php namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class JsonRequest extends FormRequest {
/**
* Get the validator instance for the request.
*
* @return \Illuminate\Validation\Validator
*/
protected function getValidatorInstance()
{
$factory = $this->container->make('Illuminate\Validation\Factory');
if (method_exists($this, 'validator'))
{
return $this->container->call([$this, 'validator'], compact('factory'));
}
return $factory->make(
$this->json()->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes()
);
}
}
8 likes
Please or to participate in this conversation.