Skip to content

Commit 0737b1c

Browse files
committed
DefaultFormRenderer: supports option 'nextTo'
1 parent 63594dd commit 0737b1c

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/Forms/Rendering/DefaultFormRenderer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ public function renderErrors(Nette\Forms\IControl $control = null, bool $own = t
217217
$errors = $control
218218
? $control->getErrors()
219219
: ($own ? $this->form->getOwnErrors() : $this->form->getErrors());
220+
return $this->doRenderErrors($errors, (bool) $control);
221+
}
222+
223+
224+
private function doRenderErrors(array $errors, bool $control): string
225+
{
220226
if (!$errors) {
221227
return '';
222228
}
@@ -450,6 +456,8 @@ public function renderControl(Nette\Forms\IControl $control): Html
450456
$description = $this->getValue('control requiredsuffix') . $description;
451457
}
452458

459+
$els = $errors = [];
460+
renderControl:
453461
$control->setOption('rendered', true);
454462
$el = $control->getControl();
455463
if ($el instanceof Html) {
@@ -458,7 +466,15 @@ public function renderControl(Nette\Forms\IControl $control): Html
458466
}
459467
$el->class($this->getValue('control .error'), $control->hasErrors());
460468
}
461-
return $body->setHtml($el . $description . $this->renderErrors($control));
469+
$els[] = $el;
470+
$errors = array_merge($errors, $control->getErrors());
471+
472+
if ($nextTo = $control->getOption('nextTo')) {
473+
$control = $control->getForm()->getComponent($nextTo);
474+
goto renderControl;
475+
}
476+
477+
return $body->setHtml(implode('', $els) . $description . $this->doRenderErrors($errors, true));
462478
}
463479

464480

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']);

0 commit comments

Comments
 (0)