Display label before input using formRow view helper in Zf2
Asked Answered
B

3

8

In Zend Framework 2.1.4 I am using the standard form view helpers to render out my form elements.

When I try:

<?php echo $this->formRow($form->get('Title'));?>

The label text and input element are placed within the label:

<label>
<span>Title</span><input type="text" name="Title" placeholder="Inserisci titolo"
required="required" value="">
</label>

The same with:

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

However, if I render out the label and input individually:

echo $this->formLabel($form->get('Title'));
echo $this->formInput($form->get('Title'));

It generates the html I want:

<label for="Title">Title</label>
<input type="text" name="Title" placeholder="Insert Title" required="required" value="">

How can I achieve the same with the formRow view helper?

Bronez answered 11/4, 2013 at 10:59 Comment(0)
A
7

If a form element does not have an "id" attribute, the label will wrap the input:

<label>Label<input /></label>

Otherwise:

<label for="test">Label</label><input id="test" />
Armor answered 3/8, 2013 at 13:20 Comment(1)
Bill, thank you. It is difficult to speak in a language that has ever taught.Armor
S
0

Looking at (zf2 version 2.25 dev):

\Zend\Form\View\Helper\FormRow

It appears that if you don't provide an id for your form elements, the default general behaviour is to place the input element inside their corresponding label element.

The second argument for the formRow view helper, will place the label text before (prepend) or after (append) the input element in the document flow. (The default is to place it before.)

Check the render method for more details.

Salmonoid answered 26/10, 2013 at 0:10 Comment(0)
C
-2

In first you must look source code to understand how formRow works : https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormRow.php

After you'll see in this code that __invoke has $labelPosition parameter that you can prepend or append with const LABEL_APPEND and LABEL_PREPEND.

In short, try to do something like this :

$this->formRorw($form->get('element'), 'prepend'); // Or append as you want
Colan answered 11/4, 2013 at 14:41 Comment(1)
it will not throw the element outside label tag, it will just put the element before label text or after label text, but in all cases: inside the label tagDorettadorette

© 2022 - 2024 — McMap. All rights reserved.