Can now leave out actions and triggers.

If added though, all data must be present. #2477
This commit is contained in:
James Cole
2019-08-25 07:55:21 +02:00
parent c0909aebba
commit 8680254503
2 changed files with 71 additions and 15 deletions

View File

@@ -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
*/

View File

@@ -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;
}