Firstly, many thanks for all the hard work on Chronforms which continues to be a great tool.
I have migrated most of my forms from CF7 to CF8 but there is one issue that is stopping me from migrating the last two forms which I describe below. The issue is on the Repeater loop, and in particular the creation of initial instances of a loop.
I have created a simple form to describe the issue and the workaround I have found.
In this simple form, I have set
- The repeater name in Repeater Area's wizard setting to "craft_loop".
- The key name to "nnnn" with a start count of 0 and max of 10.
- I have a single field inside the repeater with field name "craft"
I insert a PHP script that sets up two initial repeater instances (these would normally be read from a database):
$this->data['craft_loop'][1]="1";
$this->data['craft'][1] = "Laser";
$this->data['craft_loop'][2]="1";
$this->data['craft'][2] = "Phantom";
On inspection of the html created when the form is loaded, I see the form has two loop instances with the field with names:
<input type="text" name="craft[1]" id="craft_nnnn" placeholder="" value="" fdprocessedid="glouoa">
<input type="text" name="craft[2]" id="craft_nnnn" placeholder="" value="" fdprocessedid="jqznwx">
If I then add a third instance manually within the form using the an "add clone" button it gets named
<input type="text" id="craft_nnnn-3" placeholder="" value="" oname="craft[nnnn]" name="craft[3]" fdprocessedid="d6ubm">
Note the structure of the ID field is different, where the manually created id has "-3" appended for the 3rd instance.
In addition there is a hidden field that, for the initial instances, has the structure
<input type="hidden" name="craft_loop[nnnn]" value="1">
<input type="hidden" name="craft_loop[nnnn]" value="1">
and for instances created within the form looks like this:
<input type="hidden" value="1" oname="craft_loop[nnnn]" name="craft_loop[1]">
This difference in names is causing problems for listeners and other javascript I have. To make them consistent I located file
~/administrator/components/com_chronoforms8/pages/chronoforms/views/area_repeater/output.php
and made a couple of changes:
line 29 from
<input type="hidden" name="<?php echo CF8::getname($element); ?>[<?php echo $key; ?>]" value="1">
to
<input type="hidden" name="<?php echo CF8::getname($element); ?>[<?php echo $k; ?>]" value="1">
line 24 from
echo str_replace("[".$key."]", "[".$k."]", $content);
to
if($key!=$k)
{
//replace all occurances of loop [loopkey] with [loopindex]
$content = str_replace("[".$key."]", "[".$k."]", $content);
// then replace all occurances of _loopkey with _loopkey-loopindex - (this probably needs more rigor)
$content = str_replace("_".$key, "_".$key."-".$k, $content);
//dd($content);
}
echo $content;
This brings about more consistency in the html generated, and my listeners and javascript work on both initial and manually added repeats.
Can these changes or more thorough changes be included in a future release of CF8?
There were some other issues that I encountered but found acceptable work arounds:
- I found that setting values for fields in the initial repeats in the php action do not get copied into the fields.
- I have got round this by writing some javascript to copy the values across to the fields from the data structure.
- For the pre-created repeats, none of the listeners set up in the wizard designer worked.
- I got round this by adding javascript code to add the listeners for the repeats.
Environment is
- CF8 8.0.49
- Joomla: 8.4.0
- PHP 8.3.26