Monday, 15 April 2013

html - Can I make array input fields like name[] in Zend Form 1.11 -



html - Can I make array input fields like name[] in Zend Form 1.11 -

how can create input name in zend framework 1.11 zend form

<input name="name[]" class="name"> <input name="name[]" class="name"> <input name="name[]" class="name">

i not want index/key in array. want dynamic.

you can seek create own view_helper.

i can propose this:

in my library, create helper directory. in directory, create formarray.php file this: (it's adaptation of zend_view_helper_formtext class)

class zend_view_helper_formarray extends zend_view_helper_formelement { public function formarray($name, $value = null, $attribs = null) { $info = $this->_getinfo($name, $value, $attribs); extract($info); // name, value, attribs, options, listsep, disable // build element $disabled = ''; if ($disable) { // disabled $disabled = ' disabled="disabled"'; } $sep = ''; $end = 1; if (isset($attribs['nb_repeat']) && is_int($attribs['nb_repeat']) && $attribs['nb_repeat'] >1) $end = $attribs['nb_repeat']; if (isset($attribs['sep_repeat'])) $sep = $attribs['sep_repeat']; $xhtml = ''; unset($attribs['nb_repeat']); unset($attribs['sep_repeat']); ($i = 1; $i <= $end; $i++){ if ($i != 1) $xhtml .= $sep; $xhtml .= '<input name="' . $this->view->escape($name) . '[]"' . ' value="' . $this->view->escape($value) . '"' . $disabled . $this->_htmlattribs($attribs) . $this->getclosingbracket(); } homecoming $xhtml; } }

as can see, add together 2 attributes nb_repeat , sep_repeat define number of input want , separator betwwen each. remove id attribute.

in controller, add together path of view helper this:

$this->view->addhelperpath('my/helper/', 'my_helper');

and now, in form, can create element this:

$test = new zend_form_element_text('name'); $test->setattrib('class', 'name') ->setattrib('nb_repeat', 3) // have 3 input ->setattrib('sep_repeat', "\n") // have new line betwwen each input in code source ->adddecorators(array( array('viewhelper', array('helper' => 'formarray') ), ) );

i hope help you. :)

html zend-framework zend-form

No comments:

Post a Comment