Fix bad array validation.

This commit is contained in:
James Cole 2020-10-24 16:49:05 +02:00
parent c910cbe5da
commit 08f347cd57
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
2 changed files with 21 additions and 2 deletions

View File

@ -275,6 +275,10 @@ class TransactionStoreRequest extends FormRequest
{ {
$validator->after( $validator->after(
function (Validator $validator) { function (Validator $validator) {
// must be valid array.
$this->validateTransactionArray($validator);
// must submit at least one transaction. // must submit at least one transaction.
$this->validateOneTransaction($validator); $this->validateOneTransaction($validator);

View File

@ -53,6 +53,9 @@ trait TransactionValidation
* @var array $transaction * @var array $transaction
*/ */
foreach ($transactions as $index => $transaction) { foreach ($transactions as $index => $transaction) {
if(!is_int($index)) {
continue;
}
$this->validateSingleAccount($validator, $index, $transactionType, $transaction); $this->validateSingleAccount($validator, $index, $transactionType, $transaction);
} }
} }
@ -178,6 +181,20 @@ trait TransactionValidation
} }
} }
/**
* @param Validator $validator
*/
public function validateTransactionArray(Validator $validator): void {
$transactions = $this->getTransactionsArray($validator);
foreach($transactions as $key => $value) {
if(!is_int($key)) {
$validator->errors()->add('transactions.0.description', (string) trans('validation.at_least_one_transaction'));
Log::debug('Added error: at_least_one_transaction.');
return;
}
}
}
/** /**
* Adds an error to the validator when there are no transactions in the array of data. * Adds an error to the validator when there are no transactions in the array of data.
* *
@ -235,8 +252,6 @@ trait TransactionValidation
foreach ($transactions as $transaction) { foreach ($transactions as $transaction) {
$originalType = $this->getOriginalType((int) ($transaction['transaction_journal_id'] ?? 0)); $originalType = $this->getOriginalType((int) ($transaction['transaction_journal_id'] ?? 0));
// if type is not set, fall back to the type of the journal, if one is given. // if type is not set, fall back to the type of the journal, if one is given.
$types[] = $transaction['type'] ?? $originalType; $types[] = $transaction['type'] ?? $originalType;
} }
$unique = array_unique($types); $unique = array_unique($types);