Skip to content

Commit a09b73a

Browse files
committed
Rules::getToggleStates() synced with netteForms.js toggleControl
1 parent bfaaeea commit a09b73a

File tree

2 files changed

+141
-3
lines changed

2 files changed

+141
-3
lines changed

src/Forms/Rules.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,22 @@ public function getToggles(bool $actual = false): array
215215

216216

217217
/** @internal */
218-
public function getToggleStates(array $toggles = [], bool $success = true): array
218+
public function getToggleStates(array $toggles = [], bool $success = true, bool $emptyOptional = null): array
219219
{
220220
foreach ($this->toggles as $id => $hide) {
221221
$toggles[$id] = ($success xor !$hide) || !empty($toggles[$id]);
222222
}
223223

224-
foreach ($this->rules as $rule) {
224+
$emptyOptional = $emptyOptional ?? (!$this->isRequired() && !$this->control->isFilled());
225+
foreach ($this as $rule) {
225226
if ($rule->branch) {
226-
$toggles = $rule->branch->getToggleStates($toggles, $success && static::validateRule($rule));
227+
$toggles = $rule->branch->getToggleStates(
228+
$toggles,
229+
$success && static::validateRule($rule),
230+
$rule->validator === Form::BLANK ? false : $emptyOptional
231+
);
232+
} elseif (!$emptyOptional || $rule->validator === Form::FILLED) {
233+
$success = $success && static::validateRule($rule);
227234
}
228235
}
229236
return $toggles;

tests/Forms/Forms.toggle.phpt

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,134 @@ test(function () {
548548
'a' => true,
549549
], $form->getToggles());
550550
});
551+
552+
553+
test(function () { // combined with rules
554+
$form = new Form;
555+
$form->addText('1');
556+
$form->addText('2');
557+
$form->addText('3')
558+
->setRequired()
559+
->addConditionOn($form['1'], Form::NOT_EQUAL, 'x')
560+
->toggle('a')
561+
->addConditionOn($form['2'], Form::NOT_EQUAL, 'x')
562+
->toggle('b')
563+
->endCondition()
564+
->endCondition();
565+
566+
Assert::same([
567+
'a' => false,
568+
'b' => false,
569+
], $form->getToggles());
570+
});
571+
572+
573+
test(function () {
574+
$form = new Form;
575+
$form->addText('1');
576+
$form->addText('2');
577+
$form->addText('3')
578+
->setRequired()
579+
->addRule(Form::NOT_EQUAL, null, 'x')
580+
->addConditionOn($form['1'], Form::EQUAL, 'x')
581+
->toggle('a')
582+
->addConditionOn($form['2'], Form::EQUAL, 'x')
583+
->toggle('b');
584+
585+
Assert::same([
586+
'a' => false,
587+
'b' => false,
588+
], $form->getToggles());
589+
});
590+
591+
592+
test(function () {
593+
$form = new Form;
594+
$form->addText('1');
595+
$form->addText('2');
596+
$form->addText('3')
597+
->setRequired()
598+
->addRule(Form::EQUAL, null, 'x')
599+
->addConditionOn($form['1'], Form::NOT_EQUAL, 'x')
600+
->toggle('a')
601+
->addConditionOn($form['2'], Form::NOT_EQUAL, 'x')
602+
->toggle('b');
603+
604+
Assert::same([
605+
'a' => false,
606+
'b' => false,
607+
], $form->getToggles());
608+
});
609+
610+
611+
test(function () {
612+
$form = new Form;
613+
$form->addText('1');
614+
$form->addText('2');
615+
$form->addText('3')
616+
->addConditionOn($form['1'], Form::NOT_EQUAL, 'x')
617+
->setRequired()
618+
->addRule(Form::EQUAL, null, 'x')
619+
->toggle('a')
620+
->addConditionOn($form['2'], Form::NOT_EQUAL, 'x')
621+
->toggle('b');
622+
623+
Assert::same([
624+
'a' => true,
625+
'b' => false,
626+
], $form->getToggles());
627+
});
628+
629+
630+
test(function () {
631+
$form = new Form;
632+
$form->addText('1');
633+
$form->addText('2');
634+
$form->addText('3')
635+
->addRule(Form::EQUAL, null, 'x')
636+
->addConditionOn($form['1'], Form::NOT_EQUAL, 'x')
637+
->toggle('a')
638+
->addConditionOn($form['2'], Form::NOT_EQUAL, 'x')
639+
->toggle('b');
640+
641+
Assert::same([
642+
'a' => true,
643+
'b' => true,
644+
], $form->getToggles());
645+
});
646+
647+
648+
test(function () {
649+
$form = new Form;
650+
$form->addText('1');
651+
$form->addText('2');
652+
$form->addText('3')
653+
->addConditionOn($form['1'], Form::NOT_EQUAL, 'x')
654+
->addRule(Form::EQUAL, null, 'x')
655+
->toggle('a')
656+
->addConditionOn($form['2'], Form::NOT_EQUAL, 'x')
657+
->toggle('b');
658+
659+
Assert::same([
660+
'a' => true,
661+
'b' => true,
662+
], $form->getToggles());
663+
});
664+
665+
666+
test(function () {
667+
$form = new Form;
668+
$form->addText('1');
669+
$form->addText('2');
670+
$form->addText('3')
671+
->addConditionOn($form['1'], Form::NOT_EQUAL, 'x')
672+
->addRule(Form::EQUAL, null, 'x')
673+
->toggle('a')
674+
->addConditionOn($form['2'], Form::NOT_EQUAL, 'x')
675+
->toggle('b');
676+
677+
Assert::same([
678+
'a' => true,
679+
'b' => true,
680+
], $form->getToggles());
681+
});

0 commit comments

Comments
 (0)