67 lines
2.1 KiB
PHP
67 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Master\Http\Requests\Country;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Illuminate\Validation\Validator;
|
|
use Modules\Master\Http\Requests\MasterRequest;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
class UpdateCountryRequest extends MasterRequest
|
|
{
|
|
/**
|
|
* 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
|
|
{
|
|
return [
|
|
'name' => 'required|string|max:50|unique:countries,name,' . $this->country->id,
|
|
'status' => 'nullable|integer|max:1',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Configure the validator instance.
|
|
*/
|
|
public function withValidator(Validator $validator)
|
|
: void
|
|
{
|
|
$validator->after(function (Validator $validator) {
|
|
if ($validator->errors()->any()) {
|
|
$errors = json_decode($validator->errors()->toJson(), true);
|
|
|
|
foreach ($errors as $key => $value) {
|
|
flash($value[0]);
|
|
}
|
|
return redirect()->route('master.country.index')->with('error', 'Country 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' => 'Country updated failed.'
|
|
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
|
|
}
|
|
}
|