1

I have am having trouble setting the basic values of a zend form element submit button (Zendframework1). I basically want to set a unique id number in it and then retrieve this number once the button has been submitted.

Below is my code. you will nots that I tried to use the setValue() method but this did not work.

$new = new Zend_Form_Element_Submit('new');
  $new
   ->setDecorators($this->_buttonDecorators)
   ->setValue($tieredPrice->sample_id) 
   ->setLabel('New');

  $this->addElement($new);

I would also appreciate any advice on what I use to receive the values. i.e what method I will call to retrieve the values?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
andreea115
  • 289
  • 1
  • 3
  • 14

2 Answers2

1

The label is used as the value on submit buttons, and this is what is submitted. There isn't a way to have another value submitted as part of the button as well - you'd either need to change the submit name or value (= label).

What you probably want to do instead is add a hidden field to the form and give that your numeric value instead.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
1

It's a little bit tricky but not impossible :

You need to implement your own view helper and use it with your element.

At first, you must add a custom view helper path :

How to add a view helper directory (zend framework)

Implement your helper :

class View_Helper_CustomSubmit extends Zend_View_Helper_FormSubmit
{
    public function customSubmit($name, $value = null, $attribs = null)
    {
        if( array_key_exists( 'value', $attribs ) ) {
            $value = $attribs['value'];
            unset( $attribs['value'] );
        }

        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable, id
        // check if disabled
        $disabled = '';
        if ($disable) {
            $disabled = ' disabled="disabled"';
        }

        if ($id) {
            $id = ' id="' . $this->view->escape($id) . '"';
        }

        // XHTML or HTML end tag?
        $endTag = ' />';
        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
            $endTag= '>';
        }

        // Render the button.
        $xhtml = '<input type="submit"'
                . ' name="' . $this->view->escape($name) . '"'
                        . $id
                        . ' value="' . $this->view->escape( $value ) . '"'
                                . $disabled
                                . $this->_htmlAttribs($attribs)
                                . $endTag;

        return $xhtml;
    }

}

So, you assign the helper to the element :

$submit = $form->createElement( 'submit', 'submitElementName' );
$submit->setAttrib( 'value', 'my value' );  
$submit->helper = 'customSubmit';
$form->addELement( $submit );

This way, you can retrieve the value of the submitted form :

$form->getValue( 'submitElementName' );
Community
  • 1
  • 1
Alexandre Brach
  • 325
  • 2
  • 12
  • What would be the advantage of this approach over using the built in `$new->setLabel()` method? – Tim Fountain Aug 20 '13 at 15:28
  • You're right, it would be better to use `setLabel` instead of `setAttrib( 'customLabel' )` (and in the helper, `$this->_label` instead of `$attribs['customLabel']`) – Alexandre Brach Aug 20 '13 at 15:37
  • hi alex and Tim. i am totally confused wiht Alex solution-and Tim subsequent comment. Is Alex saying that it is indeed possible to make a custom submit button. if so, what is the customSubmit method for; is that for receiving the values or for creating the button. – andreea115 Aug 20 '13 at 16:01
  • Andreea, I've just edit my code and I have tested it : it allow you to get the submitted value , even if I don't really understand why do you need this behavior. With this method, I don't know how to use `$form->setValue()` directly, you have to use `$form->setAttib( 'value', 'XXX' )`. You don't have to call the `customSubmit` method yourself, all you need is in my response – Alexandre Brach Aug 20 '13 at 19:54
  • My point was, as far as I can see this helper doesn't add anything that can't be done with the formSubmit helper built into ZF. You can already set the submit button's value (just slightly confusingly using `setLabel()`). But this doesn't really help the OP since she's wanting to submit a numeric value with the button (probably not something you'd want to show to the user). – Tim Fountain Aug 21 '13 at 09:18