Abstract class to allow custom validation
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Http\Exception\HttpResponseException;
abstract class CustomRequest extends Request
{
/**
* Custom Validation without validation of Laravel.
*
* @return boolean
*/
abstract public function customValidate();
/**
* Return the errors.
*
* @return array
*/
abstract public function getErrorMessages();
/**
* Validate the class instance.
*
* @return void
*/
public function validate()
{
if (!$this->passesAuthorization()) {
$this->failedAuthorization();
}
elseif (!$this->customValidate()) {
$this->throwExceptionValidation($this->getErrorMessages());
}
}
protected function throwExceptionValidation(array $errors)
{
$response = $this->response($errors);
throw new HttpResponseException($response);
}
}
namespace App\Http\Requests;
class TestRequest extends CustomRequest
{
protected $redirect = '/';
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function customValidate()
{
return false;
}
public function getErrorMessages()
{
return [
'title' => 'Title is required',
];
}
}