1

How can I make input name like this in Zend Framework 1.11 with Zend Form

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

I do not want index/key in the array. I want it dynamic.

1 Answer 1

1

You can try do make your own View_Helper.

I can propose this:

In a My library, create Helper directory.
In this directory, create a FormArray.php file like this: (it's an 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 the 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']);
        for ($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();
        }
        return $xhtml;
    }
}

As you can see, I add 2 attributes nb_repeat and sep_repeat to define the number of input that you want and the separator betwwen each.
I also remove the id attribute.

In your Controller, add the path of this view Helper like this:

$this->view->addHelperPath('My/Helper/', 'My_Helper'); 

And now, in your form, you can create your element like this:

$test = new Zend_Form_Element_Text('name');
$test->setAttrib('class', 'name')
      ->setAttrib('nb_repeat', 3)    // to have 3 input
      ->setAttrib('sep_repeat', "\n") // to have a new line betwwen each input in your code source
      ->addDecorators(array(
                          array('ViewHelper',
                                    array('helper' => 'formArray')
                               ),                                                                                       
                          )                           
                     );

I hope it will help you. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.