Zend\Form\View\Helper\FormRow: labels are appended by default #3626

Closed
Martin-P opened this Issue Jan 31, 2013 · 6 comments

Comments

Projects
None yet
4 participants
Contributor

Martin-P commented Jan 31, 2013

This commit 8b5ed9e from 6 days ago breaks standard functionality. Labels are appended by default now due to these changes. When I revert these changes everything works as expected again.

I read this fix is intended as it is (zendframework#3544) and the default action when an id is present is to append the label? I understand that you want this with a checkbox or radio element, but making this the default for all other fields makes no sense to me. I can not imagine I would want the label after my textarea or text-input, not even when I use an id.

Edit
The presence or absense of an id should not affect the layout, it should merely do what it should do: add an id.
Suggested fix: Make it configurable just as it is when no id is provided.

Contributor

iquabius commented Mar 9, 2013

Can you take a look at this @bakura10?

Contributor

bakura10 commented Mar 9, 2013

I'll review everything next thursday, I'll be super busy this week :).

Envoyé de mon iPhone

Le 9 mars 2013 à 21:42, Josias Duarte Busiquia notifications@github.com a écrit :

Can you take a look at this @bakura10?


Reply to this email directly or view it on GitHub.

Contributor

Martin-P commented Mar 27, 2013

Simply reverting the changes you made is enough to fix this issue. Everything already was configurable before you made the changes.

Contributor

Martin-P commented Mar 28, 2013

I looked into it better and 8b5ed9e changed how the formRow view helper works.

Before the changes in 8b5ed9e the next call to formRow() would still prepend the label by default. The changes in 8b5ed9e altered this behaviour and because of the append of the first formRow() all following formRow()-calls append the label as well.

Before those changes I could do this, but due to the changes made, the labels get appended because of the first append:

<?php
<?php
$form = new \Zend\Form\Form();
$form->add(array(
    'type' => 'checkbox',
    'name' => 'checkbox-name',
    'attributes' => array(
        'id' => 'checkbox-id',
    ),
    'options' => array(
        'label' => 'Checkbox label',
    ),
));
$form->add(array(
    'type' => 'text',
    'name' => 'text-name',
    'attributes' => array(
        'id' => 'text-id',
    ),
    'options' => array(
        'label' => 'Text label',
    ),
));
$form->add(array(
    'type' => 'textarea',
    'name' => 'textarea-name',
    'attributes' => array(
        'id' => 'textarea-id',
    ),
    'options' => array(
        'label' => 'Textarea label',
    ),
));

$form->prepare();
?>
<pre><?php echo $this->escapeHtml($this->formRow($form->get('checkbox-name'), 'append')); ?></pre>
<pre><?php echo $this->escapeHtml($this->formRow($form->get('text-name'))); ?></pre>
<!--
Output: <input type="text" name="text-name" id="text-id" value=""><label for="text-id">Text label</label>
-->
<pre><?php echo $this->escapeHtml($this->formRow($form->get('textarea-name'))); ?></pre>
<!--
Output: <textarea name="textarea-name" id="textarea-id"></textarea><label for="textarea-id">Textarea label</label>
-->

Now I must "reset" the append of the first formRow()-call and explicitly set the next call to formRow() to prepend and do this:

<?php
$form = new \Zend\Form\Form();
$form->add(array(
    'type' => 'checkbox',
    'name' => 'checkbox-name',
    'attributes' => array(
        'id' => 'checkbox-id',
    ),
    'options' => array(
        'label' => 'Checkbox label',
    ),
));
$form->add(array(
    'type' => 'text',
    'name' => 'text-name',
    'attributes' => array(
        'id' => 'text-id',
    ),
    'options' => array(
        'label' => 'Text label',
    ),
));
$form->add(array(
    'type' => 'textarea',
    'name' => 'textarea-name',
    'attributes' => array(
        'id' => 'textarea-id',
    ),
    'options' => array(
        'label' => 'Textarea label',
    ),
));

$form->prepare();
?>
<pre><?php echo $this->escapeHtml($this->formRow($form->get('checkbox-name'), 'append')); ?></pre>
<pre><?php echo $this->escapeHtml($this->formRow($form->get('text-name'), 'prepend')); ?></pre>
<!--
Output: <label for="text-id">Text label</label><input type="text" name="text-name" id="text-id" value="">
-->
<pre><?php echo $this->escapeHtml($this->formRow($form->get('textarea-name'))); ?></pre>
<!--
Output: <label for="textarea-id">Textarea label</label><textarea name="textarea-name" id="textarea-id"></textarea>
-->

In short: everything works fine, but your change altered the basic functionality of the formRow() view helper.

Contributor

iquabius commented Mar 28, 2013

Just to make sure I understood the problem...

The only issue is that the view helper is caching the append/prepend option? right?

Contributor

Martin-P commented Mar 28, 2013

The only issue is that the view helper is caching the append/prepend option? right?

Yes, that is the problem indeed. I could not find the exact problem when I opened this issue, but I do have a solution to fix it now.

On line 163 of Zend\Form\View\Helper\FormRow the label position can be reset to prepend if none provided by adding the else:

if ($labelPosition !== null) {
    $this->setLabelPosition($labelPosition);
} else {
    $this->setLabelPosition(self::LABEL_PREPEND);
}

iquabius added a commit to iquabius/zf2 that referenced this issue Mar 29, 2013

@ghost ghost assigned weierophinney Apr 12, 2013

weierophinney added a commit that referenced this issue Apr 12, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment