Question

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?

Was it helpful?

Solution

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" />

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top