I'm a bit confused with designing forms in zend. I understood that I have the fields in my form class and the look should be done in the views.
In the index view which is nearly plain html I don't have problems, but in the add and edit views which show my form I have problems to change the look.
I have a viewscript like follows:
<?php
$title = 'AVB ändern';
$this->headTitle($title);
?>
<h1><?= $this->escapeHtml($title) ?></h1>
<?php
$id= $form->get('id');
$id->setAttribute('class', 'form-control');
$id->setAttribute('placeholder', 'id');
$avbname= $form->get('avbname');
$avbname->setAttribute('class', 'form-control');
$avbname->setAttribute('placeholder', 'avbname');
$vbedingungen= $form->get('vbedingungen');
$vbedingungen->setAttribute('class', 'form-control');
$vbedingungen->setAttribute('placeholder', 'vbedingungen');
$versichererid= $form->get('versichererid');
$versichererid->setAttribute('class', 'form-control');
$versichererid->setAttribute('placeholder', 'versichererid');
$aktiv= $form->get('aktiv');
$aktiv->setAttribute('class', 'form-control');
$aktiv->setAttribute('placeholder', 'aktiv');
$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');
$form->prepare();
echo $this->form()->openTag($form);
?>
<div class="form-group">
<?= $this->formElement($id) ?>
<?= $this->formElementErrors()->render($id, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($avbname) ?>
<?= $this->formElement($avbname) ?>
<?= $this->formElementErrors()->render($avbname, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($vbedingungen) ?>
<?= $this->formElement($vbedingungen) ?>
<?= $this->formElementErrors()->render($vbedingungen, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($versichererid) ?>
<?= $this->formElement($versichererid) ?>
<?= $this->formElementErrors()->render($versichererid, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($aktiv) ?>
<?= $this->formElement($aktiv) ?>
<?= $this->formElementErrors()->render($aktiv, s['class' => 'help-block']) ?>
</div>
<?php
echo $this->formSubmit($submit);
echo $this->formHidden($form->get('id'));
$form->setAttribute('action', $this->url('typavb', ['action' => 'edit']));
echo $this->form()->closeTag();
Of course it shows one field beneath the other. How can I show two fields in a row (with the labels) ? I really would appreciate an example or a tip to a good tutorial, which shows how to do it properly with this zend3 concept.
Is it even the right place to do it in the view or do I need a new layout.phtml for this case?
To print parts of Elements separately, there's several functions pre-defined in ZF. You can find all of them in
\Zend\Form\ConfigProvider->getViewHelperConfig(), see here on Github.In your case, your already using
formLabel,formElementandformElementErrors.These are handy for separte use if you have something like Currency, where you'd like a user to both fill in an amount and choose a currency but only use a single label, e.g.:
An entire "form row" is made up out of:
So, as in this example you need the entire "amount" bit, you could shorten the above to:
If you look closely through the linked
ConfigProviderof 'zendframework/zend-form', you might've noticed there's also aformViewHelper. This can be used to print an entire form in a single go, like so:file: add-foo.phtml
And that's it. It prints the whole form. Of course it uses the ZF defined ViewHelpers, as such also with that layout and classes applied.
If you wish, can take that config and override it in your own projects.
For example, your question code shows you add
<div class="form-group"></div>around each row. Presumably for Bootstrap 4. To do this magically so you need not do:We can adjust the
formRowViewHelper. Simply follow these steps:FormRow.phpin your own project, e.g.module/Foo/src/View/Helper/FormRow.phtmlrenderfunction, like so:form-groupclass div), so define it in the class, like so:renderfunction, you'll find the following code (before theelse):Place after the above:
Done.
Now when you do
$this->form($form)theFormElementViewHelper from ZendFramework will receive your customformRowViewHelper when it its Factory does->get('formRow'), as the config is overwritten to your own. As such, all rows will now automagically have the surrounding div.Bit more than you asked for, but have fun ;) I'm gonna stop avoiding work now O:)