5

レンダリングしたい:

<input type="text" value="" name="foo[]" />
<input type="text" value="" name="bar[]" />

しかし、Zend_Form_Element には (文字列) 名が必要なので、次のようにする必要があります。

$this->addElement('text', '1', array(
    'belongsTo' => 'foo'
));

$this->addElement('text', '2', array(
    'belongsTo' => 'bar'
));

しかし、出力は次のとおりです。

<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-2"  type="text" value="" name="bar[2]" />

次のような出力も受け入れることができます。

<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-1"  type="text" value="" name="bar[1]" />

しかし Zend_Form_Element は同じ名前の要素を書き換えます

私が必要なことをする方法はありますか?

4

2 に答える 2

7

複数の値の場合:

$foo = new Zend_Form_Element_Text('foo');
// Other parameters
$foo->setIsArray(TRUE);
$this->addElement($foo);

生成:name="foo[]"

--

などの特定のキーを探している場合は、次name="foo[bar]"を使用します。

$bar= new Zend_Form_Element_Text('bar');
// Other parameters
$bar->setBelongsTo('foo');
$this->addElement($bar);

--

ZF 1.11.5 でテスト済み

于 2011-05-16T18:34:02.073 に答える
0

class MyFooForm extends Zend_Form { public function init() { $fullNameOpts = array( 'required'=>false, 'label'=>'fullName', 'isArray'=>true, 'validators' => array( array('stringLength ', false, array(1, 250) ) ) ); $this->addElement('text' ,'fullName',$fullNameOpts); // 残りの要素、フォームなどはここに入る } }

そして、それは作成します

<dd id="fullName-element"><input type="text" class="inputAccesible" value="" id="fullName"name="fullName[]"></dd>

Element.php の Form の 512 行目の "isArray" チェックにあります。私は通常の zend_form と crossValidation をカスタムバリデータで使用しており、サブフォームをプッシュしてメインフォームを複製しています。これは、ユーザーが同じフォームを複数回追加できるためです。さらに、私はカスタムデコレータを研究するのが面倒で、作成しましたが、サブフォームと配列表記を殺してしまうので、通常のものを使い続けるだけで解決します

私はZf1.10です。

于 2010-06-22T01:34:10.787 に答える