I am sending all the filled-in input forms to kirby and save each of them to a new text-file in a subpage.
I want to replace all the items in the $data
array, by looping over the $section
object from Kirby Structure, which is what I used to create the input forms (Numbering Kirby Modules and Kirby Builder structure field).
So iterating over the ID
s and name
s I built from the numeration of each element in the structure field, and put them in the array, if my understanding of how the input form works is correct (first time trying this).
<?php
return function($site, $pages, $page) {
$alert = null;
if(r::is('post') && get('register')) {
if(!empty(get('website'))) {
// lets tell the bot that everything is ok
go($page->url());
exit;
}
$data = array(
'firstname' => esc(get('firstname')),
'lastname' => esc(get('lastname')),
'company' => esc(get('company')),
'email' => esc(get('email')),
'message' => esc(get('message'))
);
$rules = array(
'firstname' => array('required'),
'lastname' => array('required'),
'email' => array('required', 'email'),
);
$messages = array(
'firstname' => 'Please enter a valid first name',
'lastname' => 'Please enter a valid last name',
'email' => 'Please enter a valid email address',
);
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
} else {
// everything is ok, let's try to create a new registration
try {
$newRegistration = $page->find('registrations')->children()->create(str::slug($data['lastname'] . '-' . $data['firstname'] . '-' . time()) , 'register', $data);
$success = 'Your registration was successful';
$data = array();
} catch(Exception $e) {
echo 'Your registration failed: ' . $e->getMessage();
}
}
}
return compact('alert', 'data', 'success');
};