I’m trying to use Uniform to allow site visitors to sign up to my clients newsletter. The newsletter is managed through Hubspot and I managed to send data to the provided Webhook.
It actually works but does never ever returns the error response from Hubspot. FOr example if a user (an email) already exists in the system…
Tried to output non-success quite verbose for debugging
// site/snippet/newsletter-form.php
...
<?php if ($form->success()): ?>
Thank you for your message. We will get back to you soon!
<?php else: ?>
No-success:
<?php snippet('uniform/errors', ['form' => $form]) ?>
<?php endif; ?>
<?php if ($form->error()): ?>
Error Errors:
<?php foreach($form->error() as $err): ?>
<?= $err ?>
<?php endforeach ?>
<?php endif; ?>
Do I need to manually parse the response to their API and return it to the frontend. If so – how?
This is what my controller looks like:
// site/controllers/default.php
use Uniform\Form;
return function ($kirby)
{
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
]);
if ($kirby->request()->is('POST')) {
// hubspot want data to be in properties array inside data -> do that to the data
$properties = $form->data('', '', true);
$form
->webhookAction([
'url' => 'https://api.hubapi.com/crm/v3/objects/contacts',
'json' => true,
'params' => [
'method' => 'POST',
'data' => [
'properties' => $properties
],
'headers' => ["Authorization: Bearer $access_token"],
],
]);
}
return compact('form');
};
As far as I can see from a quick look into the source code, this Webhook action never returns anything, unless the remote request itself fails. So if the request is successful and the error only in the response, nothing will ever be returned.
You’re right. For now I duplicated the code into a Custom Action (see Actions - Kirby Uniform) and raising an error on HTTP request codes above 201 (which is “Created”).