我对在zend中设计表单感到困惑。我知道我的表单中有字段,外观应该在视图中完成。
在索引视图中几乎是普通的html我没有问题,但在添加和编辑视图中显示我的表单我有问题改变外观。
我有一个如下的观点:
<?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();
当然,它显示了另一个下方的一个区域。如何连续显示两个字段(带标签)?我真的很感激一个好的教程的示例或提示,它展示了如何使用这个zend3概念正确地完成它。
它甚至是在视图中执行它的正确位置还是我需要一个新的layout.phtml这种情况?
要分别打印元素的各个部分,ZF中预定义了几个功能。你可以在\Zend\Form\ConfigProvider->getViewHelperConfig()
找到所有这些,参见here on Github。
在你的情况下,你已经在使用qazxsw poi,qazxsw poi和qazxsw poi。
如果你有像货币这样的东西,你可以方便单独使用,你希望用户同时填写金额并选择货币但只使用一个标签,例如:
formLabel
整个“表格行”由以下部分组成:
因此,在此示例中,您需要整个“金额”位,您可以将上述内容缩短为:
formElement
如果你仔细观察'zendframework / zend-form'的链接formElementErrors
,你可能会注意到还有一个$this->formLabel($form->get('amount'));
$this->formElement($form->get('amount'));
$this->formElementErrors($form->get('amount'));
$this->formElement($form->get('currency'));
$this->formElementErrors($form->get('currency'));
ViewHelper。这可以用于一次打印整个表单,如下所示:
file:add-foo.phtml
$this->formRow($form->get('amount')); // prints all elements for the row
$this->formElement($form->get('currency'));
$this->formElementErrors($form->get('currency'));
就是这样。它打印整个表单。当然它使用ZF定义的ViewHelpers,因此也应用了布局和类。
如果您愿意,可以使用该配置并在您自己的项目中覆盖它。
例如,您的问题代码显示您在每行周围添加ConfigProvider
。大概是为Bootstrap 4.要神奇地做到这一点,所以你不需要这样做:
form
我们可以调整 <?= $this->form($form) ?>
ViewHelper。只需按以下步骤操作:
<div class="form-group"></div>
,例如 <div class="form-group">
<?= $this->formRow($form->get('foo')) ?>
</div>
formRow
函数,如下所示:FormRow.php
module/Foo/src/View/Helper/FormRow.phtml
类div),所以在类中定义它,如下所示:render
use Zend\Form\View\Helper\FormRow as ZendFormRow;
class FormRow extends ZendFormRow
{
public function render(ElementInterface $element, $labelPosition = null)
{
// its content
}
}
函数的底部,您将找到以下代码(在form-group
之前): class FormRow extends ZendFormRow
{
protected $inputRow = '<div class="form-group">%s</div>';
// the other stuff
}
放在上面之后:
render
else
完成。
现在当你做 if ($this->renderErrors) {
$markup .= $elementErrors;
}
时,来自ZendFramework的 $markup = sprintf(
$this->inputRow,
$markup,
);
ViewHelper会在它的工厂执行 'view_helpers' => [
'aliases' => [
'formrow' => FormRow::class,
'form_row' => FormRow::class,
'formRow' => FormRow::class,
'FormRow' => FormRow::class,
],
'factories' => [
FormRow::class => InvokableFactory::class,
],
],
时接收你的自定义$this->form($form)
ViewHelper,因为配置会被你自己覆盖。因此,所有行现在将自动拥有周围的div。
比你要求的更多,但玩得开心;)我现在要停止避免工作O :)