jesse_orange_newable's avatar

Designing a form middleware

Hi all,

I have a WordPress site with a bunch of forms made using the HubSpot form builder that are then included in the page via an iframe, all easy stuff so far.

A new requirement has come up that requires using the Companies House API to do a lookup and basically say we don't want you as you're not a Limited company or you needed to have been trading for X years on a per form basis.

I was wondering if I could just make a tiny Laravel app that spits out forms, kind of like Filament, so instead of using the HubSpot builder, I have a link that says click here to apply and it goes to https://myapp.something/name-of-form.

This way I can handle the data in any which way I want, the only issue is that this would remove marketer's ability to build forms. Currently there are 28 forms.

0 likes
1 reply
LaryAI's avatar
Level 58

To address your requirement of integrating Companies House API checks into your form submissions while maintaining the flexibility for marketers to build and manage forms, you can indeed create a small Laravel application that serves as a middleware for your forms. This application will handle the API integration and form validation based on your business rules (e.g., company type and trading duration), and then pass the data back to your main WordPress site or directly to HubSpot.

Here’s a step-by-step approach to building this solution:

Step 1: Set Up Your Laravel Project

First, create a new Laravel project which will act as the middleware for handling the forms.

composer create-project --prefer-dist laravel/laravel laravelFormMiddleware

Step 2: Create a Form Model and Controller

You'll need a model to handle your form data and a controller to manage the business logic.

php artisan make:model Form -m
php artisan make:controller FormController

In the migration file that is generated (database/migrations/xxxx_xx_xx_xxxxxx_create_forms_table.php), add the necessary fields that you expect from the form.

Schema::create('forms', function (Blueprint $table) {
    $table->id();
    $table->string('company_name');
    $table->string('registration_number');
    // Add other fields as necessary
    $table->timestamps();
});

Run the migration:

php artisan migrate

Step 3: Implement Companies House API Integration

In your FormController, add the logic to perform the API call to Companies House to validate the company details.

use Illuminate\Http\Request;
use GuzzleHttp\Client;

class FormController extends Controller
{
    public function checkCompany(Request $request)
    {
        $client = new Client();
        $response = $client->request('GET', 'https://api.companieshouse.gov.uk/company/'.$request->registration_number, [
            'headers' => [
                'Authorization' => 'Basic '.base64_encode('your_api_key')
            ]
        ]);

        $data = json_decode($response->getBody()->getContents(), true);

        // Implement your business logic to check company type and trading years
        if ($data['type'] != 'limited' || $data['trading_years'] < required_years) {
            return response()->json(['error' => 'Invalid company type or trading duration'], 422);
        }

        // Save form data or pass it to another service
        // Form::create($request->all());

        return response()->json(['success' => 'Form submitted successfully']);
    }
}

Step 4: Route Setup

Define a route in routes/web.php that will handle the form submission.

Route::post('/submit-form', [FormController::class, 'checkCompany']);

Step 5: Linking with WordPress

On your WordPress site, you can modify the form action URLs to point to the Laravel application’s endpoint (https://myapp.something/submit-form). This way, when the form is submitted, it first goes through the Laravel middleware for validation.

Step 6: Handling CORS

If your Laravel app and WordPress site are on different domains, you'll need to handle Cross-Origin Resource Sharing (CORS) in your Laravel application.

You can do this by modifying the cors.php config file in Laravel.

'paths' => ['api/*', 'submit-form'],
'allowed_origins' => ['https://yourwordpresssite.com'],

Conclusion

This setup allows you to intercept form submissions, perform necessary validations using the Companies House API, and then proceed with the form data as required. It integrates smoothly without taking away the ability of marketers to manage form creation and design, as they can still use the familiar tools provided by HubSpot or WordPress.

Please or to participate in this conversation.