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:
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.
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).
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!