Skip to content

Commit 37358ff

Browse files
committed
DefaultFormRenderer: supports option 'nextTo' & style 'control .multi'
1 parent 53b85ac commit 37358ff

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

src/Forms/Rendering/DefaultFormRenderer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DefaultFormRenderer implements Nette\Forms\IFormRenderer
4646
* .... label.requiredsuffix
4747
* \---
4848
*
49-
* /--- control.container [.odd]
49+
* /--- control.container [.odd .multi]
5050
* .... CONTROL [.required .error .text .password .file .submit .button]
5151
* .... control.requiredsuffix
5252
* .... control.description
@@ -88,6 +88,7 @@ class DefaultFormRenderer implements Nette\Forms\IFormRenderer
8888
'control' => [
8989
'container' => 'td',
9090
'.odd' => null,
91+
'.multi' => null,
9192

9293
'description' => 'small',
9394
'requiredsuffix' => '',
@@ -217,6 +218,12 @@ public function renderErrors(Nette\Forms\IControl $control = null, bool $own = t
217218
$errors = $control
218219
? $control->getErrors()
219220
: ($own ? $this->form->getOwnErrors() : $this->form->getErrors());
221+
return $this->doRenderErrors($errors, (bool) $control);
222+
}
223+
224+
225+
private function doRenderErrors(array $errors, bool $control): string
226+
{
220227
if (!$errors) {
221228
return '';
222229
}
@@ -450,6 +457,8 @@ public function renderControl(Nette\Forms\IControl $control): Html
450457
$description = $this->getValue('control requiredsuffix') . $description;
451458
}
452459

460+
$els = $errors = [];
461+
renderControl:
453462
$control->setOption('rendered', true);
454463
$el = $control->getControl();
455464
if ($el instanceof Html) {
@@ -458,7 +467,16 @@ public function renderControl(Nette\Forms\IControl $control): Html
458467
}
459468
$el->class($this->getValue('control .error'), $control->hasErrors());
460469
}
461-
return $body->setHtml($el . $description . $this->renderErrors($control));
470+
$els[] = $el;
471+
$errors = array_merge($errors, $control->getErrors());
472+
473+
if ($nextTo = $control->getOption('nextTo')) {
474+
$control = $control->getForm()->getComponent($nextTo);
475+
$body->class($this->getValue('control .multi'), true);
476+
goto renderControl;
477+
}
478+
479+
return $body->setHtml(implode('', $els) . $description . $this->doRenderErrors($errors, true));
462480
}
463481

464482

tests/Forms/Forms.renderer.1.expect

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,14 @@
9393
<tr class="required">
9494
<th><label for="frm-password" class="required">Choose password:</label></th>
9595

96-
<td><input type="password" name="password" id="frm-password" required data-nette-rules='[{"op":":filled","msg":"Choose your password"},{"op":":minLength","msg":"The password is too short: it must be at least 3 characters","arg":3}]' class="text"></td>
97-
</tr>
98-
99-
<tr>
100-
<th><label for="frm-password2">Reenter password:</label></th>
101-
102-
<td><input type="password" name="password2" id="frm-password2" data-nette-rules='[{"op":":valid","rules":[{"op":":filled","msg":"Reenter your password"},{"op":":equal","msg":"Passwords do not match","arg":{"control":"password"}}],"control":"password"}]' class="text">
96+
<td><input type="password" name="password" id="frm-password" required data-nette-rules='[{"op":":filled","msg":"Choose your password"},{"op":":minLength","msg":"The password is too short: it must be at least 3 characters","arg":3}]' class="text"><input type="password" name="password2" id="frm-password2" data-nette-rules='[{"op":":valid","rules":[{"op":":filled","msg":"Reenter your password"},{"op":":equal","msg":"Passwords do not match","arg":{"control":"password"}}],"control":"password"}]' class="text"><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%},{"op":":filled","rules":[{"op":":image","msg":"Uploaded file is not image"}],"control":"avatar"}]' class="text">
10397

10498
<span class="error">
10599
Reenter your password
106100
</span>
107101
</td>
108102
</tr>
109103

