2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
2015-03-12 10:44:39 +10:00
|
|
|
|
2017-01-30 21:40:43 +02:00
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
|
|
use App\Libraries\Utils;
|
2019-09-12 19:30:10 -04:00
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2015-03-12 10:44:39 +10:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2019-09-12 19:30:10 -04:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2016-12-13 17:52:09 +11:00
|
|
|
use Response;
|
2015-03-12 10:44:39 +10:00
|
|
|
|
2016-05-31 23:15:37 +03:00
|
|
|
// https://laracasts.com/discuss/channels/general-discussion/laravel-5-modify-input-before-validation/replies/34366
|
2017-01-30 18:05:31 +02:00
|
|
|
abstract class Request extends FormRequest
|
|
|
|
|
{
|
2016-06-01 12:39:42 +03:00
|
|
|
// populate in subclass to auto load record
|
|
|
|
|
protected $autoload = [];
|
|
|
|
|
|
2016-05-31 23:15:37 +03:00
|
|
|
/**
|
|
|
|
|
* Validate the input.
|
|
|
|
|
*
|
2017-01-30 21:40:43 +02:00
|
|
|
* @param \Illuminate\Validation\Factory $factory
|
|
|
|
|
*
|
2016-05-31 23:15:37 +03:00
|
|
|
* @return \Illuminate\Validation\Validator
|
|
|
|
|
*/
|
|
|
|
|
public function validator($factory)
|
|
|
|
|
{
|
|
|
|
|
return $factory->make(
|
|
|
|
|
$this->sanitizeInput(), $this->container->call([$this, 'rules']), $this->messages()
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-03-12 10:44:39 +10:00
|
|
|
|
2016-05-31 23:15:37 +03:00
|
|
|
/**
|
|
|
|
|
* Sanitize the input.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function sanitizeInput()
|
|
|
|
|
{
|
2016-06-01 12:39:42 +03:00
|
|
|
if (method_exists($this, 'sanitize')) {
|
|
|
|
|
$input = $this->container->call([$this, 'sanitize']);
|
|
|
|
|
} else {
|
|
|
|
|
$input = $this->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// autoload referenced entities
|
|
|
|
|
foreach ($this->autoload as $entityType) {
|
|
|
|
|
if ($id = $this->input("{$entityType}_public_id") ?: $this->input("{$entityType}_id")) {
|
2016-07-03 18:11:58 +02:00
|
|
|
$class = 'App\\Models\\' . ucwords($entityType);
|
2016-06-01 12:39:42 +03:00
|
|
|
$entity = $class::scope($id)->firstOrFail();
|
|
|
|
|
$input[$entityType] = $entity;
|
|
|
|
|
$input[$entityType . '_id'] = $entity->id;
|
|
|
|
|
}
|
2016-05-31 23:15:37 +03:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 12:39:42 +03:00
|
|
|
$this->replace($input);
|
|
|
|
|
|
2016-05-31 23:15:37 +03:00
|
|
|
return $this->all();
|
|
|
|
|
}
|
2016-12-13 17:52:09 +11:00
|
|
|
|
2019-09-12 19:30:10 -04:00
|
|
|
protected function failedValidation(Validator $validator)
|
2016-12-13 17:52:09 +11:00
|
|
|
{
|
|
|
|
|
/* If the user is not validating from a mobile app - pass through parent::response */
|
2019-09-12 19:30:10 -04:00
|
|
|
if ( ! request()->api_secret) {
|
|
|
|
|
parent::failedValidation($validator);
|
2016-12-22 19:12:12 +02:00
|
|
|
}
|
2016-12-13 17:52:09 +11:00
|
|
|
|
|
|
|
|
/* If the user is validating from a mobile app - pass through first error string and return error */
|
2019-09-12 19:30:10 -04:00
|
|
|
if ($value = $validator->getMessageBag()->first()) {
|
|
|
|
|
$message['error'] = ['message' => $value];
|
|
|
|
|
$message = json_encode($message, JSON_PRETTY_PRINT);
|
|
|
|
|
$headers = Utils::getApiHeaders();
|
2016-12-13 17:52:09 +11:00
|
|
|
|
2019-09-12 19:30:10 -04:00
|
|
|
throw new ValidationException($validator, Response::make($message, 400, $headers));
|
2016-12-13 17:52:09 +11:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-12 10:44:39 +10:00
|
|
|
}
|