代码之家  ›  专栏  ›  技术社区  ›  pia-sophie

理解zend中的网格布局

  •  0
  • pia-sophie  · 技术社区  · 7 年前

    我知道我的表单类中有字段,而视图应该在视图中完成。

     <?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概念。

    0 回复  |  直到 7 年前
        1
  •  1
  •   rkeet Aurimas    7 年前

    为了单独打印部分元素,ZF中预先定义了几个函数。你可以在里面找到它们 \Zend\Form\ConfigProvider->getViewHelperConfig() ,参见 here on Github

    在你的情况下,你已经在使用 formLabel , formElement 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'));
    

    整个“表单行”由以下内容组成:

    • 标签(可选)
    • 元素

    因此,在本例中,您需要完整的“金额”位,您可以将上述内容缩短为:

    $this->formRow($form->get('amount'));             // prints all elements for the row
    $this->formElement($form->get('currency'));
    $this->formElementErrors($form->get('currency'));
    

    如果你仔细观察 ConfigProvider form

    文件:添加-食品phtml

        <?= $this->form($form) ?>
    

    就这样。它打印整个表单。当然,它使用ZF定义的ViewHelpers,同样地,它还应用了布局和类。

    如果您愿意,可以接受该配置并在您自己的项目中重写它。

    例如,您的问题代码显示您添加 <div class="form-group"></div>

        <div class="form-group">
            <?= $this->formRow($form->get('foo')) ?>
        </div>
    

    我们可以调整 formRow

    1. FormRow.php 在你自己的项目中,例如。 module/Foo/src/View/Helper/FormRow.phtml
    2. 请确保从ZF的FormRow扩展它并复制到原始的(ZF) render 比如,功能:
        use Zend\Form\View\Helper\FormRow as ZendFormRow;
    
        class FormRow extends ZendFormRow
        {
            public function render(ElementInterface $element, $labelPosition = null)
            {
                // its content
            }
        }
    
    1. 我们要添加一个包装( form-group 类div),所以在类中定义它,如下所示:
        class FormRow extends ZendFormRow
        {
            protected $inputRow = '<div class="form-group">%s</div>';
            // the other stuff
        }
    
    1. 提供 函数,您将找到以下代码(在 else
        if ($this->renderErrors) {
            $markup .= $elementErrors;
        }
    

        $markup = sprintf(
            $this->inputRow,
            $markup,
        );
    
    1. 使用与ZF相同的别名注册新的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) FormElement 来自ZendFramework的ViewHelper将接收您的自定义 formRow公司 ViewHelper的工厂 ->get('formRow') ,因为配置被改写为您自己的配置。因此,所有行现在将自动拥有周围的div。