Skip to content

Commit 3638828

Browse files
committed
addSubmit() added $onSubmit parameter
1 parent 8f97f7f commit 3638828

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/Forms/Container.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,19 @@ public function addColor(string $name, string|Stringable|null $label = null): Co
561561

562562
/**
563563
* Adds button used to submit form.
564+
* @param (\Closure(Controls\SubmitButton, mixed[]|object): void | \Closure(static, mixed[]|object): void | \Closure(mixed[]|object): void) $onSubmit
564565
*/
565-
public function addSubmit(string $name, string|Stringable|null $caption = null): Controls\SubmitButton
566+
public function addSubmit(
567+
string $name,
568+
string|Stringable|null $caption = null,
569+
?\Closure $onSubmit = null,
570+
): Controls\SubmitButton
566571
{
567-
return $this[$name] = new Controls\SubmitButton($caption);
572+
$control = $this[$name] = new Controls\SubmitButton($caption);
573+
if ($onSubmit) {
574+
$control->onClick[] = $onSubmit;
575+
}
576+
return $control;
568577
}
569578

570579

tests/Forms/Forms.onClick.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,23 @@ test('invalid submission due to validation', function () {
104104
$form->fireEvents();
105105
Assert::same(['invalidClick', 'error', 'submit'], $called);
106106
});
107+
108+
109+
test('addSubmit() with $onSubmit parameter', function () {
110+
$_SERVER['REQUEST_METHOD'] = 'POST';
111+
$_POST = ['btn' => ''];
112+
113+
$called = [];
114+
$form = new Form;
115+
$form->allowCrossOrigin();
116+
$form->addText('name');
117+
$form->addSubmit('btn', null, function (SubmitButton $button) use (&$called) {
118+
$called[] = 'click';
119+
});
120+
121+
$form->onSuccess[] = function (Form $form) use (&$called) {
122+
$called[] = 'success';
123+
};
124+
$form->fireEvents();
125+
Assert::same(['click', 'success'], $called);
126+
});

0 commit comments

Comments
 (0)