Skip to content

Commit a7e779c

Browse files
committed
[FIX] UI: align Container\Form context renderers.
1 parent ecaa346 commit a7e779c

File tree

7 files changed

+41
-115
lines changed

7 files changed

+41
-115
lines changed

components/ILIAS/UI/src/Implementation/Component/Input/Container/Form/Form.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,4 @@ protected function extractRequestData(ServerRequestInterface $request): InputDat
6161
{
6262
return new PostDataFromServerRequest($request);
6363
}
64-
65-
/**
66-
* @inheritdoc
67-
*/
68-
public function getPromptButtons(): array
69-
{
70-
return [];
71-
}
72-
73-
/**
74-
* @inheritdoc
75-
*/
76-
public function getPromptTitle(): string
77-
{
78-
return '';
79-
}
8064
}

components/ILIAS/UI/src/Implementation/Component/Input/Container/Form/FormRendererFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525

2626
class FormRendererFactory extends Render\DefaultRendererFactory
2727
{
28-
public const NO_BUTTONS_FORM = [
28+
public const FORM_CONTEXTS_WITHOUT_BUTTONS = [
2929
'StateStatePrompt',
3030
'RoundTripModal',
3131
];
3232

3333
public function getRendererInContext(Component\Component $component, array $contexts): Render\AbstractComponentRenderer
3434
{
35-
$has_context_without_buttons = array_intersect(self::NO_BUTTONS_FORM, $contexts);
35+
$has_context_without_buttons = array_intersect(self::FORM_CONTEXTS_WITHOUT_BUTTONS, $contexts);
3636

3737
if (! empty($has_context_without_buttons)) {
38-
return new NoButtonsContextRenderer(
38+
return new FormWithoutSubmitButtonsContextRenderer(
3939
$this->ui_factory,
4040
$this->tpl_factory,
4141
$this->lng,

components/ILIAS/UI/src/Implementation/Component/Input/Container/Form/NoButtonsContextRenderer.php renamed to components/ILIAS/UI/src/Implementation/Component/Input/Container/Form/FormWithoutSubmitButtonsContextRenderer.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,27 @@
2727
use ILIAS\UI\Component;
2828
use LogicException;
2929

30-
class NoButtonsContextRenderer extends AbstractComponentRenderer
30+
class FormWithoutSubmitButtonsContextRenderer extends Renderer
3131
{
3232
/**
3333
* @inheritdoc
3434
*/
3535
public function render(Component\Component $component, RendererInterface $default_renderer): string
3636
{
37-
if (!$component instanceof Form\Form) {
38-
$this->cannotHandleComponent($component);
37+
if ($component instanceof Form\Standard) {
38+
return $this->renderFormWithoutSubmitButtons($component, $default_renderer);
3939
}
40-
return $this->renderNoSubmit($component, $default_renderer);
40+
41+
$this->cannotHandleComponent($component);
4142
}
4243

43-
protected function renderNoSubmit(
44-
Form\Form $component,
44+
protected function renderFormWithoutSubmitButtons(
45+
Form\Standard $component,
4546
RendererInterface $default_renderer
4647
): string {
47-
$tpl = $this->getTemplate("tpl.no_submit.html", true, true);
48+
$tpl = $this->getTemplate("tpl.without_submit_buttons.html", true, true);
4849

50+
$this->maybeAddDedicatedName($component, $tpl);
4951
$this->maybeAddRequired($component, $tpl);
5052
$this->addPostURL($component, $tpl);
5153
$this->maybeAddError($component, $tpl);
@@ -75,41 +77,4 @@ static function (string $id) use ($component): string {
7577

7678
return $tpl->get();
7779
}
78-
79-
protected function addPostURL(Component\Input\Container\Form\FormWithPostURL $component, Template $tpl): void
80-
{
81-
if ('' !== ($url = $component->getPostURL())) {
82-
$tpl->setCurrentBlock("action");
83-
$tpl->setVariable("URL", $url);
84-
$tpl->parseCurrentBlock();
85-
}
86-
}
87-
88-
protected function maybeAddError(Form\Form $component, Template $tpl): void
89-
{
90-
if (null !== ($error = $component->getError())) {
91-
$tpl->setCurrentBlock("error");
92-
$tpl->setVariable("ERROR", $error);
93-
$tpl->parseCurrentBlock();
94-
}
95-
}
96-
97-
protected function maybeAddRequired(Form\Form $component, Template $tpl): void
98-
{
99-
if ($component->hasRequiredInputs()) {
100-
$tpl->setVariable("TXT_REQUIRED_TOP", $this->txt("required_field"));
101-
$tpl->setVariable("TXT_REQUIRED", $this->txt("required_field"));
102-
}
103-
}
104-
105-
/**
106-
* @inheritdoc
107-
*/
108-
protected function getComponentInterfaceName(): array
109-
{
110-
return [
111-
Component\Input\Container\Form\Standard::class,
112-
FormWithoutSubmitButton::class,
113-
];
114-
}
11580
}

components/ILIAS/UI/src/Implementation/Component/Input/Container/Form/Renderer.php

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,14 @@ public function render(Component\Component $component, RendererInterface $defaul
3838
return $this->renderStandard($component, $default_renderer);
3939
}
4040

41-
if ($component instanceof Form\FormWithoutSubmitButton) {
42-
return $this->renderNoSubmit($component, $default_renderer);
43-
}
44-
4541
$this->cannotHandleComponent($component);
4642
}
4743

4844
protected function renderStandard(Form\Standard $component, RendererInterface $default_renderer): string
4945
{
5046
$tpl = $this->getTemplate("tpl.standard.html", true, true);
51-
if ($component->getDedicatedName() !== null) {
52-
$tpl->setVariable("NAME", 'name="' . $component->getDedicatedName() . '"');
53-
}
5447

48+
$this->maybeAddDedicatedName($component, $tpl);
5549
$this->maybeAddRequired($component, $tpl);
5650
$this->addPostURL($component, $tpl);
5751
$this->maybeAddError($component, $tpl);
@@ -68,39 +62,11 @@ protected function renderStandard(Form\Standard $component, RendererInterface $d
6862
return $tpl->get();
6963
}
7064

71-
protected function renderNoSubmit(Form\FormWithoutSubmitButton $component, RendererInterface $default_renderer): string
65+
protected function maybeAddDedicatedName(Form\Form $component, Template $tpl): void
7266
{
73-
$tpl = $this->getTemplate("tpl.no_submit.html", true, true);
74-
75-
$this->maybeAddRequired($component, $tpl);
76-
$this->addPostURL($component, $tpl);
77-
$this->maybeAddError($component, $tpl);
78-
79-
$tpl->setVariable("INPUTS", $default_renderer->render($component->getInputGroup()));
80-
81-
/** @var $component Form\FormWithoutSubmitButton */
82-
$enriched_component = $component->withAdditionalOnLoadCode(
83-
static function (string $id) use ($component): string {
84-
return "
85-
// @TODO: we need to refactor the signal-management to prevent using jQuery here.
86-
$(document).on('{$component->getSubmitSignal()}', function () {
87-
let form = document.getElementById('$id');
88-
if (!form instanceof HTMLFormElement) {
89-
throw new Error(`Element '$id' is not an instance of HTMLFormElement.`);
90-
}
91-
92-
// @TODO: we should use the triggering button as an emitter here. When doing
93-
// so, please also change file.js processFormSubmissionHook().
94-
form.requestSubmit();
95-
});
96-
";
97-
}
98-
);
99-
100-
$id = $this->bindJavaScript($enriched_component) ?? $this->createId();
101-
$tpl->setVariable("ID", $id);
102-
103-
return $tpl->get();
67+
if ($component->getDedicatedName() !== null) {
68+
$tpl->setVariable("NAME", 'name="' . $component->getDedicatedName() . '"');
69+
}
10470
}
10571

10672
protected function addPostURL(Component\Input\Container\Form\FormWithPostURL $component, Template $tpl): void

components/ILIAS/UI/src/Implementation/Component/Input/Container/Form/Standard.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
use ILIAS\UI\Implementation\Component\Input;
2626
use ILIAS\UI\Implementation\Component\Input\NameSource;
2727
use ILIAS\UI\Implementation\Component\SignalGeneratorInterface;
28+
use ILIAS\UI\Implementation\Component\Prompt\IsPromptContentInternal;
2829
use ILIAS\UI\Component\Signal;
2930
use ILIAS\UI\Implementation\Component\JavaScriptBindable as JavaScriptBindableTrait;
3031
use ILIAS\UI\Component\JavaScriptBindable;
3132

3233
/**
3334
* This implements a standard form.
3435
*/
35-
class Standard extends Form implements C\Input\Container\Form\Standard, JavaScriptBindable
36+
class Standard extends Form implements C\Input\Container\Form\Standard, IsPromptContentInternal, JavaScriptBindable
3637
{
3738
use HasPostURL;
3839
use JavaScriptBindableTrait;
@@ -55,10 +56,10 @@ public function __construct(
5556
/**
5657
* @inheritDoc
5758
*/
58-
public function withSubmitLabel(string $caption): C\Input\Container\Form\Standard
59+
public function withSubmitLabel(string $label): C\Input\Container\Form\Standard
5960
{
6061
$clone = clone $this;
61-
$clone->submit_caption = $caption;
62+
$clone->submit_caption = $label;
6263
return $clone;
6364
}
6465

@@ -70,6 +71,16 @@ public function getSubmitLabel(): ?string
7071
return $this->submit_caption;
7172
}
7273

74+
public function getPromptButtons(): array
75+
{
76+
return [];
77+
}
78+
79+
public function getPromptTitle(): string
80+
{
81+
return '';
82+
}
83+
7384
public function getSubmitSignal(): Signal
7485
{
7586
return $this->submit_signal;

components/ILIAS/UI/src/templates/default/Input/tpl.no_submit.html renamed to components/ILIAS/UI/src/templates/default/Input/tpl.without_submit_buttons.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<form id="{ID}" role="form" class="c-form c-form--horizontal" enctype="multipart/form-data"<!-- BEGIN reference_error --> describedby="{ERROR_ID}"<!-- END reference_error --> {NAME}<!-- BEGIN action --> action="{URL}"<!-- END action --> method="post" novalidate="novalidate">
22
<!-- BEGIN error -->
3-
<div id="{ERROR_ID}" class="c-form__error-msg alert alert-danger">
4-
{ERROR}
5-
</div>
3+
<div class="c-form__error-msg alert alert-danger" id="{ERROR_ID}"><span class="sr-only">{ERROR_LABEL}: </span>{ERROR}</div>
64
<!-- END error -->
75

86
{INPUTS}

components/ILIAS/UI/tests/Component/Input/Container/Form/NoSubmitFormTest.php renamed to components/ILIAS/UI/tests/Component/Input/Container/Form/FormWithoutSubmitButtonsTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function getNewDedicatedName(string $dedicated_name): string
5656
/**
5757
* @author Thibeau Fuhrer <[email protected]>
5858
*/
59-
class NoSubmitFormTest extends \ILIAS_UI_TestBase
59+
class FormWithoutSubmitButtonsTest extends \ILIAS_UI_TestBase
6060
{
6161
protected SignalGenerator $signal_generator;
6262
protected NameSource $namesource;
@@ -144,7 +144,8 @@ public function testRenderWithRequiredInputs(): void
144144
public function testRenderWithError(): void
145145
{
146146
$post_url = 'http://ilias.localhost/some_url?param1=foo&param2=bar';
147-
$error_lang_var = 'ui_error_in_group';
147+
$error_lang_var = 'ui_error';
148+
$error_lang_var_in_group = 'ui_error_in_group';
148149

149150
$dummy_input = $this->buildInputFactory()->text('test_label')->withAdditionalTransformation(
150151
$this->refinery->custom()->constraint(
@@ -171,11 +172,12 @@ static function ($value): bool {
171172
$form = $form->withRequest($request);
172173
$data = $form->getData();
173174

174-
$expected_html =
175-
"<form id=\"id_1\" role=\"form\" class=\"c-form c-form--horizontal\" enctype=\"multipart/form-data\" action=\"$post_url\" method=\"post\" novalidate=\"novalidate\">" .
176-
"<div id=\"\" class=\"c-form__error-msg alert alert-danger\">$error_lang_var</div>" .
177-
$dummy_input->getCanonicalName() .
178-
"</form>";
175+
$expected_html = <<<EOT
176+
<form id="id_2" role="form" class="c-form c-form--horizontal" enctype="multipart/form-data" describedby="id_1" action="$post_url" method="post" novalidate="novalidate">
177+
<div class="c-form__error-msg alert alert-danger" id="id_1"><span class="sr-only">$error_lang_var:</span>$error_lang_var_in_group
178+
</div>{$dummy_input->getCanonicalName()}
179+
</form>
180+
EOT;
179181

180182
$context = $this->createMock(\ILIAS\UI\Component\Modal\RoundTrip::class);
181183
$context->method('getCanonicalName')->willReturn('RoundTripModal');

0 commit comments

Comments
 (0)