🧩 Form Controller
This guide demonstrates how to create and process a custom form using the FormController in PageBlocks.
📄 1. Example Form Markup
html
<form pb-form action="/form/submit" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{csrf_token}">
<input type="hidden" name="honeypot" value="">
<input type="hidden" name="formname" value="Job">
<div class="modal-body">
<div class="mb-3">
<label for="job-name" class="col-form-label">Name:</label>
<input type="text" class="form-control" name="name" id="job-name">
<span class="invalid-feedback" data-error="name"></span>
</div>
<div class="mb-3">
<label for="job-email" class="col-form-label">E-mail:</label>
<input type="email" class="form-control" name="email" id="job-email">
<span class="invalid-feedback" data-error="email"></span>
</div>
<div class="mb-3">
<label for="job-file" class="col-form-label">Summary (PDF, DOC, DOCX):</label>
<input type="file" class="form-control" name="file" id="job-file">
<span class="invalid-feedback" data-error="file"></span>
</div>
<div class="mb-3">
<label for="job-message" class="col-form-label">Message:</label>
<textarea class="form-control" name="message" id="job-message"></textarea>
<span class="invalid-feedback" data-error="message"></span>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>🛣 2. Route Definition
Define the route in core/App/routes/web.php:
php
use PageBlocks\App\Http\Controllers\FormController;
Route::post('/form/submit', [FormController::class, 'handle']);⚙️ 3. Form Controller Logic
Located at core/App/Http/Controllers/FormController.php:
php
<?php
namespace PageBlocks\App\Http\Controllers;
use Boshnik\PageBlocks\Http\Request;
use Boshnik\PageBlocks\Support\Mail;
class FormController extends Controller
{
public function handle(Request $request)
{
// ⚠️ Validate the form data
$validated = $request->validate([
'honeypot' => 'empty|exclude', // Anti-spam: must be empty
'formname' => 'required|string',
'name' => 'required|string|max:255',
'email' => 'required|email',
'file' => 'required|file|mimes:pdf,doc,docx,jpg|min:100|max:2048',
'message' => 'nullable|string',
]);
// 📥 Store uploaded file in assets/images/files
$validated['file'] = $request->file('file')->store('assets/images/files');
// Add user IP address
$validated['ip'] = $request->ip();
// 💾 Save the form to a pbTableValue table
$save = query(\pbTableValue::class)->create([
'model_type' => 'pbMenu', // Could also be modDocument or modUser
'field_name' => 'forms',
'createdon' => time(),
'values' => json_encode($validated),
]);
if (!$save) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Failed to save form: ' . print_r($validated, true));
}
// 📬 Send email to manager
Mail::to('pageblocks@boshnik.com')
->subject('Resume Submission')
->view('chunks/email/manager', $validated)
->attach($validated['file'])
->send();
// 📤 Send confirmation to user
Mail::send($validated['email'], 'Your resume has been received', 'We will contact you shortly!');
// Or using helper:
// mail($validated['email'], 'Your resume has been received', 'We will contact you shortly!');
return response()->success('Success message');
}
}