mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Copy method for validation.
This commit is contained in:
@@ -69,6 +69,7 @@ class RecurrenceUpdateRequest extends Request
|
|||||||
'title' => $this->nullableString('title'),
|
'title' => $this->nullableString('title'),
|
||||||
'description' => $this->nullableString('description'),
|
'description' => $this->nullableString('description'),
|
||||||
'first_date' => $this->date('first_date'),
|
'first_date' => $this->date('first_date'),
|
||||||
|
'notes' => $this->nullableString('notes'),
|
||||||
'repeat_until' => $this->date('repeat_until'),
|
'repeat_until' => $this->date('repeat_until'),
|
||||||
'nr_of_repetitions' => $this->nullableInteger('nr_of_repetitions'),
|
'nr_of_repetitions' => $this->nullableInteger('nr_of_repetitions'),
|
||||||
'apply_rules' => $applyRules,
|
'apply_rules' => $applyRules,
|
||||||
@@ -90,22 +91,21 @@ class RecurrenceUpdateRequest extends Request
|
|||||||
{
|
{
|
||||||
/** @var Recurrence $recurrence */
|
/** @var Recurrence $recurrence */
|
||||||
$recurrence = $this->route()->parameter('recurrence');
|
$recurrence = $this->route()->parameter('recurrence');
|
||||||
$first = clone $recurrence->first_date;
|
|
||||||
$first->subDay();
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'type' => 'in:withdrawal,transfer,deposit',
|
'type' => 'in:withdrawal,transfer,deposit',
|
||||||
'title' => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id),
|
'title' => sprintf('between:1,255|uniqueObjectForUser:recurrences,title,%d', $recurrence->id),
|
||||||
'description' => 'between:1,65000',
|
'description' => 'between:1,65000',
|
||||||
'first_date' => sprintf('date|after:%s', $first->format('Y-m-d')),
|
'first_date' => 'date',
|
||||||
'apply_rules' => [new IsBoolean],
|
'apply_rules' => [new IsBoolean],
|
||||||
'active' => [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',
|
'nr_of_repetitions' => 'numeric|between:1,31',
|
||||||
'repetitions.*.type' => 'in:daily,weekly,ndom,monthly,yearly',
|
'repetitions.*.type' => 'in:daily,weekly,ndom,monthly,yearly',
|
||||||
'repetitions.*.moment' => 'between:0,10',
|
'repetitions.*.moment' => 'between:0,10',
|
||||||
'repetitions.*.skip' => 'required|numeric|between:0,31',
|
'repetitions.*.skip' => 'required|numeric|between:0,31',
|
||||||
'repetitions.*.weekend' => 'required|numeric|min:1|max:4',
|
'repetitions.*.weekend' => 'required|numeric|min:1|max:4',
|
||||||
|
|
||||||
'transactions.*.description' => 'required|between:1,255',
|
'transactions.*.description' => 'required|between:1,255',
|
||||||
'transactions.*.amount' => 'required|numeric|more:0',
|
'transactions.*.amount' => 'required|numeric|more:0',
|
||||||
'transactions.*.foreign_amount' => 'numeric|more:0',
|
'transactions.*.foreign_amount' => 'numeric|more:0',
|
||||||
@@ -146,7 +146,7 @@ class RecurrenceUpdateRequest extends Request
|
|||||||
$this->validateRecurrenceRepetition($validator);
|
$this->validateRecurrenceRepetition($validator);
|
||||||
$this->validateRepetitionMoment($validator);
|
$this->validateRepetitionMoment($validator);
|
||||||
$this->validateForeignCurrencyInformation($validator);
|
$this->validateForeignCurrencyInformation($validator);
|
||||||
$this->validateAccountInformation($validator);
|
$this->valUpdateAccountInfo($validator);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,57 @@ use Log;
|
|||||||
*/
|
*/
|
||||||
trait RecurrenceValidation
|
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.
|
* Adds an error to the validator when there are no repetitions in the array of data.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user