Hi
I’m tried to followed steps from tutorial Creating pages from frontend but I’m getting an error when I send the form Call to a member function children() on boolean and this is underline :
$newRegistration = $page->find('registrations')->children()->create(str::slug($data['lastname'] . '-' . $data['firstname'] . '-' . time()) , 'register', $data);
My controller, template, and snippets are exactly like the article said. But in case, I post them here :
Controller event.php
<?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');
};
Template event.php
<?php snippet('header') ?>
<main class="main" role="main">
<div class="text">
<?php
// if the form was successfully submitted and the page created, show the success message
if(isset($success)): ?>
<div class="message">
<?php echo $success; ?>
</div>
<?php endif ?>
<?php
// if the form input does not validate, show a list of alerts
if($alert): ?>
<div class="alert">
<ul>
<?php foreach($alert as $message): ?>
<li><?php echo html($message) ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
<?php if(!isset($success)) {
// if the $success variable is not set, show the form (i.e. when the page is first loaded or the form submission was not successful)
snippet('registration-form', compact('data'));
}
?>
</div>
</main>
<?php snippet('footer') ?>
