ZF-5056: value of button is empty

Issue Type: Bug Created: 2008-11-24T19:23:14.000+0000 Last Updated: 2013-01-08T08:33:15.000+0000 Status: Resolved Fix version(s): - 1.12.2 (25/Feb/13)

Reporter: xletian (xletian) Assignee: Frank Brückner (frosch) Tags: - Zend_Form

  • zf-crteam-priority
  • zf-crteam-review

Related issues: - ZF-3961

Attachments: - Button.php.patch


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


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


Posted by Matthew Weier O'Phinney (matthew) on 2008-11-24T19:52:46.000+0000

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.

Posted by Anselm Föhr (anselm) on 2009-04-29T06:37:40.000+0000

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

<pre class="highlight">
$form = new Zend_Form();
$b = new Zend_Form_Element_Button('submit');

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

Actual result: a button without value attribute.

Posted by Matthew Weier O'Phinney (matthew) on 2009-04-29T07:35:38.000+0000

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.

Posted by Michaël Perrin (sweedymick) on 2009-07-17T07:39:12.000+0000

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 :

<pre class="highlight">
        // 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:

<pre class="highlight">
--- 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 :… . 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.

Posted by Christian Albrecht (alab) on 2010-04-05T04:24:02.000+0000

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.

Posted by Olivier Mellinger (parktrip) on 2010-04-26T00:29:23.000+0000

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

Posted by Thomas G. (fernandorey) on 2011-02-17T09:42:26.000+0000

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

Instead, you should use

<pre class="highlight">
$button1Checked = ($form->button1->getValue() !== null);

Posted by Thomas (mctom987) on 2012-03-06T00:53:59.000+0000

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

Posted by Thomas (mctom987) on 2012-03-06T00:59:58.000+0000

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.

<pre class="literal">$submit = $this->createElement('button', 'submit');
$submit->setAttrib('type', 'submit')
       ->setAttrib('value', 'save')
// Save

<pre class="literal">$this->getView()->formButton('submit', 'save', array('type' => 'submit'));
// save

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

Posted by Claude Duvergier (cduv) on 2012-12-19T17:53:12.000+0000

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

Posted by Frank Brückner (frosch) on 2012-12-20T16:16:40.000+0000

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

Posted by Frank Brückner (frosch) on 2012-12-21T08:42:05.000+0000

@Claude You forgot the comment from Christian Albrecht.

<pre class="highlight">
 * @group ZF-5056
public function testRenderingFormWithTwoButtonElementsAfterValidation()
    // Set decorators

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

Posted by Frank Brückner (frosch) on 2012-12-21T08:47:18.000+0000

My suggestion.

Posted by Rob Allen (rob) on 2012-12-22T11:49:39.000+0000

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

Posted by Frank Brückner (frosch) on 2013-01-08T08:33:15.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.