Skip to content

Commit

Permalink
Buat validasi file valid (#651)
Browse files Browse the repository at this point in the history
* Buat validasi file valid

* Fix styling

* ubah pesan invalid

* Update catatan_rilis.md

---------

Co-authored-by: yogiperdana <[email protected]>
Co-authored-by: Afila <[email protected]>
  • Loading branch information
3 people authored Mar 6, 2023
1 parent 83de156 commit 5ba51ac
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Http/Requests/ArtikelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function rules()
'judul' => 'required|string|max:191',
'isi' => 'required',
'status' => 'required',
'gambar' => 'nullable|image|mimes:jpg,jpeg,png|max:1024',
'gambar' => 'nullable|image|mimes:jpg,jpeg,png|max:1024|valid_file',
];
}
}
13 changes: 13 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
Expand All @@ -55,6 +56,7 @@ public function boot()
$this->penduduk();
$this->config();
$this->blade();
$this->file();
}

protected function penduduk()
Expand Down Expand Up @@ -174,6 +176,17 @@ protected function blade()
});
}

protected function file()
{
Validator::extend('valid_file', function ($attributes, $value, $parameters) {
$contains = preg_match('/<\?php|<script|function|__halt_compiler|<html/i', File::get($value));
if ($contains) {
return false;
}
return true;
});
}

/**
* Register any application services.
*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
'unique' => 'The :attribute has already been taken.',
'uploaded' => 'The :attribute failed to upload.',
'url' => 'The :attribute format is invalid.',
'valid_file' => 'The :attribute format is invalid',

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/id/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
'timezone' => 'Isian :attribute harus berupa zona waktu yang valid.',
'unique' => 'Isian :attribute sudah ada sebelumnya.',
'url' => 'Format isian :attribute tidak valid.',

'valid_file' => 'Format :Jenis berkas yang anda unggah berbahaya.',
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
Expand Down

0 comments on commit 5ba51ac

Please sign in to comment.