2023-10-02 11:27:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Writeoff\Http\Requests\Branch;
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Illuminate\Validation\Validator;
|
2023-10-10 04:18:12 +00:00
|
|
|
use Modules\Writeoff\Entities\Branch;
|
|
|
|
use function Deployer\input;
|
2023-10-02 11:27:22 +00:00
|
|
|
|
|
|
|
class UpdateBranchRequest extends FormRequest
|
|
|
|
{
|
2023-10-10 04:18:12 +00:00
|
|
|
|
|
|
|
public $_id;
|
2023-10-02 11:27:22 +00:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
: bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
: array
|
|
|
|
{
|
2023-10-10 04:18:12 +00:00
|
|
|
$this->_id = json_decode(json_decode(file_get_contents('php://input'))->components[0]->snapshot)->data->id;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'kode' => 'required|string|max:9|min:9|unique:branches,kode,' . $this->_id,
|
2023-10-02 11:27:22 +00:00
|
|
|
'name' => 'required|string|max:100'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the validator instance.
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator)
|
|
|
|
: void
|
|
|
|
{
|
|
|
|
$validator->after(function (Validator $validator) {
|
|
|
|
if ($validator->errors()->any()) {
|
|
|
|
$error = json_decode($validator->errors()->toJson(), true);
|
|
|
|
foreach ($error as $key => $value) {
|
|
|
|
flash($value[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('parameter.branches.index')->with('error', 'Branch updated failed.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function failedValidation(Validator|\Illuminate\Contracts\Validation\Validator $validator)
|
|
|
|
: JsonResponse
|
|
|
|
{
|
|
|
|
$errors = (new ValidationException($validator))->errors();
|
|
|
|
|
|
|
|
throw new HttpResponseException(response()->json([
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $errors,
|
|
|
|
'messages' => 'Branch updated failed.'
|
|
|
|
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
|
|
|
|
}
|
2023-10-09 11:12:51 +00:00
|
|
|
|
2023-10-02 11:27:22 +00:00
|
|
|
}
|