Copy method for validation.

This commit is contained in:
James Cole
2019-08-27 05:57:09 +02:00
parent a67bbaa1eb
commit 96caf3491e
2 changed files with 56 additions and 5 deletions

View File

@@ -69,6 +69,7 @@ class RecurrenceUpdateRequest extends Request
'title' => $this->nullableString('title'),
'description' => $this->nullableString('description'),
'first_date' => $this->date('first_date'),
'notes' => $this->nullableString('notes'),
'repeat_until' => $this->date('repeat_until'),
'nr_of_repetitions' => $this->nullableInteger('nr_of_repetitions'),
'apply_rules' => $applyRules,
@@ -90,22 +91,21 @@ class RecurrenceUpdateRequest extends Request
{
/** @var Recurrence $recurrence */
$recurrence = $this->route()->parameter('recurrence');
$first = clone $recurrence->first_date;
$first->subDay();
return [
'type' => 'in:withdrawal,transfer,deposit',
'title' => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id),
'description' => 'between:1,65000',
'first_date' => sprintf('date|after:%s', $first->format('Y-m-d')),
'first_date' => 'date',
'apply_rules' => [new IsBoolean],
'active' => [new IsBoolean],
'repeat_until' => sprintf('date|after:%s', $first->format('Y-m-d')),
'repeat_until' => 'date',
'nr_of_repetitions' => 'numeric|between:1,31',
'repetitions.*.type' => 'in:daily,weekly,ndom,monthly,yearly',
'repetitions.*.moment' => 'between:0,10',
'repetitions.*.skip' => 'required|numeric|between:0,31',
'repetitions.*.weekend' => 'required|numeric|min:1|max:4',
'transactions.*.description' => 'required|between:1,255',
'transactions.*.amount' => 'required|numeric|more:0',
'transactions.*.foreign_amount' => 'numeric|more:0',
@@ -146,7 +146,7 @@ class RecurrenceUpdateRequest extends Request
$this->validateRecurrenceRepetition($validator);
$this->validateRepetitionMoment($validator);
$this->validateForeignCurrencyInformation($validator);
$this->validateAccountInformation($validator);
$this->valUpdateAccountInfo($validator);
}
);
}

View File

@@ -37,6 +37,57 @@ use Log;
*/
trait RecurrenceValidation
{
/**
* Validate account information input for recurrences which are being updated.
*
* TODO must always trigger when the type of the recurrence changes.
*
* @param Validator $validator
*/
public function valUpdateAccountInfo(Validator $validator): void
{
//Log::debug('Now in validateAccountInformation()');
$data = $validator->getData();
$transactionType = $data['type'] ?? 'invalid';
$transactions = $data['transactions'] ?? [];
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
foreach ($transactions as $index => $transaction) {
$transactionType = $transaction['type'] ?? $transactionType;
$accountValidator->setTransactionType($transactionType);
// validate source account.
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : null;
$sourceName = $transaction['source_name'] ?? null;
$validSource = $accountValidator->validateSource($sourceId, $sourceName);
// do something with result:
if (false === $validSource) {
$validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError);
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError);
return;
}
// validate destination account
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null;
$destinationName = $transaction['destination_name'] ?? null;
$validDestination = $accountValidator->validateDestination($destinationId, $destinationName);
// do something with result:
if (false === $validDestination) {
$validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError);
$validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError);
return;
}
}
}
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*