silverstripe-userforms/code/Form/UserForm.php

185 lines
4.3 KiB
PHP
Raw Normal View History

2015-07-24 04:37:48 +02:00
<?php
/**
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class UserForm extends Form
{
2015-07-24 04:37:48 +02:00
/**
* @param Controller $controller
* @param string $name
*/
2016-07-21 07:53:59 +02:00
public function __construct(Controller $controller, $name = 'Form')
{
2015-07-24 04:37:48 +02:00
$this->controller = $controller;
$this->setRedirectToFormOnValidationError(true);
parent::__construct(
$controller,
$name,
new FieldList(),
new FieldList()
2015-07-24 04:37:48 +02:00
);
$this->setFields($fields = $this->getFormFields());
$fields->setForm($this);
$this->setActions($actions = $this->getFormActions());
$actions->setForm($this);
$this->setValidator($this->getRequiredFields());
2016-07-21 07:53:59 +02:00
// This needs to be re-evaluated since fields have been assigned
$this->setupFormErrors();
// Number each page
$stepNumber = 1;
foreach($this->getSteps() as $step) {
$step->setStepNumber($stepNumber++);
}
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
if($controller->DisableCsrfSecurityToken) {
$this->disableSecurityToken();
}
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
$data = Session::get("FormInfo.{$this->FormName()}.data");
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
if(is_array($data)) {
$this->loadDataFrom($data);
}
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
$this->extend('updateForm');
}
2016-07-21 07:53:59 +02:00
public function setupFormErrors()
{
// Suppress setupFormErrors if fields haven't been bootstrapped
if ($this->fields && $this->fields->exists()) {
return parent::setupFormErrors();
}
return $this;
}
2015-08-06 01:01:21 +02:00
/**
* Used for partial caching in the template.
*
* @return string
*/
2016-07-21 07:53:59 +02:00
public function getLastEdited()
{
2015-08-06 01:01:21 +02:00
return $this->controller->LastEdited;
}
2016-07-21 07:53:59 +02:00
2015-08-10 05:18:50 +02:00
/**
* @return bool
2015-08-10 05:18:50 +02:00
*/
2016-07-21 07:53:59 +02:00
public function getDisplayErrorMessagesAtTop()
{
return (bool)$this->controller->DisplayErrorMessagesAtTop;
2015-08-10 05:18:50 +02:00
}
2015-08-06 01:01:21 +02:00
/**
* Return the fieldlist, filtered to only contain steps
*
* @return ArrayList
2015-08-06 01:01:21 +02:00
*/
2016-07-21 07:53:59 +02:00
public function getSteps()
{
return $this->Fields()->filterByCallback(function($field) {
return $field instanceof UserFormsStepField;
});
2015-08-06 01:01:21 +02:00
}
2015-07-24 04:37:48 +02:00
/**
* 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.
*
* This will be a list of top level composite steps
*
2015-07-24 04:37:48 +02:00
* @return FieldList
*/
2016-07-21 07:53:59 +02:00
public function getFormFields()
{
$fields = new UserFormsFieldList();
$target = $fields;
foreach ($this->controller->Fields() as $field) {
$target = $target->processNext($field);
2015-07-24 04:37:48 +02:00
}
2015-08-17 05:30:32 +02:00
$fields->clearEmptySteps();
2015-07-24 04:37:48 +02:00
$this->extend('updateFormFields', $fields);
2016-07-21 07:53:59 +02:00
$fields->setForm($this);
2015-07-24 04:37:48 +02:00
return $fields;
}
/**
2015-09-11 00:20:06 +02:00
* Generate the form actions for the UserDefinedForm. You
2015-07-24 04:37:48 +02:00
* can manipulate these by using {@link updateFormActions()} on
* a decorator.
*
* @todo Make form actions editable via their own field editor.
*
* @return FieldList
*/
2016-07-21 07:53:59 +02:00
public function getFormActions()
{
2015-07-24 04:37:48 +02:00
$submitText = ($this->controller->SubmitButtonText) ? $this->controller->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit');
$clearText = ($this->controller->ClearButtonText) ? $this->controller->ClearButtonText : _t('UserDefinedForm.CLEARBUTTON', 'Clear');
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
$actions = new FieldList(
new FormAction("process", $submitText)
);
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
if($this->controller->ShowClearButton) {
$actions->push(new ResetFormAction("clearForm", $clearText));
}
2016-07-21 07:53:59 +02:00
2015-07-24 04:37:48 +02:00
$this->extend('updateFormActions', $actions);
2016-07-21 07:53:59 +02:00
$actions->setForm($this);
2015-07-24 04:37:48 +02:00
return $actions;
}
/**
* Get the required form fields for this form.
*
* @return RequiredFields
*/
2016-07-21 07:53:59 +02:00
public function getRequiredFields()
{
2015-07-24 04:37:48 +02:00
// Generate required field validator
$requiredNames = $this
2016-07-21 07:53:59 +02:00
->getController()
2015-07-24 04:37:48 +02:00
->Fields()
->filter('Required', true)
->column('Name');
$required = new RequiredFields($requiredNames);
$this->extend('updateRequiredFields', $required);
2016-07-21 07:53:59 +02:00
$required->setForm($this);
2015-07-24 04:37:48 +02:00
return $required;
}
2015-08-11 01:40:37 +02:00
/**
* Override some we can add UserForm specific attributes to the form.
*
* @return array
*/
2016-07-21 07:53:59 +02:00
public function getAttributes()
{
2015-08-11 01:40:37 +02:00
$attrs = parent::getAttributes();
2016-07-21 07:53:59 +02:00
2015-08-11 01:40:37 +02:00
$attrs['class'] = $attrs['class'] . ' userform';
$attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation;
$attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop;
2016-07-21 07:53:59 +02:00
2015-08-11 01:40:37 +02:00
return $attrs;
}
/**
* @return string
*/
public function getButtonText() {
return $this->config()->get('button_text');
2016-07-21 07:53:59 +02:00
}
2015-07-24 04:37:48 +02:00
}