Initial multi-page form

This commit is contained in:
David Craig 2015-08-04 17:12:17 +12:00 committed by Damian Mooyman
parent 47c3df9c64
commit d83a450307
4 changed files with 145 additions and 2 deletions

View File

@ -34,6 +34,8 @@ class UserFormFieldEditorExtension extends DataExtension {
public function getFieldEditorGrid() {
$fields = $this->owner->Fields();
$this->createInitialFormStep();
$editableColumns = new GridFieldEditableColumns();
$editableColumns->setDisplayFields(array(
'ClassName' => function($record, $column, $grid) {
@ -66,6 +68,30 @@ class UserFormFieldEditorExtension extends DataExtension {
return $fieldEditor;
}
/**
* A UserForm must have at least one step.
* If no steps exist, create an initial step, and put all fields inside it.
*
* @return void
*/
public function createInitialFormStep() {
// If there's already an initial step, do nothing.
if ($this->owner->Fields()->filter('ClassName', 'EditableFormStep')->Count()) {
return;
}
$step = EditableFormStep::create();
$step->ParentID = $this->owner->ID;
$step->write();
// Assign each field to the initial step.
foreach ($this->owner->Fields()->exclude('ID', $step->ID) as $field) {
$field->StepID = $step->ID;
$field->write();
}
}
/**
* @return array
*/

View File

@ -35,17 +35,41 @@ class UserForm extends Form {
$this->extend('updateForm');
}
/**
* Get the form steps.
*
* @return ArrayList
*/
public function getFormSteps() {
$steps = new ArrayList();
foreach ($this->controller->Fields()->filter('ClassName', 'EditableFormStep') as $step) {
$steps->push(array(
'Title' => $step->Title,
'Fields' => $this->getFormFields($step)
));
}
return $steps;
}
/**
* Get the form fields for the form on this page. Can modify this FieldSet
* by using {@link updateFormFields()} on an {@link Extension} subclass which
* is applied to this controller.
*
* @param EditableFormStep $parent
*
* @return FieldList
*/
public function getFormFields() {
public function getFormFields($parent = null) {
if(!$parent) {
$parent = $this->controller;
}
$fields = new FieldList();
foreach($this->controller->Fields() as $editableField) {
foreach($parent->Fields() as $editableField) {
// get the raw form field from the editable version
$field = $editableField->getFormField();

View File

@ -0,0 +1,57 @@
<?php
/**
* A step in multi-page user form
*
* @package userforms
*/
class EditableFormStep extends EditableFormField {
/**
* @config
* @var string
*/
private static $singular_name = 'Step';
/**
* @config
* @var string
*/
private static $plural_name = 'Steps';
/**
* @config
* @var array
*/
private static $has_many = array(
'Fields' => 'EditableFormField'
);
/**
* @return FieldList
*/
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('MergeField');
$fields->removeByName('StepID');
$fields->removeByName('Default');
$fields->removeByName('Validation');
$fields->removeByName('CustomRules');
return $fields;
}
/**
* @return FormField
*/
public function getFormField() {
return false;
}
/**
* @return boolean
*/
public function showInReports() {
return false;
}
}

36
templates/UserForm.ss Normal file
View File

@ -0,0 +1,36 @@
<% if $IncludeFormTag %>
<form $AttributesHTML>
<% end_if %>
<% if $Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>
<fieldset>
<% if $Legend %><legend>$Legend</legend><% end_if %>
<% loop $FormSteps %>
<fieldset>
<h2>$Title</h2>
<% loop $Fields %>
$FieldHolder
<% end_loop %>
</fieldset>
<% end_loop %>
<div class="clear"><!-- --></div>
</fieldset>
<% if $Actions %>
<div class="Actions">
<% loop $Actions %>
$Field
<% end_loop %>
</div>
<% end_if %>
<% if $IncludeFormTag %>
</form>
<% end_if %>