Writeoff/Http/Requests/Debitur/UpdateDebiturRequest.php

76 lines
2.5 KiB
PHP
Raw Normal View History

2023-11-13 12:13:33 +00:00
<?php
namespace Modules\Writeoff\Http\Requests\Debitur;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
class UpdateDebiturRequest extends FormRequest
{
public $_id;
/**
* 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
{
$this->_id = json_decode(json_decode(file_get_contents('php://input'))->components[0]->snapshot)->data->id;
return [
2023-11-15 08:04:25 +00:00
'kode' => 'required|string|max:6|min:6|unique:debitur,kode,' . $this->_id,
2023-11-13 12:13:33 +00:00
'name' => 'required|string|max:100',
'branch_id' => 'required|integer|exists:branches,id',
2023-11-15 08:04:25 +00:00
'status' => 'required|boolean',
2023-11-13 12:13:33 +00:00
'address' => 'nullable|string|max:255',
'npwp' => 'nullable|string|max:16|min:16',
'registered_at' => 'nullable|date_format:Y-m-d'
];
}
/**
* 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.debitur.index')->with('error', 'Debitur 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' => 'Debitur updated failed.'
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
}