This commit is contained in:
James Cole
2021-03-31 19:36:08 +02:00
parent 0756054690
commit cbcf251bb3
5 changed files with 74 additions and 50 deletions

View File

@@ -22,6 +22,7 @@
declare(strict_types=1);
namespace FireflyIII\Validation\AutoBudget;
use Illuminate\Validation\Validator;
/**
@@ -36,27 +37,27 @@ trait ValidatesAutoBudgetRequest
{
$data = $validator->getData();
$type = $data['auto_budget_type'] ?? '';
$amount = $data['auto_budget_amount'] ?? '';
$period = (string)($data['auto_budget_period'] ?? '');
$currencyId = $data['auto_budget_currency_id'] ?? '';
$currencyCode = $data['auto_budget_currency_code'] ?? '';
$amount = array_key_exists('auto_budget_amount', $data) ? $data['auto_budget_amount'] : null;
$period = array_key_exists('auto_budget_period', $data) ? $data['auto_budget_period'] : null;
$currencyId = array_key_exists('auto_budget_currency_id', $data) ? (int)$data['auto_budget_currency_id'] : null;
$currencyCode = array_key_exists('auto_budget_currency_code', $data) ? $data['auto_budget_currency_code'] : null;
if (is_numeric($type)) {
$type = (int)$type;
}
if (0 === $type || 'none' === $type || '' === $type) {
if (0 === $type) {
return;
}
// basic float check:
if ('' === $amount) {
$validator->errors()->add('auto_budget_amount', (string)trans('validation.amount_required_for_auto_budget'));
}
if (1 !== bccomp((string)$amount, '0')) {
if (null !== $amount && 1 !== bccomp((string)$amount, '0')) {
$validator->errors()->add('auto_budget_amount', (string)trans('validation.auto_budget_amount_positive'));
}
if ('' === $period) {
$validator->errors()->add('auto_budget_period', (string)trans('validation.auto_budget_period_mandatory'));
}
if ('' === $currencyCode && '' === $currencyId) {
if (null !== $amount && null !== $currencyId && null !== $currencyCode && '' === $currencyCode && '' === $currencyId) {
$validator->errors()->add('auto_budget_amount', (string)trans('validation.require_currency_info'));
}
}