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 5932697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:52:12+00:00 2026-05-22T14:52:12+00:00

I am trying to dynamically add fields to my CakePHP form, but it seems

  • 0

I am trying to dynamically add fields to my CakePHP form, but it seems that each method I used had its own downsides which I couldn’t fix. Here is what I tried:

  1. Don’t add a new input, just separate the values in a single input with a comma. It seemed pretty easy, with some problems when trying to explode the input and save each value in a new row in the database. But I abandoned the idea when I had to edit these values…gather all the values and implode them in an input, then when I saved check whether all values are still there, if not, delete the corresponding row from the table…To much work to do.

  2. I tried creating new inputs on the fly, with JS. But here, I stumbled upon 2 problems: the Security component, which was throwing a blackhole attempt (and I really need this component to check the integrity of the form) and the fact that if validation fails, my inputs will disappear together with their values. I must add that a single Model may have unlimited fields, so the inputs will be something like name=[Model][14][field], name=[Model][17][field], therefore I can’t restrict the Security component not to validate those certain inputs (I know their IDs only when I add them with JS, anyway, way after the Security component builds its data).

  3. Adding fields with PHP (therefore, reload the page after a new field is added), but I cannot seem to find out how do I add a field exactly after another present field, and also, how to maintain the input’s data upon validation errors.

Any suggestion is highly appreciated!

Thank you!

  • 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-22T14:52:13+00:00Added an answer on May 22, 2026 at 2:52 pm

    I’m not sure I can give you a full answer to your question, but hopefully I can give you a couple ideas.

    I found myself in a similar situation when trying to create an admin system for polls.

    Each Poll had many PollOptions and I wanted to make it possible to dynamically add as many poll options as needed on my polls/admin_edit page.

    I managed this with CakePHP’s built-in functionality plus a bit of Javascript.

    When building the form in the admin_edit view, I first put in the Poll fields and under that I added this:

    <div id="poll-options">
    <?php 
    if (isset($this->data['PollOption'])) {
        $i = 0;
        foreach ($this->data['PollOption'] as $opt) {
            echo $form->hidden("PollOption.$i.id");
            echo $form->input("PollOption.$i.name", array('label' => "Option " . ($i + 1)));
            $i++;
        }
    }
    ?>
    </div>
    

    $this->data was set in PollsController. If the Poll already had related PollOptions, they were also included in $this->data. Also, if the form had already been submitted and there were validation errors, all the needed PollOption fields were built again when the page displayed because they were still in $this->data from the controller.

    So that took care of making sure I always had the fields I needed in the view.

    When the form was submitted, I attempted to save the data with a simple

    $this->Poll->saveAll($this->data, array('atomic' => false, 'validate' => 'first'));
    

    (You can check the CakePHP API or docs if you’re not sure of the above syntax)

    I used Javascript (jQuery) to dynamically add the PollOption fields:

    $('#add-option-button').click(function(event){
        var optionCount = $('#poll-options > div').size() + 1;
        var inputHtml = '<div class="input text"><label for="PollOption' + optionCount + 'Name">Option ' + optionCount
            + '</label><input id="PollOption' + optionCount + 'Name" type="text" name="data[PollOption][' + optionCount + '][name]" /></div>';
        event.preventDefault();
        $('#poll-options').append(inputHtml);
    });
    

    You mentioned you were doing this yourself, but you shouldn’t have any trouble with it if you don’t try to use the Security component on the form.

    Hope this helps point you in the right direction, or maybe give you some ideas.

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

Sidebar

Related Questions

I have 3 HTML form inputs fields that is dynamically generated by a add
I'm trying to dynamically add elements to a Cakephp form: <?php echo $javascript->link('jquery',false); ?>
Trying to make a make generic select control that I can dynamically add elements
I am trying to pass dynamically created form fields to a php script, and
I am trying to dynamically add form controls to a gui I'm developing in
How add custom field dynamicly? I'm trying that, but the field won't insert into
I'm trying to dynamically add elements to a vector that is contained within a
Trying to dynamically add a user control that dynamically generates content. The User Control
we are trying to add ColumnDefinition to a Grid in WPF dynamically from code
Question I am trying to dynamically get the default for a type that is

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.