Thanks @Greg_808 the example there was just for the template.
So far I’m starting off with a base of:
# In form building function
$LinksCompField = CompositeField::create();
$LinksTemplateField = CompositeField::create(
TextField::create('EventLinksTemplate[Title]', 'Website label'),
TextField::create('EventLinksTemplate[Url]', 'Website Url')
)->setName('EventLinksTemplate');
if ($this->ExistingID)
{
$Event = Event::get_by_id('Event', $this->ExistingID);
$Links = $Event->EventLinks();
if ( ! empty($Links))
{
$LinksCompField = CompositeField::create();
foreach ($Links as $Link)
{
$LinksCompField->push(TextField::create('EventLinks['.$Link->ID.'][Title]', 'Website label')->setValue($Link->Title));
$LinksCompField->push(TextField::create('EventLinks['.$Link->ID.'][Url]', 'Website Url')->setValue($Link->Url));
}
}
}
else if ( ! empty($_POST) and array_key_exists('EventLinks', $_POST))
{
foreach ($_POST['EventLinks'] as $key => $Link)
{
$LinksCompField->push(TextField::create('EventLinks['.$key.'][Title]', 'Website label')->setValue($Link['Title']));
$LinksCompField->push(TextField::create('EventLinks['.$key.'][Url]', 'Website Url')->setValue($Link['Url']));
}
}
$LinksCompField->setName('Links');
Template:
<div id="LinkWrapper">
$Fields.FieldByName('Links')
</div>
<div id="LinkTemplate">
<!— Template below will be used for clone —>
$Fields.FieldByName('EventLinksTemplate')
</div>
<button id="AddMoreLinks" type="button">Add more links</button>
This is looking impossible, or at least difficult… Seems SS doesn’t allow dynamically added form fields (what the clones would be above) due to this line in FormRequestHandler
Cloned fields don’t appear in that list. Form->loadDataFrom also uses that list.
if there’s a field validation error, it never gets to the doEventsForm (so I could store $_POST) and of course the EventForm has no $_POST as it was redirected back.
So far resorted to extending FormRequestHandler and adding a session store of $_POST to the httpSubmission method for use in EventForm.
Nobody in the history of SS has ever needed dynamic fields on a front end form before?!