Skip to content

Firefly III: a personal finances manager

Update request must also have valid triggers and actions.

James Cole7fc94415fbd6
diff --git a/app/Api/V1/Requests/Models/Rule/UpdateRequest.php b/app/Api/V1/Requests/Models/Rule/UpdateRequest.php
index 3e476d3e41..e9a6764985 100644
--- a/app/Api/V1/Requests/Models/Rule/UpdateRequest.php
+++ b/app/Api/V1/Requests/Models/Rule/UpdateRequest.php
@@ -167,7 +167,9 @@ class UpdateRequest extends FormRequest
$validator->after(
function (Validator $validator) {
$this->atLeastOneTrigger($validator);
+ $this->atLeastOneValidTrigger($validator);
$this->atLeastOneAction($validator);
+ $this->atLeastOneValidAction($validator);
}
);
}
@@ -187,6 +189,35 @@ class UpdateRequest extends FormRequest
}
}
+ /**
+ * Adds an error to the validator when there are no repetitions in the array of data.
+ *
+ * @param Validator $validator
+ */
+ protected function atLeastOneValidTrigger(Validator $validator): void
+ {
+ $data = $validator->getData();
+ $triggers = $data['triggers'] ?? [];
+ $allInactive = true;
+ $inactiveIndex = 0;
+ // need at least one trigger
+ if (is_array($triggers) && empty($triggers)) {
+ return;
+ }
+ foreach ($triggers as $index => $trigger) {
+ $active = array_key_exists('active', $trigger) ? $trigger['active'] : true; // assume true
+ if (true === $active) {
+ $allInactive = false;
+ }
+ if (false === $active) {
+ $inactiveIndex = $index;
+ }
+ }
+ if (true === $allInactive) {
+ $validator->errors()->add(sprintf('triggers.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_trigger'));
+ }
+ }
+
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*
@@ -201,4 +232,34 @@ class UpdateRequest extends FormRequest
$validator->errors()->add('title', (string)trans('validation.at_least_one_action'));
}
}
+
+ /**
+ * Adds an error to the validator when there are no repetitions in the array of data.
+ *
+ * @param Validator $validator
+ */
+ protected function atLeastOneValidAction(Validator $validator): void
+ {
+ $data = $validator->getData();
+ $actions = $data['actions'] ?? [];
+ $allInactive = true;
+ $inactiveIndex = 0;
+ // need at least one action
+ if (is_array($actions) && empty($actions)) {
+ return;
+ }
+
+ foreach ($actions as $index => $action) {
+ $active = array_key_exists('active', $action) ? $action['active'] : true; // assume true
+ if (true === $active) {
+ $allInactive = false;
+ }
+ if (false === $active) {
+ $inactiveIndex = $index;
+ }
+ }
+ if (true === $allInactive) {
+ $validator->errors()->add(sprintf('actions.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_action'));
+ }
+ }
}