diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 05cbead4dd..24e450fbb1 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -25,6 +25,7 @@ namespace FireflyIII\Http\Requests; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionType; use Illuminate\Validation\Validator; +use Log; /** * Class JournalFormRequest. @@ -237,12 +238,19 @@ class JournalFormRequest extends Request { $data = $validator->getData(); $type = $data['what'] ?? 'invalid'; + Log::debug(sprintf('Type is %s', $type)); if ($type === 'withdrawal') { + $selectedCurrency = (int)($data['amount_currency_id_amount'] ?? 0); $accountCurrency = (int)($data['source_account_currency'] ?? 0); - $nativeAmount = (string)$data['native_amount']; - if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount) { - $validator->errors()->add('native_amount', trans('validation.numeric', ['attribute' => 'native_amount'])); + Log::debug(sprintf('Selected currency is %d, account currency is %d', $selectedCurrency, $accountCurrency)); + $nativeAmount = (string)($data['native_amount'] ?? ''); + if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount + && $selectedCurrency !== 0 + && $accountCurrency !== 0 + ) { + Log::debug('ADD validation error on native_amount'); + $validator->errors()->add('native_amount', trans('validation.numeric_native')); return; } @@ -252,9 +260,12 @@ class JournalFormRequest extends Request if ($type === 'deposit') { $selectedCurrency = (int)($data['amount_currency_id_amount'] ?? 0); $accountCurrency = (int)($data['destination_account_currency'] ?? 0); - $nativeAmount = (string)$data['native_amount']; - if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount) { - $validator->errors()->add('native_amount', trans('validation.numeric', ['attribute' => 'native_amount'])); + $nativeAmount = (string)($data['native_amount'] ?? ''); + if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount + && $selectedCurrency !== 0 + && $accountCurrency !== 0 + ) { + $validator->errors()->add('native_amount', trans('validation.numeric_native')); return; } @@ -262,17 +273,29 @@ class JournalFormRequest extends Request // and for transfers if ($type === 'transfer') { + $sourceCurrency = (int)($data['source_account_currency'] ?? 0); $destinationCurrency = (int)($data['destination_account_currency'] ?? 0); - $sourceAmount = (string)$data['source_amount']; - $destinationAmount = (string)$data['destination_amount']; - if ($sourceCurrency !== $destinationCurrency && '' === $sourceAmount) { - $validator->errors()->add('source_amount', trans('validation.numeric', ['attribute' => 'source_amount'])); + $sourceAmount = (string)($data['source_amount'] ?? ''); + $destinationAmount = (string)($data['destination_amount'] ?? ''); + + Log::debug(sprintf('Source currency is %d, destination currency is %d', $sourceCurrency, $destinationCurrency)); + + if ($sourceCurrency !== $destinationCurrency && '' === $sourceAmount + && $sourceCurrency !== 0 + && $destinationCurrency !== 0 + ) { + $validator->errors()->add('source_amount', trans('validation.numeric_source')); } - if ($sourceCurrency !== $destinationCurrency && '' === $destinationAmount) { + if ($sourceCurrency !== $destinationCurrency && '' === $destinationAmount + && $sourceCurrency !== 0 + && $destinationCurrency !== 0 + ) { + $validator->errors()->add('destination_amount', trans('validation.numeric_destination')); $validator->errors()->add('destination_amount', trans('validation.numeric', ['attribute' => 'destination_amount'])); } + return; } } diff --git a/resources/lang/en_US/validation.php b/resources/lang/en_US/validation.php index f9e017f755..6040f20399 100644 --- a/resources/lang/en_US/validation.php +++ b/resources/lang/en_US/validation.php @@ -91,6 +91,9 @@ return [ 'min.array' => 'The :attribute must have at least :min items.', 'not_in' => 'The selected :attribute is invalid.', 'numeric' => 'The :attribute must be a number.', + 'numeric_native' => 'The native amount must be a number.', + 'numeric_destination' => 'The destination amount must be a number.', + 'numeric_source' => 'The source amount must be a number.', 'regex' => 'The :attribute format is invalid.', 'required' => 'The :attribute field is required.', 'required_if' => 'The :attribute field is required when :other is :value.', diff --git a/tests/Feature/Controllers/Transaction/SingleControllerTest.php b/tests/Feature/Controllers/Transaction/SingleControllerTest.php index 563e7044ff..486c4e0492 100644 --- a/tests/Feature/Controllers/Transaction/SingleControllerTest.php +++ b/tests/Feature/Controllers/Transaction/SingleControllerTest.php @@ -810,6 +810,8 @@ class SingleControllerTest extends TestCase $data = [ 'what' => 'transfer', 'amount' => '10', + 'source_amount' => '10', + 'destination_amount' => '10', 'amount_currency_id_amount' => 1, 'source_account_currency' => 1, 'destination_account_currency' => 2,