58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Karbohidrat;
|
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RequestUpdateKarbohidrat extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'nama_karbohidrat' => 'required|string'
|
|
];
|
|
}
|
|
|
|
public function messages(){
|
|
return [
|
|
'nama_karbohidrat.required' => 'Nama Karbohidrat wajib diisi'
|
|
];
|
|
}
|
|
|
|
protected function failedValidation(Validator $validator){
|
|
throw new HttpResponseException(
|
|
response()->json([
|
|
'status' => 'VALIDATION_FAILED',
|
|
'message' => 'Validasi Gagal',
|
|
'errors' => $validator->errors()->messages(),
|
|
], 422)
|
|
);
|
|
}
|
|
|
|
public function withValidator($validator){
|
|
$validator->after(function($validator){
|
|
$id = $this->route('karbohidrat') ?? $this->karbohidrat_id ?? null;
|
|
$exists = DB::connection('dbOrderGizi')
|
|
->table('public.karbohidrat')
|
|
->where('statusenabled', true)
|
|
->where('nama_karbohidrat', 'ILIKE', $this->input('nama_karbohidrat'));
|
|
|
|
if($id) $exists->where('karbohidrat_id', '!=', $id);
|
|
|
|
if($exists->exists()){
|
|
$validator->errors()->add(
|
|
'nama_karbohidrat',
|
|
'Nama Karbohidrat sudah digunakan'
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|