简体   繁体   English

如何在ZF2表单元素之后添加“换行”?

[英]How to add a “new line” after a ZF2 form element?

Using ZF2 I am trying to place <br> or a similar element between my form elements. 我正在使用ZF2尝试在表单元素之间放置<br>或类似元素。 ZF1 had something like "Decorators" which are no longer in ZF2 to my knowledge. ZF1具有类似“装饰器”之类的东西,据我所知,它们已不再位于ZF2中。 However, ZF2 does not have something $form->addBr() element, and that is what I need. 但是,ZF2没有$form->addBr()元素,这就是我需要的。

Here is how I render a form in my View: 这是我在视图中渲染表单的方式:

<?php echo $this->form($this->form);?>

Here is how I prepare my $form in my controller 这是我在控制器中准备$form的方法

    // Set up checkbox
    $checkbox = new Element\Checkbox('checkbox');
    $checkbox->setChecked(true);

    //Set up text
    $text = new Element\Text('text');
    $text->setLabel("Hi");
    $text->setValue(333);

    // Assemble Fielset
    $fieldset = new Fieldset("FS");
    $fieldset->setLabel("Label");

    $fieldset->add($checkbox);

    //NOTE:  I need a "NEW LINE" Here
    $fieldset->ADD_NEW_LINE();// no such method 


    $fieldset->add($text);

    // Assemble Form
    $form = new Form();
    $form->add($fieldset);

Current Issue: 目前的问题:

Form elements render out on a single line when I want them to be on a new line each. 当我希望每个表单元素都换行时,表单元素会在一行上呈现。

Question

When I want to ZF2 just render the entire form in one go, like I try to do here (preferably without having code in view that renders out the form line by line), how can I make it so that I can place new form elements on new lines? 当我想ZF2一次性渲染整个表单时,就像我在这里尝试做的那样(最好是没有使代码逐行呈现的代码),如何制作它以便放置新的表单元素在新的线?

I am open to any solutions -- whether it be programmatic ZF2 solutions or CSS solutions (if possible) or other solutions I can't think of yet. 我愿意接受任何解决方案-无论是编程ZF2解决方案还是CSS解决方案(如果可能),还是我想不到的其他解决方案。 I just want the form to render out with elements being shown on new lines instead of showing up on a single line. 我只希望表单呈现出来,而元素显示在新行上,而不是显示在一行上。

ZF2 Renders HTML like so: ZF2渲染HTML如下:

 <fieldset> <legend>Legend</legend> <label><span>Check</span> <input name="name[checkbox]" value="0" type="hidden"> <input name="name[checkbox]" value="1" checked="checked" type="checkbox"> </label> <label><span>Value</span> <input name="name[text]" value="123" type="text"> </label> </fieldset> 

You can either do this with CSS or override the formRow() helper (which the form() helper uses) to output the markup you want. 您可以使用CSS进行此操作,也可以覆盖formRow()帮助器( form()帮助器使用)以输出所需的标记。

I created a simple module that overrides the form row helper to output divs (with appropriate classes for styling): https://packagist.org/packages/tfountain/tf-form - feel free to either use this or copy the approach and customise to suit your needs. 我创建了一个简单的模块,该模块将覆盖表单行帮助程序以输出div(具有用于样式设置的适当类): https ://packagist.org/packages/tfountain/tf-form-随意使用此方法或复制该方法并进行自定义满足您的需求。 Mine will give you markup like this: 我的会给你这样的标记:

<div id="some_element" class="form-row form-row-text">
    <label><span>Value</span>
        <input name="name[text]" value="123" type="text">
    </label>
</div>

If you want to roll your own similar solution, this is the helper code: https://github.com/tfountain/tf-form/blob/master/src/TfForm/Form/View/Helper/FormRow.php 如果您想推出自己的类似解决方案,请使用帮助程序代码: https : //github.com/tfountain/tf-form/blob/master/src/TfForm/Form/View/Helper/FormRow.php

I was able to make this happen -- essentially copied ZF2's own mechanisms and got some help from this answer: https://stackoverflow.com/a/15827116/2883328 我能够做到这一点-本质上是复制了ZF2自己的机制,并从以下答案中获得了一些帮助: https : //stackoverflow.com/a/15827116/2883328

I removed FieldSet that was only messing me up, and then used a loop to cycle through the Form elements, amending <br/> where I wanted it - after each element. 我删除了FieldSet使我FieldSet ,然后使用循环遍历Form元素,并在每个需要的地方修改了<br/>我想要的地方。 So much for that. 这么多。

<?php
/**
 * inside view template
 *
 * @var $this \Zend\View\Renderer\PhpRenderer
 * @var $form \Zend\Form\Form
 */
$form = $this->form;
?>
<fieldset>
    <legend>Legend</legend>
    <?php
    echo $this->form()->openTag($form);
    foreach ($form as $element)
        $formContent .= $this->formrow($element) . "<br/>"; //note the "BR"

    echo $formContent;
    echo $this->form()->closeTag();

    ?>
</fieldset>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM