0
public function init()
{
    $txt = $this->createElement('text', '0')
            ->setBelongsTo('txt');
    $this->addElement($txt);

    $fields = $this->createElement('text', '0')
            ->setBelongsTo('fields');
    $this->addElement($fields);
}

But in this case one of fields is not displayed. How to make both field's arrays start from 0. I could leave txt[] and fields[] but when I do this->populate($_POST); it doesn't work.

1 Answer 1

1

There is something wrong in general. The second parameter you pass to the element has to be its name, which has to be unique (form field name). The setBelongsTo calls will not work because your elements do not have the respective names. What you are doing is to create an element with the name 0 and then overwrite it with another element, which is the one displayed. You will see this, if you look at your source code in the browser. If you're using setIsArray then you should make both elements belong to the same array.

I don't know exactly what you want to achieve but it should look more like this:

public function init() 
{
    $txt = $this->createElement('text', 'mytext')
                ->setBelongsTo('myarray');

    $this->addElement($txt);

    $fields = $this->createElement('text', 'myothertext')
                   ->setBelongsTo('myarray');

    $this->addElement($fields); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Output of Your code will be <input type="text" name="mytxet[mytext]" id="txt-txt" value="">and I want <input type="text" name="txt[0]" id="txt-0" value="">
use createElement('text', '0'), createElement('text', '1')

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.