How to copy a field by clicking on a button and submit it with Kirby inside the form? The number of repeated fields can be endless.
Example of what I would like to have
Controller
<?php
return function ($kirby, $page) {
if($kirby->user()) {
} else {
go('/login');
}
// if the form has been submitted…
if ($kirby->request()->is('POST') && get('submit')) {
// check the honeypot and exit if is has been filled in
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'title' => get('title'),
'text' => get('text'),
'problems' => get('problems'),
'tipp' => get('tipp'),
'place' => get('place'),
'state' => get('state'),
'financetext' => get('financetext'),
'financeneeds' => get('financeneeds'),
'initiatortext' => get('initiatortext'),
'website' => get('website'),
'author' => Data::encode($kirby->user()->id(), 'yaml')
];
$rules = [
'title' => ['required'],
'text' => ['required'],
'place' => ['required'],
'state' => ['required'],
'website' => ['url'],
'initiatortext' => ['required', 'max' => 200]
];
$messages = [
'title' => 'Please enter your (link: #title text: title)',
'text' => 'Please enter a valid (link: #email text: email address)',
'place' => 'Bitte gib einen Ort ein.',
'state' => 'Bitte wähle ein Bundesland aus.',
'website' => 'Bitte gib eine URL ein.',
'initiatortext' => 'Bitte beschreibe die Mitwrikenden.'
];
// some of the data is invalid
if ($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
} else {
// Get all files from the request
$uploads = $kirby->request()->files()->get('file');
// authenticate as almighty
$kirby->impersonate('kirby');
// everything is ok, let's try to create a new registration
try {
// we store registrations as subpages of the current page
$project = page('projekte')->createChild([
'slug' => str::slug($data['title'] . '-' . $data['place']),
'template' => 'projekt',
'content' => $data
])->changeStatus('unlisted');
//uploading files right here
//they will be arranged in the new project
foreach ($uploads as $upload) {
try {
$name = $upload['name'];
$file = $project->createFile([
'source' => $upload['tmp_name'],
'filename' => $name,
'template' => 'upload',
'content' => [
'date' => date('Y-m-d h:m')
]
]);
}
catch (Exception $e) {
$alerts[$upload['name']] = $e->getMessage();
}
}
// encode the files to yml format and update the field
$fileArray = A::wrap($file->filename());
$page = $project->update([
'gallery' => Data::encode($fileArray, 'yaml')
]);
if ($project) {
// store referer and name in session
$kirby->session()->set([
'referer' => $page->uri(),
'regName' => esc($data['title'])
]);
go('projekte/danke');
}
} catch (Exception $e) {
$alert = ['Your registration failed: ' . $e->getMessage()];
}
}
}
// return data to template
return [
'alert' => $alert ?? null,
'data' => $data ?? false,
];
};