1

Zend フォーム要素の送信ボタン (Zendframework1) の基本的な値の設定に問題があります。私は基本的に一意の ID 番号を設定し、ボタンが送信されたらこの番号を取得したいと考えています。

以下は私のコードです。setValue() メソッドを使用しようとしましたが、これは機能しませんでした。

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

  $this->addElement($new);

また、値を受け取るために何を使用するかについてのアドバイスもいただければ幸いです。つまり、値を取得するためにどのメソッドを呼び出すか?

4

2 に答える 2

1

ラベルは送信ボタンの値として使用され、これが送信されます。ボタンの一部として別の値を送信する方法はありません。送信名または値 (= ラベル) を変更する必要があります。

おそらく代わりにやりたいことは、非表示フィールドをフォームに追加し、代わりに数値を与えることです。

于 2013-08-20T14:10:17.973 に答える
1

少しトリッキーですが、不可能ではありません:

独自のビュー ヘルパーを実装し、要素で使用する必要があります。

最初に、カスタム ビュー ヘルパー パスを追加する必要があります。

ビュー ヘルパー ディレクトリを追加する方法 (zend フレームワーク)

ヘルパーを実装します:

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' );
于 2013-08-20T15:11:25.767 に答える