如何为Zend_Form_Element_Submit(zendframework 1)设置一个值

时间:2013-08-20 12:56:24

标签: zend-framework submit zend-form

我无法设置zend表单元素提交按钮(Zendframework1)的基本值。我基本上想在其中设置一个唯一的ID号,然后在提交按钮后检索此号码。

以下是我的代码。你不会尝试使用setValue()方法,但这不起作用。

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

  $this->addElement($new);

我也很感激有关我用来接收价值的建议。即我将调用哪种方法来检索值?

2 个答案:

答案 0 :(得分:1)

标签用作提交按钮上的值,这是提交的内容。没有办法将另一个值作为按钮的一部分提交 - 您需要更改提交名称或值(=标签)。

您可能想要做的是在表单中添加一个隐藏字段,然后将其赋予您的数值。

答案 1 :(得分:1)

这有点棘手,但并非不可能:

您需要实现自己的视图助手并将其与元素一起使用。

首先,您必须添加自定义视图助手路径:

How to add a view helper directory (zend framework)

实施帮助:

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;
    }

}

因此,您将帮助器分配给元素:

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

这样,您可以检索提交表单的值:

$form->getValue( 'submitElementName' );