Issues

ZF-5056: value of button is empty

Description

I don't know why the code was added in the file,but the value of button always is empty.

file: library\Zend\Form\Decorator\ViewHelper.php :197

code:

            if (stristr($type, 'button')) {
                $element->content = $element->getLabel();
                return null;
            }

Comments

I needmore information. I know why the code reads that way, but I need to know a) what code you wrote, b) what your expected result was, and c) what the actual result was. Without that, I have no idea what you want fixed.

The Problem is that you can't setValue for a button:


$form = new Zend_Form();
$b = new Zend_Form_Element_Button('submit');
$b->setValue('foo');
$form->addElement($b);

Expected output when rendering the form: a button with value attribute.

Actual result: a button without value attribute.

Hmm.. differs from in this regard, as the button label for an is its value. As such, we used setLabel() to set the value for buttons -- but then does not get a value attribute.

I'll triage this following the 1.8.0 release.

While this behavior is accurate for , it is not for as this one handles both a label and a value. At its current state, the Zend Framework makes no use of the defined value of when rendering the element, although there is some code about it in the FormButton helper :


        // add a value if one is given
        if (!empty($value)) {
            $xhtml .= ' value="' . $this->view->escape($value) . '"';
        }

The best way to avoid this problem is to apply this patch:


--- Form/Decorator/ViewHelper.php    (revision 15671)
+++ Form/Decorator/ViewHelper.php    (working copy)
             if ($element instanceof $type) {
                 if (stristr($type, 'button')) {
                     $element->content = $element->getLabel();
-                    return null;
+                    return $element->getValue();
                 }
                 return $element->getLabel();
             }

Just as a little warning, don't forget that when submitting the form, Internet Explorer 6 and 7 send the inner HTML of the button tag instead of the content of the value attribute, as described here : http://msdn.microsoft.com/en-us/library/… . Internet Explorer 8 doesn't, but for older version some workarounds need to be implemented. So if the value attribute is defined, the submitted data will defer for Internet Explorer than what it is currently.

Another problem arises when you validate a submitted Form with mixed and

When submitting the Form by hitting "baz", "foo" will not be submitted, hence the Value empty.

After Validation will be rendered.

Is there any reason for that little bug not to be fixed ? Because it's quite annoying if we want to deploy html5 websites

If you need to check if a button was clicked, isChecked() will not work because of this bug.

Instead, you should use


$button1Checked = ($form->button1->getValue() !== null);

Bump. Please fix this. Zend_Form_Element_Button::setValue() is ignored, yet Zend_View_Helper_FormButton::setValue() works as expected.

$submit = $this->createElement('button', 'submit'); $submit->setAttrib('type', 'submit') ->setAttrib('value', 'save') ->setLabel('Save') ->setDecorators(array('ViewHelper')); // Save

$this->getView()->formButton('submit', 'save', array('type' => 'submit')); // save

Bump. Please fix this. Zend_Form_Element_Button::setValue() is ignored, yet Zend_View_Helper_FormButton::setValue() works as expected. I'm using ZF 1.11.11 and this is still an issue.

$submit = $this->createElement('button', 'submit');
$submit->setAttrib('type', 'submit')
       ->setAttrib('value', 'save')
       ->setLabel('Save')
       ->setDecorators(array('ViewHelper'));
// Save
$this->getView()->formButton('submit', 'save', array('type' => 'submit'));
// save

Sorry for the DP, couldn't find an "edit".

Still an issue on version 1.12.1: any reason not to fix this by applying Michaël Perrin or Thomas's patch?

@Claude Unit test are missing, but I will fix this.

@Claude You forgot the comment from Christian Albrecht.


/**
 * @group ZF-5056
 */
public function testRenderingFormWithTwoButtonElementsAfterValidation()
{
    // Set decorators
    $this->form->setDecorators(
        array(
             'FormElements',
        )
    );
    $this->form->setElementDecorators(
        array(
             'ViewHelper',
        )
    );

    // Add elements
    $this->form->addElement('button', 'foo', array('value' => 1));
    $this->form->addElement('button', 'bar', array('value' => 2));

    // Validate form
    $this->form->isValid(array('foo' => 1));

    $expected = 'foo'
              . PHP_EOL
              . PHP_EOL
              . 'bar';

    // Test
    $this->assertEquals(
        $expected,
        trim($this->form->render($this->getView()))
    );
}

My suggestion.

Frank's patch looks good to me. Please commit to trunk and release-1.12.

Fixed on trunk (25188) and release-1.12 (25189)