110-
<tr>
111-
<th><label for="frm-avatar">Picture:</label></th>
112-
113-
<td><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%},{"op":":filled","rules":[{"op":":image","msg":"Uploaded file is not image"}],"control":"avatar"}]' class="text"></td>
114-
</tr>
115-
116104
<tr>
117105
<th><label for="frm-note">Comment:</label></th>
118106

tests/Forms/Forms.renderer.1.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ $form->addSelect('countrySetItems', 'Country:')
8686
$form->addGroup('Your account');
8787

8888
$form->addPassword('password', 'Choose password:')
89+
->setOption('nextTo', 'password2')
8990
->addRule(Form::FILLED, 'Choose your password')
9091
->addRule(Form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3);
9192

9293
$form->addPassword('password2', 'Reenter password:')
94+
->setOption('nextTo', 'avatar')
9395
->addConditionOn($form['password'], Form::VALID)
9496
->addRule(Form::FILLED, 'Reenter your password')
9597
->addRule(Form::EQUAL, 'Passwords do not match', $form['password']);

tests/Forms/Forms.renderer.2.expect

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282
<dt><label for="frm-password" class="required">Choose password:</label></dt>
8383

84-
<dd><input type="password" name="password" id="frm-password" required data-nette-rules='[{"op":":filled","msg":"Choose your password"},{"op":":minLength","msg":"The password is too short: it must be at least 3 characters","arg":3}]' class="text is-invalid"> • <small>(at least 3 characters)</small>
84+
<dd class="multi"><input type="password" name="password" id="frm-password" required data-nette-rules='[{"op":":filled","msg":"Choose your password"},{"op":":minLength","msg":"The password is too short: it must be at least 3 characters","arg":3}]' class="text is-invalid"><input type="password" name="password2" id="frm-password2" data-nette-rules='[{"op":":valid","rules":[{"op":":filled","msg":"Reenter your password"},{"op":":equal","msg":"Passwords do not match","arg":{"control":"password"}}],"control":"password"}]' class="text"> • <small>(at least 3 characters)</small>
8585

8686
<span class="error">
8787
The password is too short: it must be at least 3 characters
@@ -90,21 +90,15 @@
9090

9191

9292

93-
<dt><label for="frm-password2">Reenter password:</label></dt>
94-
95-
<dd class="odd"><input type="password" name="password2" id="frm-password2" data-nette-rules='[{"op":":valid","rules":[{"op":":filled","msg":"Reenter your password"},{"op":":equal","msg":"Passwords do not match","arg":{"control":"password"}}],"control":"password"}]' class="text"></dd>
96-
97-
98-
9993
<dt><label for="frm-avatar">Picture:</label></dt>
10094

101-
<dd><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%}]' class="text"></dd>
95+
<dd class="odd"><input type="file" name="avatar" id="frm-avatar" data-nette-rules='[{"op":":fileSize","msg":"The size of the uploaded file can be up to %d% bytes.","arg":%d%}]' class="text"></dd>
10296

10397

10498

10599
<dt><label for="frm-note">Comment:</label></dt>
106100

107-
<dd class="odd"><textarea name="note" id="frm-note"></textarea></dd>
101+
<dd><textarea name="note" id="frm-note"></textarea></dd>
108102

109103
</dl>
110104

tests/Forms/Forms.renderer.2.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ $renderer->wrappers['pair']['container'] = null;
4646
$renderer->wrappers['controls']['container'] = 'dl';
4747
$renderer->wrappers['control']['container'] = 'dd';
4848
$renderer->wrappers['control']['.odd'] = 'odd';
49+
$renderer->wrappers['control']['.multi'] = 'multi';
4950
$renderer->wrappers['control']['.error'] = 'is-invalid';
5051
$renderer->wrappers['control']['errors'] = true;
5152
$renderer->wrappers['label']['container'] = 'dt';
@@ -96,6 +97,7 @@ $form->addSelect('country', 'Country', $countries)
9697
$form->addGroup('Your account');
9798

9899
$form->addPassword('password', 'Choose password')
100+
->setOption('nextTo', 'password2')
99101
->addRule(Form::FILLED, 'Choose your password')
100102
->addRule(Form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3)
101103
->setOption('description', '(at least 3 characters)');

0 commit comments

Comments
 (0)