vulcandigital/silverstripe-formpreserve

Easily preserve and clear user entered form data contents via session in the event of errors

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:silverstripe-vendormodule

1.0.0 2018-02-23 01:20 UTC

This package is auto-updated.

Last update: 2024-11-18 21:00:44 UTC


README

Easily preserve and clear user entered form data contents via session in the event of errors

Requirements

  • silverstripe/framework: ^4

Usage

class YourForm extends Form 
{
    private static $extensions = [
        FormPreserveExtension::class
    ];
    
    public function __construct(RequestHandler $controller = null, $name = self::DEFAULT_NAME, FieldList $fields = null, FieldList $actions = null, $validator = null)
    {
        /** @var ArrayData $preserved */
        $preserved = $this->retrievePreserved();

        $fields = FieldList::create([
            TextField::create('FirstName', 'First name:', $preserved->FirstName)->setAttribute('required', true),
            EmailField::create('Email', 'Your email:', $preserved->Email)->setAttribute('placeholder', 'Where do we send our response?')->setAttribute('required', true),
        ]);

        $actions = FieldList::create([
            FormAction::create('doSubmit', 'Send')->setUseButtonTag(true)->setButtonContent('<i class="fal fa-paper-plane"></i> Send')->addExtraClass('btn btn-primary')
        ]);

        parent::__construct($controller, $name, $fields, $actions, $validator);
    }
}

Your form submit action:

class ContactPageController extends \PageController 
{
   public function doSubmit($data, Form $form)
    {
        FormPreserve::preserve($form, $data);

        if (!isset($data['Email']) || !filter_var($data['Email'], FILTER_VALIDATE_EMAIL)) {
            return $this->redirectBack();
        }

        FormPreserve::clear($form);
        return $this->redirectBack();
    }
}