Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:40+00:00 2026-05-28T00:12:40+00:00

I have a form in which people shall be able to add the same

  • 0

I have a form in which people shall be able to add the same portion of elements with a plus-button, so that something like this is produced:

<div id="person-1" class="person">
    <input type="text" name="name-1" id="name-1" />
    <input type="text" name="age-1" id="age-1" />
</div>
<!-- as of here, it's JS created -->
<div id="person-2" class="person">
    <input type="text" name="name-2" id="name-2" />
    <input type="text" name="age-2" id="age-2" />
</div>
<div id="person-3" class="person">
    <input type="text" name="name-3" id="name-3" />
    <input type="text" name="age-3" id="age-3" />
</div>

I already managed to write jquery-code that allows me to add the same elements once again with a new id (name-1, age-1, name-2, age-2, name-3, age-3, …).

Of course, Zend_Form does not know about name-2 and name-3, so it just drops them when the form contains an error and is displayed again. Neither can I access the value of name-2 with $form->getValue('name-2'). I have to go over raw $this->getRequest()->getPost().

Is there a better method I can use to combine Zend_Form and javascript-based added form elements (of same type like an hardcoded element).

Caveat: In the real problem, it’s select and not input. Found out this could make a difference (with ->setIsArray(true)), but using select would blow up the example code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:12:41+00:00Added an answer on May 28, 2026 at 12:12 am

    What you could do is create a subform container inside your main form and add an X amount of subforms to that container.
    For example:

    class My_Form extends Zend_Form
    {
        private $_numPersons = 1;
    
        public function setNumPersons($numPersons)
        {
            $this->_numPersons = (int) $numPersons;
        }
    
    
        public function init()
        {
            $container = new Zend_Form_SubForm();
            $this->addSubForm($container, 'persons');
    
            for($index = 0; $index < $this->_numPersons; $index++) {
                $personForm = new My_PersonForm();
                $container->addSubForm($personForm, $index+1);
            }
        }
    }
    

    When rendered, the input fields will have names like persons[1][name]. Note the $index+1, Zend_Form does not allow a form to be named ‘0’.

    Ofcourse, you should only use this method if the amount of person subforms is limited.

    Another strategy would be to override the isValid method and use a single My_PersonForm form to validate all the person data.

    Sidenote; the above code will only work when you define the numPersons as part of the options set, when creating the form instance. E.g.;

    $form = new My_Form(array('numPersons' => 10));

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form which I'd like to alert people if ALL of the
I have a form in which people will be entering dollar values. Possible inputs:
I have a form which users can add controls to and when they right
i have a form for adding people object ,i want to add as many
I have a form which enables the submit button only when a checkbox is
I have a form which is used to insert/display and update . In the
I have a form which takes both the user details and an image uploaded
I have a form which has a lot of SELECTs. For various reasons, I'd
I have a form which I create manually (a lot of JavaScript in it...).
I have a form which users must fill out and submit. The controller action

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.