Zend framework 如何为Zend_Form_元素_Submit(zendframework 1)设置值

Zend framework 如何为Zend_Form_元素_Submit(zendframework 1)设置值,zend-framework,submit,zend-form,Zend Framework,Submit,Zend Form,我在设置zend表单元素提交按钮(Zendframework1)的基本值时遇到问题。我基本上想在其中设置一个唯一的id号,然后在按钮提交后检索该编号 下面是我的代码。您不会注意到我试图使用setValue()方法,但这不起作用 $new = new Zend_Form_Element_Submit('new'); $new ->setDecorators($this->_buttonDecorators) ->setValue($tieredPrice->

我在设置zend表单元素提交按钮(Zendframework1)的基本值时遇到问题。我基本上想在其中设置一个唯一的id号,然后在按钮提交后检索该编号

下面是我的代码。您不会注意到我试图使用setValue()方法,但这不起作用

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

  $this->addElement($new);

我也将感谢任何关于我使用什么来接收价值观的建议。i、 e我将调用什么方法来检索值?

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


您可能想做的是在表单中添加一个隐藏字段,并将其改为数值

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

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

首先,必须添加自定义视图辅助对象路径:

实现您的助手:

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

}
通过这种方式,您可以检索已提交表单的值:

$form->getValue( 'submitElementName' );

嗨,蒂姆·本顿。非常感谢你的帮助。我听从了你的建议。热情问候和ea与使用内置的
$new->setLabel()
方法相比,这种方法的优势是什么?您是对的,最好使用
setLabel
而不是
setAttrib('customLabel')
(在助手中,
$this->\u label
而不是
$attribs['customLabel']
)嗨,亚历克斯和蒂姆。我对Alex的解决方案和Tim随后的评论完全感到困惑。Alex说定制提交按钮确实是可能的。如果是,customSubmit的方法是什么;Andreea,我刚刚编辑了我的代码并对其进行了测试:它允许您获取提交的值,即使我真的不理解您为什么需要这种行为。使用这种方法,我不知道如何直接使用
$form->setValue()
,您必须使用
$form->setAttib('value','XXX')
。你不必自己调用
customSubmit
方法,你所需要的就是我的响应。我的观点是,据我所知,这个助手没有添加任何不能使用内置在ZF中的formSubmit助手完成的操作。您已经可以设置submit按钮的值(只是使用
setLabel()
时有点混淆)。但这并没有真正帮助OP,因为她想用按钮提交一个数值(可能不是你想向用户显示的东西)。
$form->getValue( 'submitElementName' );