2009-10-28 10 views
5

I'want do renderowania:Zend_Form array notacji i puste elementy nazwy

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

ale Zend_Form_Element wymagają (string) imię, więc muszę zrobić:

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

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

ale wyjście jest:

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

mogę też przyjąć wyjście jak:

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

ale Zend_Form_Element przepisać elementów o tej samej nazwie

czy istnieje sposób, aby zrobić to, co trzeba?

+0

Chcę tego samego! Daj mi znać, jeśli to rozwiążesz. –

Odpowiedz

7

Dla wielu wartości:

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

Generuje: name="foo[]"

-

Jeśli szukasz podanych kluczy takich jak name="foo[bar]", zastosowanie:

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

- -

Testowany na ZF 1.11.5

0

klasa MyFooForm rozszerza Zend_Form { publicznego init() { $ fullNameOpts = array ( 'wymagane' => false, 'label' => 'fullname', 'isArray' => true, ' validators '=> array (tablica (' stringLength ', false, tablica (1, 250))) ); $ this-> addElement ("tekst", "fullName", $ fullNameOpts); // reszta elementów, form i rzeczy idzie tu } }

I że nie tworzy

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

to na Element.php, w formie, linię 512 "isArray" wyboru. Używam zwykłego zend_form, crossValidation z niestandardowymi walidatorami i pcham podformularze do replikowania głównego formularza, ponieważ użytkownik może dodać wiele razy ten sam formularz. Dodatkowo jestem zbyt leniwy do badania niestandardowych dekoratorów, stworzyłem jeden, ale zabija on subForms i notację tablicową, więc po prostu trzymam się normalnych, i to rozwiązuje.

Jestem w Zf 1,10.

+0

Generalnie, kiedy używamy pustej notacji tablicowej, mamy wiele pól o tej samej nazwie. Jeśli spróbujesz utworzyć nowy element o tej samej nazwie, spodziewając się nowego pola fullName [], to nie zadziała –

+0

Napisz kod, a dam ci rękę :). Zrobiłem lib, żeby sobie z tym poradzić, a moim następnym celem jest właśnie to. Masz jeden formularz i chcesz * powielić * tylko 1 element formularza. –

Powiązane problemy