Uniform Plugin - store form entry in DB

Cookies make me fat, but it doesnt stop me eating cookies.

If it helps someone else, heres my full uniform-actions.php

<?php

namespace Uniform\Actions;
use db;

class StoreAction extends Action
{
    public function perform()
    {
        try {
            // var_dump($this->form->data());
            $data = $this->form->data();
            db::insert('contact', array(
              'name'    => $data['name'],
              'email'   => $data['email'],
              'message' => $data['message'],
              'date'    => 'NOW()',
            ));
        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}

And the controller…

<?php

use Uniform\Form;

return function ($site, $pages, $page) {

    $form = new Form([

        'name' => [
            'rules' => ['required'],
            'message' => 'Please enter your name',
        ],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'message' => [
            'rules' => ['required'],
            'message' => 'Pease make some comments if you have any!',
        ],
    ]);

    if (r::is('POST')) {
        $form->emailAction([
          'to'      => 'you@example.com',
          'from'  => 'noreply@example.com',
          'subject' => $site->title()->html() . ' - contact form'
        ])->StoreAction();
    }



    return compact('form');
};
1 Like