简体   繁体   中英

Zend Form: Get array element in view script

I want to display a title and a content field for each language. So, in the form, I have:

foreach ($languages as $language)
{
    // Add the title element
    $title = new Zend_Form_Element_Text($language);
    $title->setLabel($translate->_('News Title'))
          ->setBelongsTo('title');
    $this->addElement($title);

    // Add the content element
    $content = new Zend_Form_Element_Textarea($language);
    $content->setLabel($translate->_('News Content'))
            ->setBelongsTo('content');
    $this->addElement($content);
}

If I render the form in the usual way it works perfectly:

echo $this->form;

However, I want to render each field separately to include some HTML in the middle and other jQuery stuff. My problem is that I cannot manage to access those elements. I tried

foreach ($languages as $language)
{
    $this->form->getElement($language);
}

but it only renders 'content' element. Am I overriding 'title' element?

Thanks

Yes, you're overriding the Title element. The parameter you pass to new Zend_Form_Element_Text($language); ( $language in your case) should be unique. Infact you can use it to identify and retrieve the element when you need.

To setup the param you can do something like this:

foreach ($languages as $language)
{
    // Add the title element
    $title = new Zend_Form_Element_Text('title-' . $language);

   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM