2007-09-16 03:52:35 +02:00
|
|
|
<?php
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Base class for all forms.
|
2012-03-24 04:38:57 +01:00
|
|
|
* The form class is an extensible base for all forms on a SilverStripe application. It can be used
|
2007-07-19 12:40:28 +02:00
|
|
|
* either by extending it, and creating processor methods on the subclass, or by creating instances
|
|
|
|
* of form whose actions are handled by the parent controller.
|
2007-09-16 03:52:35 +02:00
|
|
|
*
|
|
|
|
* In either case, if you want to get a form to do anything, it must be inextricably tied to a
|
2007-07-19 12:40:28 +02:00
|
|
|
* controller. The constructor is passed a controller and a method on that controller. This method
|
|
|
|
* should return the form object, and it shouldn't require any arguments. Parameters, if necessary,
|
|
|
|
* can be passed using the URL or get variables. These restrictions are in place so that we can
|
|
|
|
* recreate the form object upon form submission, without the use of a session, which would be too
|
|
|
|
* resource-intensive.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-01 20:32:10 +02:00
|
|
|
* You will need to create at least one method for processing the submission (through {@link FormAction}).
|
|
|
|
* This method will be passed two parameters: the raw request data, and the form object.
|
|
|
|
* Usually you want to save data into a {@link DataObject} by using {@link saveInto()}.
|
|
|
|
* If you want to process the submitted data in any way, please use {@link getData()} rather than
|
|
|
|
* the raw request data.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-07-07 00:06:42 +02:00
|
|
|
* <h2>Validation</h2>
|
2009-01-08 00:00:54 +01:00
|
|
|
* Each form needs some form of {@link Validator} to trigger the {@link FormField->validate()} methods for each field.
|
2012-09-26 23:34:00 +02:00
|
|
|
* You can't disable validator for security reasons, because crucial behaviour like extension checks for file uploads
|
|
|
|
* depend on it.
|
2009-01-08 00:00:54 +01:00
|
|
|
* The default validator is an instance of {@link RequiredFields}.
|
|
|
|
* If you want to enforce serverside-validation to be ignored for a specific {@link FormField},
|
|
|
|
* you need to subclass it.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2009-07-07 00:06:42 +02:00
|
|
|
* <h2>URL Handling</h2>
|
|
|
|
* The form class extends {@link RequestHandler}, which means it can
|
|
|
|
* be accessed directly through a URL. This can be handy for refreshing
|
|
|
|
* a form by ajax, or even just displaying a single form field.
|
|
|
|
* You can find out the base URL for your form by looking at the
|
|
|
|
* <form action="..."> value. For example, the edit form in the CMS would be located at
|
|
|
|
* "admin/EditForm". This URL will render the form without its surrounding
|
2014-08-15 08:53:05 +02:00
|
|
|
* template when called through GET instead of POST.
|
|
|
|
*
|
2009-07-07 00:06:42 +02:00
|
|
|
* By appending to this URL, you can render invidual form elements
|
|
|
|
* through the {@link FormField->FieldHolder()} method.
|
|
|
|
* For example, the "URLSegment" field in a standard CMS form would be
|
|
|
|
* accessible through "admin/EditForm/field/URLSegment/FieldHolder".
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage core
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-10-30 23:03:21 +01:00
|
|
|
class Form extends RequestHandler {
|
2011-12-26 08:36:24 +01:00
|
|
|
|
|
|
|
const ENC_TYPE_URLENCODED = 'application/x-www-form-urlencoded';
|
|
|
|
const ENC_TYPE_MULTIPART = 'multipart/form-data';
|
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
/**
|
2008-10-07 18:47:01 +02:00
|
|
|
* @var boolean $includeFormTag Accessed by Form.ss; modified by {@link formHtmlContent()}.
|
2008-03-16 23:15:04 +01:00
|
|
|
* A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously
|
|
|
|
*/
|
|
|
|
public $IncludeFormTag = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-11 21:25:22 +01:00
|
|
|
protected $fields;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-11 21:25:22 +01:00
|
|
|
protected $actions;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
/**
|
|
|
|
* @var Controller
|
|
|
|
*/
|
2008-02-11 21:25:22 +01:00
|
|
|
protected $controller;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-11 21:25:22 +01:00
|
|
|
protected $name;
|
|
|
|
|
|
|
|
protected $validator;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-09 02:27:16 +02:00
|
|
|
protected $formMethod = "POST";
|
2013-03-25 06:16:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $strictFormMethodCheck = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected static $current_action;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-07 18:47:01 +02:00
|
|
|
/**
|
2009-05-20 07:07:06 +02:00
|
|
|
* @var Dataobject $record Populated by {@link loadDataFrom()}.
|
2008-10-07 18:47:01 +02:00
|
|
|
*/
|
2008-03-16 23:15:04 +01:00
|
|
|
protected $record;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Keeps track of whether this form has a default action or not.
|
|
|
|
* Set to false by $this->disableDefaultAction();
|
|
|
|
*/
|
|
|
|
protected $hasDefaultAction = true;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2008-02-07 03:58:23 +01:00
|
|
|
/**
|
2008-03-16 23:15:04 +01:00
|
|
|
* Target attribute of form-tag.
|
|
|
|
* Useful to open a new window upon
|
|
|
|
* form submission.
|
|
|
|
*
|
|
|
|
* @var string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-03-16 23:15:04 +01:00
|
|
|
protected $target;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Legend value, to be inserted into the
|
2008-12-04 23:38:32 +01:00
|
|
|
* <legend> element before the <fieldset>
|
|
|
|
* in Form.ss template.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $legend;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-12 02:43:56 +01:00
|
|
|
/**
|
|
|
|
* The SS template to render this form HTML into.
|
|
|
|
* Default is "Form", but this can be changed to
|
|
|
|
* another template for customisation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-12 02:43:56 +01:00
|
|
|
* @see Form->setTemplate()
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $template;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
protected $buttonClickedFunc;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
protected $message;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
protected $messageType;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Should we redirect the user back down to the
|
2009-05-01 05:49:34 +02:00
|
|
|
* the form on validation errors rather then just the page
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-01 05:49:34 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $redirectToFormOnValidationError = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
protected $security = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
/**
|
|
|
|
* @var SecurityToken
|
|
|
|
*/
|
|
|
|
protected $securityToken = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-06 03:16:16 +01:00
|
|
|
/**
|
2011-08-22 06:44:41 +02:00
|
|
|
* @var array $extraClasses List of additional CSS classes for the form tag.
|
2009-01-06 03:16:16 +01:00
|
|
|
*/
|
|
|
|
protected $extraClasses = array();
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2011-12-26 08:36:24 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $encType;
|
|
|
|
|
2011-12-22 18:05:20 +01:00
|
|
|
/**
|
|
|
|
* @var array Any custom form attributes set through {@link setAttributes()}.
|
|
|
|
* Some attributes are calculated on the fly, so please use {@link getAttributes()} to access them.
|
|
|
|
*/
|
|
|
|
protected $attributes = array();
|
|
|
|
|
2013-06-25 16:33:00 +02:00
|
|
|
private static $allowed_actions = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'handleField',
|
2013-06-25 16:33:00 +02:00
|
|
|
'httpSubmission',
|
|
|
|
'forTemplate',
|
|
|
|
);
|
2013-06-20 11:40:55 +02:00
|
|
|
|
2013-05-26 01:09:03 +02:00
|
|
|
/**
|
|
|
|
* @var FormTemplateHelper
|
|
|
|
*/
|
|
|
|
private $templateHelper = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
private $htmlID = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
private $formActionPath = false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new form, with the given fields an action buttons.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-01-08 00:00:54 +01:00
|
|
|
* @param Controller $controller The parent controller, necessary to create the appropriate form action tag.
|
|
|
|
* @param String $name The method on the controller that will return this form object.
|
2011-10-28 03:37:27 +02:00
|
|
|
* @param FieldList $fields All of the fields in the form - a {@link FieldList} of {@link FormField} objects.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param FieldList $actions All of the action buttons in the form - a {@link FieldLis} of
|
|
|
|
* {@link FormAction} objects
|
2009-01-08 00:00:54 +01:00
|
|
|
* @param Validator $validator Override the default validator instance (Default: {@link RequiredFields})
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function __construct($controller, $name, FieldList $fields, FieldList $actions, $validator = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$fields instanceof FieldList) {
|
|
|
|
throw new InvalidArgumentException('$fields must be a valid FieldList instance');
|
|
|
|
}
|
|
|
|
if(!$actions instanceof FieldList) {
|
2013-02-18 16:01:48 +01:00
|
|
|
throw new InvalidArgumentException('$actions must be a valid FieldList instance');
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
|
|
|
if($validator && !$validator instanceof Validator) {
|
2013-04-24 18:50:40 +02:00
|
|
|
throw new InvalidArgumentException('$validator must be a Validator instance');
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-04-12 23:19:38 +02:00
|
|
|
$fields->setForm($this);
|
|
|
|
$actions->setForm($this);
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->fields = $fields;
|
|
|
|
$this->actions = $actions;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->name = $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 04:51:33 +02:00
|
|
|
if(!$this->controller) user_error("$this->class form created without a controller", E_USER_ERROR);
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Form validation
|
2009-01-08 00:00:54 +01:00
|
|
|
$this->validator = ($validator) ? $validator : new RequiredFields();
|
|
|
|
$this->validator->setForm($this);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-16 03:52:35 +02:00
|
|
|
// Form error controls
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->setupFormErrors();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:42 +02:00
|
|
|
// Check if CSRF protection is enabled, either on the parent controller or from the default setting. Note that
|
|
|
|
// method_exists() is used as some controllers (e.g. GroupTest) do not always extend from Object.
|
2012-09-26 23:34:00 +02:00
|
|
|
if(method_exists($controller, 'securityTokenEnabled') || (method_exists($controller, 'hasMethod')
|
|
|
|
&& $controller->hasMethod('securityTokenEnabled'))) {
|
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
$securityEnabled = $controller->securityTokenEnabled();
|
2009-06-27 10:48:42 +02:00
|
|
|
} else {
|
2010-12-05 09:22:57 +01:00
|
|
|
$securityEnabled = SecurityToken::is_enabled();
|
2009-06-27 10:48:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
$this->securityToken = ($securityEnabled) ? new SecurityToken() : new NullSecurityToken();
|
2009-01-10 12:35:50 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2009-01-10 12:35:50 +01:00
|
|
|
'field/$FieldName!' => 'handleField',
|
|
|
|
'POST ' => 'httpSubmission',
|
|
|
|
'GET ' => 'httpSubmission',
|
2012-03-18 21:26:20 +01:00
|
|
|
'HEAD ' => 'httpSubmission',
|
2009-01-10 12:35:50 +01:00
|
|
|
);
|
2013-06-25 16:33:00 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
/**
|
|
|
|
* Set up current form errors in session to
|
|
|
|
* the current form if appropriate.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setupFormErrors() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$errorInfo = Session::get("FormInfo.{$this->FormName()}");
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
if(isset($errorInfo['errors']) && is_array($errorInfo['errors'])) {
|
|
|
|
foreach($errorInfo['errors'] as $error) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$field = $this->fields->dataFieldByName($error['fieldName']);
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
if(!$field) {
|
|
|
|
$errorInfo['message'] = $error['message'];
|
2007-07-19 12:40:28 +02:00
|
|
|
$errorInfo['type'] = $error['messageType'];
|
|
|
|
} else {
|
2009-01-10 12:35:50 +01:00
|
|
|
$field->setError($error['message'], $error['messageType']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load data in from previous submission upon error
|
2009-01-10 12:35:50 +01:00
|
|
|
if(isset($errorInfo['data'])) $this->loadDataFrom($errorInfo['data']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($errorInfo['message']) && isset($errorInfo['type'])) {
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->setMessage($errorInfo['message'], $errorInfo['type']);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-09-18 13:55:30 +02:00
|
|
|
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
2008-10-01 20:32:10 +02:00
|
|
|
* Handle a form submission. GET and POST requests behave identically.
|
|
|
|
* Populates the form with {@link loadDataFrom()}, calls {@link validate()},
|
|
|
|
* and only triggers the requested form action/method
|
|
|
|
* if the form is valid.
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function httpSubmission($request) {
|
2013-03-25 06:16:11 +01:00
|
|
|
// Strict method check
|
|
|
|
if($this->strictFormMethodCheck) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-25 06:16:11 +01:00
|
|
|
// Throws an error if the method is bad...
|
2013-05-09 02:27:16 +02:00
|
|
|
if($this->formMethod != $request->httpMethod()) {
|
2013-03-25 06:16:11 +01:00
|
|
|
$response = Controller::curr()->getResponse();
|
|
|
|
$response->addHeader('Allow', $this->formMethod);
|
|
|
|
$this->httpError(405, _t("Form.BAD_METHOD", "This form requires a ".$this->formMethod." submission"));
|
|
|
|
}
|
|
|
|
|
2013-05-09 02:27:16 +02:00
|
|
|
// ...and only uses the variables corresponding to that method type
|
|
|
|
$vars = $this->formMethod == 'GET' ? $request->getVars() : $request->postVars();
|
2013-03-25 06:16:11 +01:00
|
|
|
} else {
|
|
|
|
$vars = $request->requestVars();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Populate the form
|
|
|
|
$this->loadDataFrom($vars, true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Protection against CSRF attacks
|
2010-12-05 09:22:57 +01:00
|
|
|
$token = $this->getSecurityToken();
|
2014-02-08 03:13:39 +01:00
|
|
|
if( ! $token->checkRequest($request)) {
|
2014-10-06 05:01:33 +02:00
|
|
|
$securityID = $token->getName();
|
|
|
|
if (empty($vars[$securityID])) {
|
2014-02-08 03:13:39 +01:00
|
|
|
$this->httpError(400, _t("Form.CSRF_FAILED_MESSAGE",
|
2014-10-06 05:01:33 +02:00
|
|
|
"There seems to have been a technical problem. Please click the back button, ".
|
|
|
|
"refresh your browser, and try again."
|
|
|
|
));
|
2014-02-08 03:13:39 +01:00
|
|
|
} else {
|
2014-10-06 05:01:33 +02:00
|
|
|
// Clear invalid token on refresh
|
|
|
|
$data = $this->getData();
|
|
|
|
unset($data[$securityID]);
|
|
|
|
Session::set("FormInfo.{$this->FormName()}.data", $data);
|
2014-02-08 03:13:39 +01:00
|
|
|
Session::set("FormInfo.{$this->FormName()}.errors", array());
|
|
|
|
$this->sessionMessage(
|
|
|
|
_t("Form.CSRF_EXPIRED_MESSAGE", "Your session has expired. Please re-submit the form."),
|
|
|
|
"warning"
|
|
|
|
);
|
|
|
|
return $this->controller->redirectBack();
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Determine the action button clicked
|
|
|
|
$funcName = null;
|
|
|
|
foreach($vars as $paramName => $paramVal) {
|
|
|
|
if(substr($paramName,0,7) == 'action_') {
|
|
|
|
// Break off querystring arguments included in the action
|
|
|
|
if(strpos($paramName,'?') !== false) {
|
|
|
|
list($paramName, $paramVars) = explode('?', $paramName, 2);
|
|
|
|
$newRequestParams = array();
|
|
|
|
parse_str($paramVars, $newRequestParams);
|
|
|
|
$vars = array_merge((array)$vars, (array)$newRequestParams);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// Cleanup action_, _x and _y from image fields
|
|
|
|
$funcName = preg_replace(array('/^action_/','/_x$|_y$/'),'',$paramName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// If the action wasnt' set, choose the default on the form.
|
|
|
|
if(!isset($funcName) && $defaultAction = $this->defaultAction()){
|
|
|
|
$funcName = $defaultAction->actionName();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
if(isset($funcName)) {
|
2013-05-24 10:25:36 +02:00
|
|
|
Form::set_current_action($funcName);
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->setButtonClicked($funcName);
|
|
|
|
}
|
2013-06-20 11:40:55 +02:00
|
|
|
|
2010-12-20 01:00:38 +01:00
|
|
|
// Permission checks (first on controller, then falling back to form)
|
|
|
|
if(
|
|
|
|
// Ensure that the action is actually a button or method on the form,
|
|
|
|
// and not just a method on the controller.
|
|
|
|
$this->controller->hasMethod($funcName)
|
|
|
|
&& !$this->controller->checkAccessAction($funcName)
|
|
|
|
// If a button exists, allow it on the controller
|
2013-02-18 15:30:36 +01:00
|
|
|
&& !$this->actions->dataFieldByName('action_' . $funcName)
|
2010-12-20 01:00:38 +01:00
|
|
|
) {
|
|
|
|
return $this->httpError(
|
2014-08-15 08:53:05 +02:00
|
|
|
403,
|
2010-12-20 01:00:38 +01:00
|
|
|
sprintf('Action "%s" not allowed on controller (Class: %s)', $funcName, get_class($this->controller))
|
|
|
|
);
|
|
|
|
} elseif(
|
|
|
|
$this->hasMethod($funcName)
|
|
|
|
&& !$this->checkAccessAction($funcName)
|
|
|
|
// No checks for button existence or $allowed_actions is performed -
|
|
|
|
// all form methods are callable (e.g. the legacy "callfieldmethod()")
|
|
|
|
) {
|
|
|
|
return $this->httpError(
|
2014-08-15 08:53:05 +02:00
|
|
|
403,
|
2012-05-24 00:33:40 +02:00
|
|
|
sprintf('Action "%s" not allowed on form (Name: "%s")', $funcName, $this->name)
|
2010-12-20 01:00:38 +01:00
|
|
|
);
|
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
// TODO : Once we switch to a stricter policy regarding allowed_actions (meaning actions must be set
|
|
|
|
// explicitly in allowed_actions in order to run)
|
2011-12-06 01:56:24 +01:00
|
|
|
// Uncomment the following for checking security against running actions on form fields
|
|
|
|
/* else {
|
|
|
|
// Try to find a field that has the action, and allows it
|
|
|
|
$fieldsHaveMethod = false;
|
|
|
|
foreach ($this->Fields() as $field){
|
|
|
|
if ($field->hasMethod($funcName) && $field->checkAccessAction($funcName)) {
|
|
|
|
$fieldsHaveMethod = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$fieldsHaveMethod) {
|
|
|
|
return $this->httpError(
|
2014-08-15 08:53:05 +02:00
|
|
|
403,
|
2011-12-06 01:56:24 +01:00
|
|
|
sprintf('Action "%s" not allowed on any fields of form (Name: "%s")', $funcName, $this->Name())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 10:22:30 +02:00
|
|
|
// Validate the form
|
|
|
|
if(!$this->validate()) {
|
2013-05-10 15:00:13 +02:00
|
|
|
return $this->getValidationErrorResponse();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 15:00:13 +02:00
|
|
|
// First, try a handler method on the controller (has been checked for allowed_actions above already)
|
|
|
|
if($this->controller->hasMethod($funcName)) {
|
|
|
|
return $this->controller->$funcName($vars, $this, $request);
|
|
|
|
// Otherwise, try a handler method on the form object.
|
|
|
|
} elseif($this->hasMethod($funcName)) {
|
|
|
|
return $this->$funcName($vars, $this, $request);
|
|
|
|
} elseif($field = $this->checkFieldsForAction($this->Fields(), $funcName)) {
|
|
|
|
return $field->$funcName($vars, $this, $request);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 15:00:13 +02:00
|
|
|
return $this->httpError(404);
|
|
|
|
}
|
|
|
|
|
2013-06-20 11:40:55 +02:00
|
|
|
public function checkAccessAction($action) {
|
|
|
|
return (
|
|
|
|
parent::checkAccessAction($action)
|
|
|
|
// Always allow actions which map to buttons. See httpSubmission() for further access checks.
|
|
|
|
|| $this->actions->dataFieldByName('action_' . $action)
|
|
|
|
// Always allow actions on fields
|
|
|
|
|| (
|
|
|
|
$field = $this->checkFieldsForAction($this->Fields(), $action)
|
|
|
|
&& $field->checkAccessAction($action)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:00:13 +02:00
|
|
|
/**
|
|
|
|
* Returns the appropriate response up the controller chain
|
|
|
|
* if {@link validate()} fails (which is checked prior to executing any form actions).
|
|
|
|
* By default, returns different views for ajax/non-ajax request, and
|
|
|
|
* handles 'appliction/json' requests with a JSON object containing the error messages.
|
|
|
|
* Behaviour can be influenced by setting {@link $redirectToFormOnValidationError}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-10 15:00:13 +02:00
|
|
|
* @return SS_HTTPResponse|string
|
|
|
|
*/
|
|
|
|
protected function getValidationErrorResponse() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if($request->isAjax()) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// Special case for legacy Validator.js implementation
|
2013-05-10 15:00:13 +02:00
|
|
|
// (assumes eval'ed javascript collected through FormResponse)
|
2012-03-09 00:12:33 +01:00
|
|
|
$acceptType = $request->getHeader('Accept');
|
|
|
|
if(strpos($acceptType, 'application/json') !== FALSE) {
|
|
|
|
// Send validation errors back as JSON with a flag at the start
|
|
|
|
$response = new SS_HTTPResponse(Convert::array2json($this->validator->getErrors()));
|
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
2009-06-18 10:22:30 +02:00
|
|
|
} else {
|
2012-03-09 00:12:33 +01:00
|
|
|
$this->setupFormErrors();
|
|
|
|
// Send the newly rendered form tag as HTML
|
|
|
|
$response = new SS_HTTPResponse($this->forTemplate());
|
|
|
|
$response->addHeader('Content-Type', 'text/html');
|
2009-06-18 10:22:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 00:12:33 +01:00
|
|
|
return $response;
|
2009-06-18 10:22:30 +02:00
|
|
|
} else {
|
|
|
|
if($this->getRedirectToFormOnValidationError()) {
|
|
|
|
if($pageURL = $request->getHeader('Referer')) {
|
2010-10-15 01:51:55 +02:00
|
|
|
if(Director::is_site_url($pageURL)) {
|
|
|
|
// Remove existing pragmas
|
|
|
|
$pageURL = preg_replace('/(#.*)/', '', $pageURL);
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirect($pageURL . '#' . $this->FormName());
|
2010-10-15 01:51:55 +02:00
|
|
|
}
|
2009-06-18 10:22:30 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirectBack();
|
2009-06-18 10:22:30 +02:00
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 06:21:14 +01:00
|
|
|
/**
|
|
|
|
* Fields can have action to, let's check if anyone of the responds to $funcname them
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-09 06:21:14 +01:00
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
protected function checkFieldsForAction($fields, $funcName) {
|
|
|
|
foreach($fields as $field){
|
|
|
|
if(method_exists($field, 'FieldList')) {
|
|
|
|
if($field = $this->checkFieldsForAction($field->FieldList(), $funcName)) {
|
|
|
|
return $field;
|
|
|
|
}
|
2013-06-20 11:40:55 +02:00
|
|
|
} elseif ($field->hasMethod($funcName) && $field->checkAccessAction($funcName)) {
|
2012-02-08 23:46:33 +01:00
|
|
|
return $field;
|
2012-01-09 06:21:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
2009-07-07 00:06:42 +02:00
|
|
|
* Handle a field request.
|
|
|
|
* Uses {@link Form->dataFieldByName()} to find a matching field,
|
2011-10-28 03:37:27 +02:00
|
|
|
* and falls back to {@link FieldList->fieldByName()} to look
|
2009-07-07 00:06:42 +02:00
|
|
|
* for tabs instead. This means that if you have a tab and a
|
|
|
|
* formfield with the same name, this method gives priority
|
|
|
|
* to the formfield.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param SS_HTTPRequest $request
|
2009-07-07 00:06:42 +02:00
|
|
|
* @return FormField
|
2008-08-09 05:29:30 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function handleField($request) {
|
2012-01-02 15:03:35 +01:00
|
|
|
$field = $this->Fields()->dataFieldByName($request->param('FieldName'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-07 00:06:42 +02:00
|
|
|
if($field) {
|
|
|
|
return $field;
|
|
|
|
} else {
|
|
|
|
// falling back to fieldByName, e.g. for getting tabs
|
|
|
|
return $this->Fields()->fieldByName($request->param('FieldName'));
|
|
|
|
}
|
2008-08-09 05:29:30 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Convert this form into a readonly form
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function makeReadonly() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->transform(new ReadonlyTransformation());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Set whether the user should be redirected back down to the
|
|
|
|
* form on the page upon validation errors in the form or if
|
2009-05-01 05:49:34 +02:00
|
|
|
* they just need to redirect back to the page
|
|
|
|
*
|
|
|
|
* @param bool Redirect to the form
|
|
|
|
*/
|
|
|
|
public function setRedirectToFormOnValidationError($bool) {
|
|
|
|
$this->redirectToFormOnValidationError = $bool;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
/**
|
|
|
|
* Get whether the user should be redirected back down to the
|
|
|
|
* form on the page upon validation errors
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getRedirectToFormOnValidationError() {
|
|
|
|
return $this->redirectToFormOnValidationError;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-09-24 13:58:32 +02:00
|
|
|
* Add a plain text error message to a field on this form. It will be saved into the session
|
2007-07-19 12:40:28 +02:00
|
|
|
* and used the next time this form is displayed.
|
|
|
|
*/
|
2014-01-30 22:38:34 +01:00
|
|
|
public function addErrorMessage($fieldName, $message, $messageType, $escapeHtml = true) {
|
2010-10-15 04:50:43 +02:00
|
|
|
Session::add_to_array("FormInfo.{$this->FormName()}.errors", array(
|
2007-07-19 12:40:28 +02:00
|
|
|
'fieldName' => $fieldName,
|
2014-01-30 22:38:34 +01:00
|
|
|
'message' => $escapeHtml ? Convert::raw2xml($message) : $message,
|
2007-07-19 12:40:28 +02:00
|
|
|
'messageType' => $messageType,
|
|
|
|
));
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function transform(FormTransformation $trans) {
|
2011-05-11 09:51:54 +02:00
|
|
|
$newFields = new FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->fields as $field) {
|
|
|
|
$newFields->push($field->transform($trans));
|
|
|
|
}
|
|
|
|
$this->fields = $newFields;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$newActions = new FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->actions as $action) {
|
|
|
|
$newActions->push($action->transform($trans));
|
|
|
|
}
|
|
|
|
$this->actions = $newActions;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We have to remove validation, if the fields are not editable ;-)
|
|
|
|
if($this->validator)
|
|
|
|
$this->validator->removeValidation();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Get the {@link Validator} attached to this form.
|
|
|
|
* @return Validator
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getValidator() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->validator;
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Set the {@link Validator} on this form.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setValidator( Validator $validator ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($validator) {
|
|
|
|
$this->validator = $validator;
|
|
|
|
$this->validator->setForm($this);
|
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Remove the {@link Validator} from this from.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function unsetValidator(){
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->validator = null;
|
2012-04-16 22:04:28 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert this form to another format.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function transformTo(FormTransformation $format) {
|
2011-05-11 09:51:54 +02:00
|
|
|
$newFields = new FieldList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->fields as $field) {
|
|
|
|
$newFields->push($field->transformTo($format));
|
|
|
|
}
|
|
|
|
$this->fields = $newFields;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We have to remove validation, if the fields are not editable ;-)
|
|
|
|
if($this->validator)
|
|
|
|
$this->validator->removeValidation();
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-12-05 09:22:57 +01:00
|
|
|
* Generate extra special fields - namely the security token field (if required).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-03-16 23:15:04 +01:00
|
|
|
public function getExtraFields() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$extraFields = new FieldList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
$token = $this->getSecurityToken();
|
|
|
|
$tokenField = $token->updateFieldSet($this->fields);
|
|
|
|
if($tokenField) $tokenField->setForm($this);
|
|
|
|
$this->securityTokenAdded = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
// add the "real" HTTP method if necessary (for PUT, DELETE and HEAD)
|
2013-05-09 02:27:16 +02:00
|
|
|
if (strtoupper($this->FormMethod()) != $this->FormHttpMethod()) {
|
2008-10-06 16:58:01 +02:00
|
|
|
$methodField = new HiddenField('_method', '', $this->FormHttpMethod());
|
|
|
|
$methodField->setForm($this);
|
|
|
|
$extraFields->push($methodField);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
return $extraFields;
|
2008-03-13 02:39:57 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-13 02:39:57 +01:00
|
|
|
/**
|
|
|
|
* Return the form's fields - used by the templates
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList The form fields
|
2008-03-13 02:39:57 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function Fields() {
|
2008-03-16 23:15:04 +01:00
|
|
|
foreach($this->getExtraFields() as $field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if(!$this->fields->fieldByName($field->getName())) $this->fields->push($field);
|
2008-03-16 23:15:04 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-07 03:58:23 +01:00
|
|
|
return $this->fields;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-13 02:39:57 +01:00
|
|
|
/**
|
2008-03-16 23:15:04 +01:00
|
|
|
* Return all <input type="hidden"> fields
|
|
|
|
* in a form - including fields nested in {@link CompositeFields}.
|
|
|
|
* Useful when doing custom field layouts.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2008-03-13 02:39:57 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function HiddenFields() {
|
2013-04-17 17:42:48 +02:00
|
|
|
return $this->Fields()->HiddenFields();
|
2008-03-13 02:39:57 +01:00
|
|
|
}
|
2012-03-07 21:54:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all fields except for the hidden fields.
|
|
|
|
* Useful when making your own simplified form layouts.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function VisibleFields() {
|
2013-04-17 17:42:48 +02:00
|
|
|
return $this->Fields()->VisibleFields();
|
2012-03-07 21:54:31 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-11 21:25:22 +01:00
|
|
|
/**
|
|
|
|
* Setter for the form fields.
|
|
|
|
*
|
2011-05-11 09:51:54 +02:00
|
|
|
* @param FieldList $fields
|
2008-02-11 21:25:22 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setFields($fields) {
|
2008-02-11 21:25:22 +01:00
|
|
|
$this->fields = $fields;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-02-11 21:25:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the form's action buttons - used by the templates
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList The action list
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function Actions() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->actions;
|
|
|
|
}
|
|
|
|
|
2008-02-11 21:25:22 +01:00
|
|
|
/**
|
|
|
|
* Setter for the form actions.
|
|
|
|
*
|
2011-05-11 09:51:54 +02:00
|
|
|
* @param FieldList $actions
|
2008-02-11 21:25:22 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setActions($actions) {
|
2008-02-11 21:25:22 +01:00
|
|
|
$this->actions = $actions;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-02-11 21:25:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Unset all form actions
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function unsetAllActions(){
|
2011-05-11 09:51:54 +02:00
|
|
|
$this->actions = new FieldList();
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2011-12-22 18:05:20 +01:00
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
* @param String
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setAttribute($name, $value) {
|
2011-12-22 18:05:20 +01:00
|
|
|
$this->attributes[$name] = $value;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-12-22 18:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getAttribute($name) {
|
2014-03-04 23:47:02 +01:00
|
|
|
if(isset($this->attributes[$name])) return $this->attributes[$name];
|
2011-12-22 18:05:20 +01:00
|
|
|
}
|
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getAttributes() {
|
2011-12-22 18:05:20 +01:00
|
|
|
$attrs = array(
|
2013-10-30 05:08:31 +01:00
|
|
|
'id' => $this->FormName(),
|
2011-12-22 18:05:20 +01:00
|
|
|
'action' => $this->FormAction(),
|
|
|
|
'method' => $this->FormMethod(),
|
|
|
|
'enctype' => $this->getEncType(),
|
|
|
|
'target' => $this->target,
|
|
|
|
'class' => $this->extraClass(),
|
|
|
|
);
|
2013-05-26 01:09:03 +02:00
|
|
|
|
2011-12-22 18:05:20 +01:00
|
|
|
if($this->validator && $this->validator->getErrors()) {
|
|
|
|
if(!isset($attrs['class'])) $attrs['class'] = '';
|
|
|
|
$attrs['class'] .= ' validationerror';
|
|
|
|
}
|
|
|
|
|
|
|
|
$attrs = array_merge($attrs, $this->attributes);
|
|
|
|
|
|
|
|
return $attrs;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-01-06 03:16:16 +01:00
|
|
|
* Return the attributes of the form tag - used by the templates.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-22 18:05:20 +01:00
|
|
|
* @param Array Custom attributes to process. Falls back to {@link getAttributes()}.
|
|
|
|
* If at least one argument is passed as a string, all arguments act as excludes by name.
|
|
|
|
* @return String HTML attributes, ready for insertion into an HTML tag
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getAttributesHTML($attrs = null) {
|
2011-12-22 18:05:20 +01:00
|
|
|
$exclude = (is_string($attrs)) ? func_get_args() : null;
|
|
|
|
|
|
|
|
if(!$attrs || is_string($attrs)) $attrs = $this->getAttributes();
|
|
|
|
|
2013-05-26 01:09:03 +02:00
|
|
|
|
2011-09-29 05:21:32 +02:00
|
|
|
// Figure out if we can cache this form
|
|
|
|
// - forms with validation shouldn't be cached, cos their error messages won't be shown
|
|
|
|
// - forms with security tokens shouldn't be cached because security tokens expire
|
|
|
|
$needsCacheDisabled = false;
|
|
|
|
if ($this->getSecurityToken()->isEnabled()) $needsCacheDisabled = true;
|
2013-05-09 02:27:16 +02:00
|
|
|
if ($this->FormMethod() != 'GET') $needsCacheDisabled = true;
|
2013-01-24 07:56:02 +01:00
|
|
|
if (!($this->validator instanceof RequiredFields) || count($this->validator->getRequired())) {
|
|
|
|
$needsCacheDisabled = true;
|
|
|
|
}
|
2011-09-29 05:21:32 +02:00
|
|
|
|
|
|
|
// If we need to disable cache, do it
|
|
|
|
if ($needsCacheDisabled) HTTP::set_cache_age(0);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-12-22 18:05:20 +01:00
|
|
|
$attrs = $this->getAttributes();
|
|
|
|
|
|
|
|
// Remove empty
|
2014-08-15 08:53:05 +02:00
|
|
|
$attrs = array_filter((array)$attrs, create_function('$v', 'return ($v || $v === 0);'));
|
|
|
|
|
2011-12-22 18:05:20 +01:00
|
|
|
// Remove excluded
|
|
|
|
if($exclude) $attrs = array_diff_key($attrs, array_flip($exclude));
|
|
|
|
|
2013-05-09 02:27:16 +02:00
|
|
|
// Prepare HTML-friendly 'method' attribute (lower-case)
|
|
|
|
if (isset($attrs['method'])) {
|
|
|
|
$attrs['method'] = strtolower($attrs['method']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create markup
|
2011-12-22 18:05:20 +01:00
|
|
|
$parts = array();
|
|
|
|
foreach($attrs as $name => $value) {
|
|
|
|
$parts[] = ($value === true) ? "{$name}=\"{$name}\"" : "{$name}=\"" . Convert::raw2att($value) . "\"";
|
2009-01-06 03:16:16 +01:00
|
|
|
}
|
2011-12-22 18:05:20 +01:00
|
|
|
|
|
|
|
return implode(' ', $parts);
|
|
|
|
}
|
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function FormAttributes() {
|
2011-12-22 18:05:20 +01:00
|
|
|
return $this->getAttributesHTML();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
/**
|
2013-05-26 01:09:03 +02:00
|
|
|
* Set the {@link FormTemplateHelper}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-26 01:09:03 +02:00
|
|
|
* @param string|FormTemplateHelper
|
2008-03-16 23:15:04 +01:00
|
|
|
*/
|
2013-05-26 01:09:03 +02:00
|
|
|
public function setTemplateHelper($helper) {
|
|
|
|
$this->templateHelper = $helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a {@link FormTemplateHelper} for this form. If one has not been
|
|
|
|
* set, return the default helper.
|
|
|
|
*
|
|
|
|
* @return FormTemplateHelper
|
|
|
|
*/
|
|
|
|
public function getTemplateHelper() {
|
|
|
|
if($this->templateHelper) {
|
|
|
|
if(is_string($this->templateHelper)) {
|
|
|
|
return Injector::inst()->get($this->templateHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->templateHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Injector::inst()->get('FormTemplateHelper');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the target of this form to any value - useful for opening the form
|
|
|
|
* contents in a new window or refreshing another frame.
|
|
|
|
*
|
|
|
|
* @param target $target The value of the target
|
|
|
|
*
|
|
|
|
* @return FormField
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setTarget($target) {
|
2008-03-16 23:15:04 +01:00
|
|
|
$this->target = $target;
|
2013-05-26 01:09:03 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-03-16 23:15:04 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Set the legend value to be inserted into
|
|
|
|
* the <legend> element in the Form.ss template.
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setLegend($legend) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->legend = $legend;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-12 02:43:56 +01:00
|
|
|
/**
|
|
|
|
* Set the SS template that this form should use
|
|
|
|
* to render with. The default is "Form".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-12 02:43:56 +01:00
|
|
|
* @param string $template The name of the template (without the .ss extension)
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setTemplate($template) {
|
2009-03-12 02:43:56 +01:00
|
|
|
$this->template = $template;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-03-12 02:43:56 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-12 02:43:56 +01:00
|
|
|
/**
|
|
|
|
* Return the template to render this form with.
|
|
|
|
* If the template isn't set, then default to the
|
|
|
|
* form class name e.g "Form".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-12 02:43:56 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getTemplate() {
|
2009-03-12 02:43:56 +01:00
|
|
|
if($this->template) return $this->template;
|
|
|
|
else return $this->class;
|
|
|
|
}
|
2011-12-26 08:36:24 +01:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
2011-12-26 08:36:24 +01:00
|
|
|
* Returns the encoding type for the form.
|
|
|
|
*
|
|
|
|
* By default this will be URL encoded, unless there is a file field present
|
|
|
|
* in which case multipart is used. You can also set the enc type using
|
|
|
|
* {@link setEncType}.
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2011-12-26 08:36:24 +01:00
|
|
|
public function getEncType() {
|
|
|
|
if ($this->encType) {
|
|
|
|
return $this->encType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fields = $this->fields->dataFields()) {
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
if ($field instanceof FileField) return self::ENC_TYPE_MULTIPART;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-26 08:36:24 +01:00
|
|
|
|
|
|
|
return self::ENC_TYPE_URLENCODED;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-12-26 08:36:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the form encoding type. The most common encoding types are defined
|
|
|
|
* in {@link ENC_TYPE_URLENCODED} and {@link ENC_TYPE_MULTIPART}.
|
|
|
|
*
|
|
|
|
* @param string $enctype
|
|
|
|
*/
|
|
|
|
public function setEncType($encType) {
|
|
|
|
$this->encType = $encType;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-12-26 08:36:24 +01:00
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
2008-10-06 16:58:01 +02:00
|
|
|
* Returns the real HTTP method for the form:
|
|
|
|
* GET, POST, PUT, DELETE or HEAD.
|
|
|
|
* As most browsers only support GET and POST in
|
|
|
|
* form submissions, all other HTTP methods are
|
|
|
|
* added as a hidden field "_method" that
|
|
|
|
* gets evaluated in {@link Director::direct()}.
|
|
|
|
* See {@link FormMethod()} to get a HTTP method
|
|
|
|
* for safe insertion into a <form> tag.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-06 16:58:01 +02:00
|
|
|
* @return string HTTP method
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function FormHttpMethod() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->formMethod;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
2008-10-06 16:58:01 +02:00
|
|
|
* Returns the form method to be used in the <form> tag.
|
|
|
|
* See {@link FormHttpMethod()} to get the "real" method.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-09 02:27:16 +02:00
|
|
|
* @return string Form HTTP method restricted to 'GET' or 'POST'
|
2008-10-06 16:58:01 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function FormMethod() {
|
2013-05-09 02:27:16 +02:00
|
|
|
if(in_array($this->formMethod,array('GET','POST'))) {
|
2008-10-06 16:58:01 +02:00
|
|
|
return $this->formMethod;
|
|
|
|
} else {
|
2013-05-09 02:27:16 +02:00
|
|
|
return 'POST';
|
2008-10-06 16:58:01 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-06 16:58:01 +02:00
|
|
|
/**
|
|
|
|
* Set the form method: GET, POST, PUT, DELETE.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-16 23:15:04 +01:00
|
|
|
* @param $method string
|
2013-03-25 06:16:11 +01:00
|
|
|
* @param $strict If non-null, pass value to {@link setStrictFormMethodCheck()}.
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2013-03-25 06:16:11 +01:00
|
|
|
public function setFormMethod($method, $strict = null) {
|
2013-05-09 02:27:16 +02:00
|
|
|
$this->formMethod = strtoupper($method);
|
2013-03-25 06:16:11 +01:00
|
|
|
if($strict !== null) $this->setStrictFormMethodCheck($strict);
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-03-25 06:16:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If set to true, enforce the matching of the form method.
|
|
|
|
*
|
|
|
|
* This will mean two things:
|
|
|
|
* - GET vars will be ignored by a POST form, and vice versa
|
|
|
|
* - A submission where the HTTP method used doesn't match the form will return a 400 error.
|
|
|
|
*
|
|
|
|
* If set to false (the default), then the form method is only used to construct the default
|
|
|
|
* form.
|
|
|
|
*
|
|
|
|
* @param $bool boolean
|
|
|
|
*/
|
|
|
|
public function setStrictFormMethodCheck($bool) {
|
|
|
|
$this->strictFormMethodCheck = (bool)$bool;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getStrictFormMethodCheck() {
|
|
|
|
return $this->strictFormMethodCheck;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Return the form's action attribute.
|
|
|
|
* This is build by adding an executeForm get variable to the parent controller's Link() value
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function FormAction() {
|
2008-08-09 05:54:55 +02:00
|
|
|
if ($this->formActionPath) {
|
|
|
|
return $this->formActionPath;
|
|
|
|
} elseif($this->controller->hasMethod("FormObjectLink")) {
|
2008-08-09 05:19:54 +02:00
|
|
|
return $this->controller->FormObjectLink($this->name);
|
2008-08-09 05:54:55 +02:00
|
|
|
} else {
|
2009-10-15 02:02:05 +02:00
|
|
|
return Controller::join_links($this->controller->Link(), $this->name);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
|
|
|
* Set the form action attribute to a custom URL.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-26 01:09:03 +02:00
|
|
|
* Note: For "normal" forms, you shouldn't need to use this method. It is
|
|
|
|
* recommended only for situations where you have two relatively distinct
|
|
|
|
* parts of the system trying to communicate via a form post.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
*
|
|
|
|
* @return Form
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setFormAction($path) {
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->formActionPath = $path;
|
2013-05-26 01:09:03 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
2013-05-26 01:09:03 +02:00
|
|
|
* Returns the name of the form.
|
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function FormName() {
|
2013-05-30 11:06:41 +02:00
|
|
|
return $this->getTemplateHelper()->generateFormID($this);
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-26 01:09:03 +02:00
|
|
|
* Set the HTML ID attribute of the form.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return FormField
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setHTMLID($id) {
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->htmlID = $id;
|
2013-05-26 01:09:03 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-26 01:09:03 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHTMLID() {
|
|
|
|
return $this->htmlID;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 08:54:59 +02:00
|
|
|
/**
|
2012-05-24 00:33:40 +02:00
|
|
|
* Returns this form's controller.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
|
|
|
* @return Controller
|
|
|
|
* @deprecated 4.0
|
2008-08-06 08:54:59 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function Controller() {
|
2013-05-30 11:06:54 +02:00
|
|
|
Deprecation::notice('4.0', 'Use getController() rather than Controller() to access controller');
|
|
|
|
|
2012-05-24 00:33:40 +02:00
|
|
|
return $this->getController();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the controller.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2012-05-24 00:33:40 +02:00
|
|
|
* @return Controller
|
|
|
|
*/
|
|
|
|
public function getController() {
|
2008-08-06 08:54:59 +02:00
|
|
|
return $this->controller;
|
|
|
|
}
|
2012-05-24 00:33:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the controller.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2012-05-24 00:33:40 +02:00
|
|
|
* @param Controller $controller
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function setController($controller) {
|
|
|
|
$this->controller = $controller;
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-05-24 00:33:40 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the form.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2012-05-24 00:33:40 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
2007-08-23 07:47:54 +02:00
|
|
|
return $this->name;
|
|
|
|
}
|
2012-05-24 00:33:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the name of the form.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2012-05-24 00:33:40 +02:00
|
|
|
* @param string $name
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$this->name = $name;
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-05-24 00:33:40 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns an object where there is a method with the same name as each data
|
2013-05-30 11:06:54 +02:00
|
|
|
* field on the form.
|
|
|
|
*
|
2008-08-06 08:54:59 +02:00
|
|
|
* That method will return the field itself.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
|
|
|
* It means that you can execute $firstName = $form->FieldMap()->FirstName()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function FieldMap() {
|
2008-08-06 08:54:59 +02:00
|
|
|
return new Form_FieldMap($this);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* The next functions store and modify the forms message attributes.
|
2013-05-30 11:06:54 +02:00
|
|
|
* messages are stored in session under $_SESSION[formname][message];
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-16 23:15:04 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function Message() {
|
2007-09-16 03:52:35 +02:00
|
|
|
$this->getMessageFromSession();
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2011-08-31 08:19:20 +02:00
|
|
|
return $this->message;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-16 23:15:04 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function MessageType() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->getMessageFromSession();
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->messageType;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2014-02-28 09:24:09 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function getMessageFromSession() {
|
|
|
|
if($this->message || $this->messageType) {
|
2007-09-16 03:52:35 +02:00
|
|
|
return $this->message;
|
2014-02-28 09:24:09 +01:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->message = Session::get("FormInfo.{$this->FormName()}.formError.message");
|
|
|
|
$this->messageType = Session::get("FormInfo.{$this->FormName()}.formError.type");
|
2014-02-28 09:24:09 +01:00
|
|
|
|
|
|
|
return $this->message;
|
2007-09-16 03:52:35 +02:00
|
|
|
}
|
2014-03-10 19:03:20 +01:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set a status message for the form.
|
2008-03-16 23:15:04 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $message the text of the message
|
|
|
|
* @param string $type Should be set to good, bad, or warning.
|
2014-01-30 22:38:34 +01:00
|
|
|
* @param boolean $escapeHtml Automatically sanitize the message. Set to FALSE if the message contains HTML.
|
|
|
|
* In that case, you might want to use {@link Convert::raw2xml()} to escape any
|
|
|
|
* user supplied data in the message.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-01-30 22:38:34 +01:00
|
|
|
public function setMessage($message, $type, $escapeHtml = true) {
|
|
|
|
$this->message = ($escapeHtml) ? Convert::raw2xml($message) : $message;
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->messageType = $type;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a message to the session, for display next time this form is shown.
|
2008-03-16 23:15:04 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $message the text of the message
|
|
|
|
* @param string $type Should be set to good, bad, or warning.
|
2014-01-30 22:38:34 +01:00
|
|
|
* @param boolean $escapeHtml Automatically sanitize the message. Set to FALSE if the message contains HTML.
|
|
|
|
* In that case, you might want to use {@link Convert::raw2xml()} to escape any
|
|
|
|
* user supplied data in the message.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-01-30 22:38:34 +01:00
|
|
|
public function sessionMessage($message, $type, $escapeHtml = true) {
|
|
|
|
Session::set(
|
|
|
|
"FormInfo.{$this->FormName()}.formError.message",
|
|
|
|
$escapeHtml ? Convert::raw2xml($message) : $message
|
|
|
|
);
|
2008-04-26 08:36:12 +02:00
|
|
|
Session::set("FormInfo.{$this->FormName()}.formError.type", $type);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2014-11-18 00:45:54 +01:00
|
|
|
public static function messageForForm($formName, $message, $type, $escapeHtml = true) {
|
2014-01-30 22:38:34 +01:00
|
|
|
Session::set(
|
|
|
|
"FormInfo.{$formName}.formError.message",
|
|
|
|
$escapeHtml ? Convert::raw2xml($message) : $message
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
Session::set("FormInfo.{$formName}.formError.type", $type);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function clearMessage() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->message = null;
|
|
|
|
Session::clear("FormInfo.{$this->FormName()}.errors");
|
|
|
|
Session::clear("FormInfo.{$this->FormName()}.formError");
|
2011-08-31 08:19:20 +02:00
|
|
|
Session::clear("FormInfo.{$this->FormName()}.data");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function resetValidation() {
|
2007-07-19 12:40:28 +02:00
|
|
|
Session::clear("FormInfo.{$this->FormName()}.errors");
|
2011-08-31 08:19:20 +02:00
|
|
|
Session::clear("FormInfo.{$this->FormName()}.data");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
2008-10-07 18:47:01 +02:00
|
|
|
* Returns the DataObject that has given this form its data
|
2009-05-20 07:07:06 +02:00
|
|
|
* through {@link loadDataFrom()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getRecord() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->record;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Get the legend value to be inserted into the
|
|
|
|
* <legend> element in Form.ss
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getLegend() {
|
2008-12-04 23:38:32 +01:00
|
|
|
return $this->legend;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Processing that occurs before a form is executed.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* This includes form validation, if it fails, we redirect back
|
2008-10-01 20:32:10 +02:00
|
|
|
* to the form with appropriate error messages.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* Triggered through {@link httpSubmission()}.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* Note that CSRF protection takes place in {@link httpSubmission()},
|
|
|
|
* if it fails the form data will never reach this method.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* @return boolean
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public function validate(){
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->validator){
|
2007-09-16 03:52:35 +02:00
|
|
|
$errors = $this->validator->validate();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($errors){
|
2012-04-30 17:15:30 +02:00
|
|
|
// Load errors into session and post back
|
|
|
|
$data = $this->getData();
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
Session::set("FormInfo.{$this->FormName()}.errors", $errors);
|
2012-04-30 17:15:30 +02:00
|
|
|
Session::set("FormInfo.{$this->FormName()}.data", $data);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-09-16 03:52:35 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2012-11-15 23:47:32 +01:00
|
|
|
const MERGE_DEFAULT = 0;
|
|
|
|
const MERGE_CLEAR_MISSING = 1;
|
|
|
|
const MERGE_IGNORE_FALSEISH = 2;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-10-07 19:44:46 +02:00
|
|
|
* Load data from the given DataObject or array.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* It will call $object->MyField to get the value of MyField.
|
2008-10-07 19:44:46 +02:00
|
|
|
* If you passed an array, it will call $object[MyField].
|
|
|
|
* Doesn't save into dataless FormFields ({@link DatalessField}),
|
2011-10-28 03:37:27 +02:00
|
|
|
* as determined by {@link FieldList->dataFields()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-07 19:44:46 +02:00
|
|
|
* By default, if a field isn't set (as determined by isset()),
|
|
|
|
* its value will not be saved to the field, retaining
|
|
|
|
* potential existing values.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Passed data should not be escaped, and is saved to the FormField
|
2013-05-30 11:06:54 +02:00
|
|
|
* instances unescaped.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Escaping happens automatically on saving the data through
|
2013-05-30 11:06:54 +02:00
|
|
|
* {@link saveInto()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @uses FieldList->dataFields()
|
2008-10-07 19:44:46 +02:00
|
|
|
* @uses FormField->setValue()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-07 19:44:46 +02:00
|
|
|
* @param array|DataObject $data
|
2012-11-15 23:47:32 +01:00
|
|
|
* @param int $mergeStrategy
|
2014-08-15 08:53:05 +02:00
|
|
|
* For every field, {@link $data} is interogated whether it contains a
|
2013-05-30 11:06:54 +02:00
|
|
|
* relevant property/key, and
|
2012-11-15 23:47:32 +01:00
|
|
|
* what that property/key's value is.
|
|
|
|
*
|
|
|
|
* By default, if {@link $data} does contain a property/key, the fields value is always replaced by {@link $data}'s
|
|
|
|
* value, even if that value is null/false/etc. Fields which don't match any property/key in {@link $data} are
|
|
|
|
* "left alone", meaning they retain any previous value.
|
|
|
|
*
|
|
|
|
* You can pass a bitmask here to change this behaviour.
|
|
|
|
*
|
|
|
|
* Passing CLEAR_MISSING means that any fields that don't match any property/key in
|
|
|
|
* {@link $data} are cleared.
|
|
|
|
*
|
|
|
|
* Passing IGNORE_FALSEISH means that any false-ish value in {@link $data} won't replace
|
|
|
|
* a field's value.
|
|
|
|
*
|
|
|
|
* For backwards compatibility reasons, this parameter can also be set to === true, which is the same as passing
|
|
|
|
* CLEAR_MISSING
|
|
|
|
*
|
|
|
|
* @param $fieldList An optional list of fields to process. This can be useful when you have a
|
2009-11-05 02:55:27 +01:00
|
|
|
* form that has some fields that save to one object, and some that save to another.
|
2012-06-20 17:01:16 +02:00
|
|
|
* @return Form
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-11-15 23:47:32 +01:00
|
|
|
public function loadDataFrom($data, $mergeStrategy = 0, $fieldList = null) {
|
2008-10-07 19:44:46 +02:00
|
|
|
if(!is_object($data) && !is_array($data)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
|
2012-06-20 17:01:16 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2012-11-15 23:47:32 +01:00
|
|
|
// Handle the backwards compatible case of passing "true" as the second argument
|
|
|
|
if ($mergeStrategy === true) {
|
|
|
|
$mergeStrategy = self::MERGE_CLEAR_MISSING;
|
|
|
|
}
|
|
|
|
else if ($mergeStrategy === false) {
|
|
|
|
$mergeStrategy = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-07 19:44:46 +02:00
|
|
|
// if an object is passed, save it for historical reference through {@link getRecord()}
|
|
|
|
if(is_object($data)) $this->record = $data;
|
|
|
|
|
|
|
|
// dont include fields without data
|
2007-07-19 12:40:28 +02:00
|
|
|
$dataFields = $this->fields->dataFields();
|
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
$name = $field->getName();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-05 02:55:27 +01:00
|
|
|
// Skip fields that have been exlcuded
|
|
|
|
if($fieldList && !in_array($name, $fieldList)) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-07 19:44:46 +02:00
|
|
|
// First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
|
|
|
|
if(is_array($data) && isset($data[$name . '_unchanged'])) continue;
|
2012-11-15 23:47:32 +01:00
|
|
|
|
|
|
|
// Does this property exist on $data?
|
|
|
|
$exists = false;
|
|
|
|
// The value from $data for this field
|
|
|
|
$val = null;
|
|
|
|
|
|
|
|
if(is_object($data)) {
|
|
|
|
$exists = (
|
|
|
|
isset($data->$name) ||
|
|
|
|
$data->hasMethod($name) ||
|
|
|
|
($data->hasMethod('hasField') && $data->hasField($name))
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($exists) {
|
|
|
|
$val = $data->__get($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(is_array($data)){
|
|
|
|
if(array_key_exists($name, $data)) {
|
|
|
|
$exists = true;
|
|
|
|
$val = $data[$name];
|
|
|
|
}
|
|
|
|
// If field is in array-notation we need to access nested data
|
|
|
|
else if(strpos($name,'[')) {
|
|
|
|
// First encode data using PHP's method of converting nested arrays to form data
|
|
|
|
$flatData = urldecode(http_build_query($data));
|
|
|
|
// Then pull the value out from that flattened string
|
|
|
|
preg_match('/' . addcslashes($name,'[]') . '=([^&]*)/', $flatData, $matches);
|
|
|
|
|
|
|
|
if (isset($matches[1])) {
|
|
|
|
$exists = true;
|
|
|
|
$val = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
2008-10-07 19:44:46 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2008-10-07 19:44:46 +02:00
|
|
|
// save to the field if either a value is given, or loading of blank/undefined values is forced
|
2012-11-15 23:47:32 +01:00
|
|
|
if($exists){
|
|
|
|
if ($val != false || ($mergeStrategy & self::MERGE_IGNORE_FALSEISH) != self::MERGE_IGNORE_FALSEISH){
|
|
|
|
// pass original data as well so composite fields can act on the additional information
|
|
|
|
$field->setValue($val, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(($mergeStrategy & self::MERGE_CLEAR_MISSING) == self::MERGE_CLEAR_MISSING){
|
2008-10-07 19:44:46 +02:00
|
|
|
$field->setValue($val, $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-20 17:01:16 +02:00
|
|
|
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Save the contents of this form into the given data object.
|
|
|
|
* It will make use of setCastedField() to do this.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-11-05 02:55:27 +01:00
|
|
|
* @param $dataObject The object to save data into
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param $fieldList An optional list of fields to process. This can be useful when you have a
|
2009-11-05 02:55:27 +01:00
|
|
|
* form that has some fields that save to one object, and some that save to another.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
|
2008-08-12 04:58:48 +02:00
|
|
|
$dataFields = $this->fields->saveableFields();
|
2007-07-19 12:40:28 +02:00
|
|
|
$lastField = null;
|
|
|
|
if($dataFields) foreach($dataFields as $field) {
|
2014-06-10 14:50:28 +02:00
|
|
|
// Skip fields that have been excluded
|
2011-10-29 06:01:52 +02:00
|
|
|
if($fieldList && is_array($fieldList) && !in_array($field->getName(), $fieldList)) continue;
|
2009-11-05 02:55:27 +01:00
|
|
|
|
|
|
|
|
2011-10-29 06:01:52 +02:00
|
|
|
$saveMethod = "save{$field->getName()}";
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2011-10-29 06:01:52 +02:00
|
|
|
if($field->getName() == "ClassName"){
|
2007-09-16 03:52:35 +02:00
|
|
|
$lastField = $field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}else if( $dataObject->hasMethod( $saveMethod ) ){
|
2007-11-19 02:12:35 +01:00
|
|
|
$dataObject->$saveMethod( $field->dataValue());
|
2011-10-29 06:01:52 +02:00
|
|
|
} else if($field->getName() != "ID"){
|
2007-09-16 03:52:35 +02:00
|
|
|
$field->saveInto($dataObject);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if($lastField) $lastField->saveInto($dataObject);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-10-01 20:32:10 +02:00
|
|
|
* Get the submitted data from this form through
|
2014-08-15 08:53:05 +02:00
|
|
|
* {@link FieldList->dataFields()}, which filters out any form-specific data
|
2013-05-30 11:06:54 +02:00
|
|
|
* like form-actions.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Calls {@link FormField->dataValue()} on each field, which returns a value
|
2013-05-30 11:06:54 +02:00
|
|
|
* suitable for insertion into a DataObject property.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-01 20:32:10 +02:00
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getData() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$dataFields = $this->fields->dataFields();
|
2009-02-02 00:49:53 +01:00
|
|
|
$data = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($dataFields){
|
|
|
|
foreach($dataFields as $field) {
|
2011-10-29 06:01:52 +02:00
|
|
|
if($field->getName()) {
|
|
|
|
$data[$field->getName()] = $field->dataValue();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $data;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Call the given method on the given field.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* This is used by Ajax-savvy form fields. By putting '&action=callfieldmethod'
|
2013-05-30 11:06:54 +02:00
|
|
|
* to the end of the form action, they can access server-side data.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param fieldName The name of the field. Can be overridden by $_REQUEST[fieldName]
|
|
|
|
* @param methodName The name of the field. Can be overridden by $_REQUEST[methodName]
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function callfieldmethod($data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$fieldName = $data['fieldName'];
|
|
|
|
$methodName = $data['methodName'];
|
|
|
|
$fields = $this->fields->dataFields();
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// special treatment needed for TableField-class and TreeDropdownField
|
|
|
|
if(strpos($fieldName, '[')) {
|
|
|
|
preg_match_all('/([^\[]*)/',$fieldName, $fieldNameMatches);
|
|
|
|
preg_match_all('/\[([^\]]*)\]/',$fieldName, $subFieldMatches);
|
|
|
|
$tableFieldName = $fieldNameMatches[1][0];
|
|
|
|
$subFieldName = $subFieldMatches[1][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($tableFieldName) && isset($subFieldName) && is_a($fields[$tableFieldName], 'TableField')) {
|
|
|
|
$field = $fields[$tableFieldName]->getField($subFieldName, $fieldName);
|
2007-09-16 03:52:35 +02:00
|
|
|
return $field->$methodName();
|
2007-09-14 19:10:37 +02:00
|
|
|
} else if(isset($fields[$fieldName])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $fields[$fieldName]->$methodName();
|
|
|
|
} else {
|
|
|
|
user_error("Form::callfieldmethod() Field '$fieldName' not found", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return a rendered version of this form.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-12 02:43:56 +01:00
|
|
|
* This is returned when you access a form as $FormObject rather
|
2013-03-19 11:43:23 +01:00
|
|
|
* than <% with FormObject %>
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
|
|
|
* @return HTML
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function forTemplate() {
|
2013-01-21 11:08:28 +01:00
|
|
|
$return = $this->renderWith(array_merge(
|
2011-04-16 07:52:10 +02:00
|
|
|
(array)$this->getTemplate(),
|
|
|
|
array('Form')
|
2008-06-12 11:04:07 +02:00
|
|
|
));
|
2013-01-21 11:08:28 +01:00
|
|
|
|
|
|
|
// Now that we're rendered, clear message
|
|
|
|
$this->clearMessage();
|
|
|
|
|
|
|
|
return $return;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
|
|
|
* Return a rendered version of this form, suitable for ajax post-back.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* It triggers slightly different behaviour, such as disabling the rewriting
|
2013-05-30 11:06:54 +02:00
|
|
|
* of # links.
|
|
|
|
*
|
|
|
|
* @return HTML
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function forAjaxTemplate() {
|
2009-03-12 02:47:21 +01:00
|
|
|
$view = new SSViewer(array(
|
|
|
|
$this->getTemplate(),
|
|
|
|
'Form'
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-19 14:56:43 +02:00
|
|
|
$return = $view->dontRewriteHashlinks()->process($this);
|
|
|
|
|
|
|
|
// Now that we're rendered, clear message
|
|
|
|
$this->clearMessage();
|
|
|
|
|
|
|
|
return $return;
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns an HTML rendition of this form, without the <form> tag itself.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Attaches 3 extra hidden files, _form_action, _form_name, _form_method,
|
|
|
|
* and _form_enctype. These are the attributes of the form. These fields
|
2013-05-30 11:06:54 +02:00
|
|
|
* can be used to send the form to Ajax.
|
|
|
|
*
|
|
|
|
* @return HTML
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function formHtmlContent() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->IncludeFormTag = false;
|
|
|
|
$content = $this->forTemplate();
|
|
|
|
$this->IncludeFormTag = true;
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_action\" id=\"" . $this->FormName . "_form_action\""
|
|
|
|
. " value=\"" . $this->FormAction() . "\" />\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_name\" value=\"" . $this->FormName() . "\" />\n";
|
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_method\" value=\"" . $this->FormMethod() . "\" />\n";
|
2014-01-02 10:44:00 +01:00
|
|
|
$content .= "<input type=\"hidden\" name=\"_form_enctype\" value=\"" . $this->getEncType() . "\" />\n";
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Render this form using the given template, and return the result as a
|
2013-05-30 11:06:54 +02:00
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* You can pass either an SSViewer or a template name.
|
|
|
|
*
|
|
|
|
* @param SSViewer|string $template
|
|
|
|
*
|
|
|
|
* @return HTML
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function renderWithoutActionButton($template) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$custom = $this->customise(array(
|
|
|
|
"Actions" => "",
|
|
|
|
));
|
|
|
|
|
2013-05-30 11:06:54 +02:00
|
|
|
if(is_string($template)) {
|
|
|
|
$template = new SSViewer($template);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $template->process($custom);
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Sets the button that was clicked. This should only be called by the
|
2013-05-30 11:06:54 +02:00
|
|
|
* {@link Controller}
|
|
|
|
*
|
|
|
|
* @param string $funcName The name of the action method that will be called
|
|
|
|
*
|
|
|
|
* @return Form
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function setButtonClicked($funcName) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->buttonClickedFunc = $funcName;
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2013-05-30 11:06:54 +02:00
|
|
|
/**
|
|
|
|
* @return FormAction
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function buttonClicked() {
|
2013-10-03 15:24:19 +02:00
|
|
|
foreach($this->actions->dataFields() as $action) {
|
|
|
|
if($action->hasMethod('actionname') && $this->buttonClickedFunc == $action->actionName()) {
|
2013-05-30 11:06:54 +02:00
|
|
|
return $action;
|
2013-10-03 15:24:19 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-23 03:00:24 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Return the default button that should be clicked when another one isn't
|
2013-05-30 11:06:54 +02:00
|
|
|
* available.
|
|
|
|
*
|
|
|
|
* @return FormAction
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function defaultAction() {
|
2013-05-30 11:06:54 +02:00
|
|
|
if($this->hasDefaultAction && $this->actions) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->actions->First();
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Disable the default button.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Ordinarily, when a form is processed and no action_XXX button is
|
|
|
|
* available, then the first button in the actions list will be pressed.
|
2013-05-30 11:06:54 +02:00
|
|
|
* However, if this is "delete", for example, this isn't such a good idea.
|
|
|
|
*
|
|
|
|
* @return Form
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function disableDefaultAction() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->hasDefaultAction = false;
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-10-26 04:23:55 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Disable the requirement of a security token on this form instance. This
|
|
|
|
* security protects against CSRF attacks, but you should disable this if
|
2013-05-30 11:06:54 +02:00
|
|
|
* you don't want to tie a form to a session - eg a search form.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Check for token state with {@link getSecurityToken()} and
|
2013-05-30 11:06:54 +02:00
|
|
|
* {@link SecurityToken->isEnabled()}.
|
|
|
|
*
|
|
|
|
* @return Form
|
2007-10-26 04:23:55 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function disableSecurityToken() {
|
2010-12-05 09:22:57 +01:00
|
|
|
$this->securityToken = new NullSecurityToken();
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-10-26 04:23:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:30:32 +01:00
|
|
|
/**
|
|
|
|
* Enable {@link SecurityToken} protection for this form instance.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Check for token state with {@link getSecurityToken()} and
|
2013-05-30 11:06:54 +02:00
|
|
|
* {@link SecurityToken->isEnabled()}.
|
|
|
|
*
|
|
|
|
* @return Form
|
2010-12-05 09:30:32 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function enableSecurityToken() {
|
2010-12-05 09:30:32 +01:00
|
|
|
$this->securityToken = new SecurityToken();
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2010-12-05 09:30:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-05 09:22:57 +01:00
|
|
|
/**
|
|
|
|
* Returns the security token for this form (if any exists).
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* Doesn't check for {@link securityTokenEnabled()}.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* Use {@link SecurityToken::inst()} to get a global token.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-05 09:22:57 +01:00
|
|
|
* @return SecurityToken|null
|
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function getSecurityToken() {
|
2010-12-05 09:22:57 +01:00
|
|
|
return $this->securityToken;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns the name of a field, if that's the only field that the current
|
2013-05-30 11:06:54 +02:00
|
|
|
* controller is interested in.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* It checks for a call to the callfieldmethod action.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-16 23:15:04 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public static function single_field_required() {
|
2013-05-30 11:06:54 +02:00
|
|
|
if(self::current_action() == 'callfieldmethod') {
|
|
|
|
return $_REQUEST['fieldName'];
|
2013-06-19 11:17:33 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the current form action being called, if available.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public static function current_action() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return self::$current_action;
|
|
|
|
}
|
2007-09-16 03:52:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-05-30 11:06:54 +02:00
|
|
|
* Set the current form action. Should only be called by {@link Controller}.
|
|
|
|
*
|
|
|
|
* @param string $action
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public static function set_current_action($action) {
|
2007-07-19 12:40:28 +02:00
|
|
|
self::$current_action = $action;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-06 03:16:16 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Compiles all CSS-classes.
|
|
|
|
*
|
2011-08-22 06:44:41 +02:00
|
|
|
* @return string
|
2009-01-06 03:16:16 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function extraClass() {
|
2011-08-22 06:44:41 +02:00
|
|
|
return implode(array_unique($this->extraClasses), ' ');
|
2009-01-06 03:16:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-06 03:16:16 +01:00
|
|
|
/**
|
2011-08-22 06:44:41 +02:00
|
|
|
* Add a CSS-class to the form-container. If needed, multiple classes can
|
2014-08-15 08:53:05 +02:00
|
|
|
* be added by delimiting a string with spaces.
|
2011-08-22 06:44:41 +02:00
|
|
|
*
|
|
|
|
* @param string $class A string containing a classname or several class
|
|
|
|
* names delimited by a single space.
|
2013-05-30 11:06:54 +02:00
|
|
|
*
|
|
|
|
* @return Form
|
2009-01-06 03:16:16 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function addExtraClass($class) {
|
2013-06-29 12:42:07 +02:00
|
|
|
//split at white space
|
|
|
|
$classes = preg_split('/\s+/', $class);
|
2011-08-22 06:44:41 +02:00
|
|
|
foreach($classes as $class) {
|
2013-06-29 12:42:07 +02:00
|
|
|
//add classes one by one
|
|
|
|
$this->extraClasses[$class] = $class;
|
2011-08-22 06:44:41 +02:00
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-01-06 03:16:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-22 06:44:41 +02:00
|
|
|
* Remove a CSS-class from the form-container. Multiple class names can
|
|
|
|
* be passed through as a space delimited string
|
|
|
|
*
|
|
|
|
* @param string $class
|
2009-01-06 03:16:16 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function removeExtraClass($class) {
|
2013-06-29 12:42:07 +02:00
|
|
|
//split at white space
|
|
|
|
$classes = preg_split('/\s+/', $class);
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
//unset one by one
|
|
|
|
unset($this->extraClasses[$class]);
|
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-01-06 03:16:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function debug() {
|
2008-03-16 23:15:04 +01:00
|
|
|
$result = "<h3>$this->class</h3><ul>";
|
|
|
|
foreach($this->fields as $field) {
|
|
|
|
$result .= "<li>$field" . $field->debug() . "</li>";
|
|
|
|
}
|
|
|
|
$result .= "</ul>";
|
|
|
|
|
|
|
|
if( $this->validator )
|
2012-12-08 12:20:20 +01:00
|
|
|
$result .= '<h3>'._t('Form.VALIDATOR', 'Validator').'</h3>' . $this->validator->debug();
|
2008-03-16 23:15:04 +01:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2007-08-17 05:09:46 +02:00
|
|
|
// TESTING HELPERS
|
2012-09-26 23:34:00 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Test a submission of this form.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return SS_HTTPResponse the response object that the handling controller produces. You can interrogate this in
|
|
|
|
* your unit test.
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function testSubmission($action, $data) {
|
2007-08-17 05:09:46 +02:00
|
|
|
$data['action_' . $action] = true;
|
2012-12-08 12:20:20 +01:00
|
|
|
|
|
|
|
return Director::test($this->FormAction(), $data, Controller::curr()->getSession());
|
2007-08-17 07:44:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
|
|
|
* Test an ajax submission of this form.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return SS_HTTPResponse the response object that the handling controller produces. You can interrogate this in
|
|
|
|
* your unit test.
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function testAjaxSubmission($action, $data) {
|
2007-08-17 07:44:46 +02:00
|
|
|
$data['ajax'] = 1;
|
|
|
|
return $this->testSubmission($action, $data);
|
2007-08-17 05:09:46 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-06 08:54:59 +02:00
|
|
|
|
2010-04-23 02:11:41 +02:00
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
2009-11-05 02:14:54 +01:00
|
|
|
class Form_FieldMap extends ViewableData {
|
2013-05-30 11:06:54 +02:00
|
|
|
|
2008-08-06 08:54:59 +02:00
|
|
|
protected $form;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function __construct($form) {
|
2008-08-06 08:54:59 +02:00
|
|
|
$this->form = $form;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-05 02:14:54 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Ensure that all potential method calls get passed to __call(), therefore
|
2013-05-30 11:06:54 +02:00
|
|
|
* to dataFieldByName.
|
|
|
|
*
|
|
|
|
* @param string
|
2009-11-05 02:14:54 +01:00
|
|
|
*/
|
2012-03-09 03:41:01 +01:00
|
|
|
public function hasMethod($method) {
|
2009-11-05 02:14:54 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-09 03:41:01 +01:00
|
|
|
public function __call($method, $args = null) {
|
2010-04-13 04:05:57 +02:00
|
|
|
return $this->form->Fields()->fieldByName($method);
|
2008-08-06 08:54:59 +02:00
|
|
|
}
|
2008-08-12 04:51:33 +02:00
|
|
|
}
|