mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-17 10:13:37 +00:00
Can now leave out actions and triggers.
If added though, all data must be present. #2477
This commit is contained in:
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Api\V1\Requests;
|
||||
|
||||
use FireflyIII\Rules\IsBoolean;
|
||||
use Illuminate\Validation\Validator;
|
||||
use function is_array;
|
||||
|
||||
|
||||
@@ -116,6 +117,57 @@ class RuleUpdateRequest extends Request
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the validator instance.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function withValidator(Validator $validator): void
|
||||
{
|
||||
$validator->after(
|
||||
function (Validator $validator) {
|
||||
$this->atLeastOneTrigger($validator);
|
||||
$this->atLeastOneAction($validator);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an error to the validator when there are no repetitions in the array of data.
|
||||
*
|
||||
* @param Validator $validator
|
||||
*/
|
||||
protected function atLeastOneAction(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$actions = $data['actions'] ?? null;
|
||||
if (is_array($actions)) {
|
||||
// need at least one action
|
||||
if (0 === count($actions)) {
|
||||
$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 atLeastOneTrigger(Validator $validator): void
|
||||
{
|
||||
$data = $validator->getData();
|
||||
$triggers = $data['triggers'] ?? null;
|
||||
if (is_array($triggers)) {
|
||||
// need at least one trigger
|
||||
if (0 === count($triggers)) {
|
||||
$validator->errors()->add('title', (string)trans('validation.at_least_one_trigger'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
|
@@ -387,25 +387,29 @@ class RuleRepository implements RuleRepositoryInterface
|
||||
public function update(Rule $rule, array $data): Rule
|
||||
{
|
||||
// update rule:
|
||||
$rule->rule_group_id = $data['rule_group_id'];
|
||||
$rule->active = $data['active'];
|
||||
$rule->stop_processing = $data['stop_processing'];
|
||||
$rule->title = $data['title'];
|
||||
$rule->strict = $data['strict'] ?? false;
|
||||
$rule->description = $data['description'];
|
||||
|
||||
$rule->rule_group_id = $data['rule_group_id'] ?? $rule->rule_group_id;
|
||||
$rule->active = $data['active'] ?? $rule->active;
|
||||
$rule->stop_processing = $data['stop_processing'] ?? $rule->stop_processing;
|
||||
$rule->title = $data['title'] ?? $rule->title;
|
||||
$rule->strict = $data['strict'] ?? $rule->strict;
|
||||
$rule->description = $data['description'] ?? $rule->description;
|
||||
$rule->save();
|
||||
|
||||
if (null !== $data['triggers']) {
|
||||
// delete triggers:
|
||||
$rule->ruleTriggers()->delete();
|
||||
|
||||
// recreate triggers:
|
||||
$this->storeTriggers($rule, $data);
|
||||
}
|
||||
if (null !== $data['actions']) {
|
||||
// delete actions:
|
||||
$rule->ruleActions()->delete();
|
||||
|
||||
// recreate triggers:
|
||||
$this->storeTriggers($rule, $data);
|
||||
|
||||
// recreate actions:
|
||||
$this->storeActions($rule, $data);
|
||||
}
|
||||
|
||||
return $rule;
|
||||
}
|
||||
|
Reference in New Issue
Block a user