74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Modules\Buyer\Http\Requests;
|
||
|
|
||
|
use Illuminate\Foundation\Http\FormRequest;
|
||
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
||
|
use Illuminate\Validation\ValidationException;
|
||
|
use Illuminate\Validation\Validator;
|
||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
|
||
|
class StoreBuyerRequest extends FormRequest
|
||
|
{
|
||
|
/**
|
||
|
* 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:buyers,name',
|
||
|
'address' => 'required|string|max:255',
|
||
|
'phone' => 'required|string|max:20',
|
||
|
'website' => 'required|string|max:50',
|
||
|
'email' => 'required|string|max:50',
|
||
|
'buyer_type' => 'required|string|max:10',
|
||
|
'npwp' => 'required|string|max:50',
|
||
|
'status' => 'nullable|string|max:1',
|
||
|
'address_outlet' => 'nullable|string|max:255'
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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('buyer.index')->with('error', 'Buyer created 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' => 'Buyer created failed.'
|
||
|
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
|
||
|
}
|
||
|
}
|