48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class StoreSpecialCodeRequest 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 [
|
|
'kode' => 'required|string|max:2|min:2|unique:special_codes,kode',
|
|
'name' => 'required|string|max:50'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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('special-code.index')->with('error', 'Special Code created failed.');
|
|
}
|
|
});
|
|
}
|
|
}
|