validates percentage on store and new depreciations

This commit is contained in:
Godfrey M 2024-07-16 13:07:12 -07:00
parent 48821f8391
commit 5bb47e290f

View file

@ -62,8 +62,21 @@ class DepreciationsController extends Controller
$depreciation->name = $request->input('name'); $depreciation->name = $request->input('name');
$depreciation->months = $request->input('months'); $depreciation->months = $request->input('months');
$depreciation->user_id = Auth::id(); $depreciation->user_id = Auth::id();
$depreciation->depreciation_min = $request->input('depreciation_min');
$request->validate([
'depreciation_min' => [
'required',
'numeric',
function ($attribute, $value, $fail) use ($request) {
if ($request->input('depreciation_type') == 'percent' && ($value < 0 || $value > 100)) {
$fail('The depreciation minimum must be between 0 and 100 when depreciation type is percentage.');
}
},
],
'depreciation_type' => 'required|in:amount,percent',
]);
$depreciation->depreciation_type = $request->input('depreciation_type'); $depreciation->depreciation_type = $request->input('depreciation_type');
$depreciation->depreciation_min = $request->input('depreciation_min');
// Was the asset created? // Was the asset created?
if ($depreciation->save()) { if ($depreciation->save()) {