Forms in Kirby Blocks

Hi there,

I could use a little help… I´m trying to build a “contactform” block.

My problem is, that I get the error Block error: "Cannot use object of type Kirby\Cms\Block as array" in block type: "contactform". I think the error is thrown, because I try to access $data[‘somevalue’] in the template. I guess I have to pass $data to the block somehow.

As far as I know it is not possible to create controllers for blocks, right? Where to put the controller code?

Thanks :slight_smile:

Block is an object, not an array. What is $data?

<?php if($success): ?>
    <div class="alert success">
        <p><?= $success ?></p>
    </div>
<?php else: ?>
<?php if (isset($alert['error'])): ?>
    <div><?= $alert['error'] ?></div>
<?php endif ?>

<form method="post" action="<?= $page->url() ?>">
<div class="honeypot">
        <label for="website">Website <abbr title="required">*</abbr></label>
        <input type="website" id="website" name="website" tabindex="-1">
    </div>
    <div class="field">
        <label for="name">
            Name <abbr title="required">*</abbr>
        </label>
        <input type="text" id="name" name="name" value="<?= $data['name'] ?? '' ?>" required>
        <?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>
    </div>
    <div class="field">
        <label for="email">
            Email <abbr title="required">*</abbr>
        </label>
        <input type="email" id="email" name="email" value="<?= $data['email'] ?? '' ?>" required>
        <?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>
    </div>
    <div class="field">
        <label for="text">
            Text <abbr title="required">*</abbr>
        </label>
        <textarea id="text" name="text" required>
            <?= $data['text']?? '' ?>
        </textarea>
        <?= isset($alert['text']) ? '<span class="alert error">' . html($alert['text']) . '</span>' : '' ?>
    </div>
    <input type="submit" name="submit" value="Submit">
</form>
<?php endif ?>

This is my contactform.php (block template). The code was copied over from the cookbook example (https://getkirby.com/docs/cookbook/forms/basic-contact-form#the-contact-page)

$data is the kirby block object. Here’s the top part of a var_dump:
Bildschirmfoto 2021-01-04 um 10.39.16

Thank you!

As mentioned, Block is an object and cannot be accessed using array syntax… $data['text']

So it should be $block->text(). Since you are creating a new block, I’d use the $block variable, anyway.

@pixelijn: I´m not trying to access a field. I’m trying to access form-data passed from the controller.

Ah now I get it. The form example from the cookbook passes $data from the controller containing all the from-data. Inside of blocks $data referes to the current block instance.

So I changed $data to $fromdata and the error is gone.

But I’m still not sure where to put the controller code for my block as there are no block controllers, only page controllers, right?

Thank you for your help!

Exactly. But I guess the logic could go into the block snippet.

Very well, Ill try this then…

Thank you!

Hi - did you get this working? If you could share the snippet of your contact form block that would be awesome, I am currently implementing the same thing

Hi Mike,

yes is working… I pasted the controller-code from the example (see above) into page-contollers. The code is run only if a form is submitted:

if($kirby->request()->is('POST') && get('submit')) { ...