2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-26 01:06:39 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-07-12 08:51:08 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
use ReflectionClass;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Represents a field in a form.
|
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* A FieldList contains a number of FormField objects which make up the whole of a form.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* In addition to single fields, FormField objects can be "composite", for example, the
|
|
|
|
* {@link TabSet} field. Composite fields let us define complex forms without having to resort to
|
|
|
|
* custom HTML.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* To subclass:
|
|
|
|
*
|
|
|
|
* Define a {@link dataValue()} method that returns a value suitable for inserting into a single
|
|
|
|
* database field.
|
|
|
|
*
|
|
|
|
* For example, you might tidy up the format of a date or currency field. Define {@link saveInto()}
|
|
|
|
* to totally customise saving.
|
|
|
|
*
|
|
|
|
* For example, data might be saved to the filesystem instead of the data record, or saved to a
|
|
|
|
* component of the data record instead of the data record itself.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-03-28 10:52:51 +02:00
|
|
|
* A form field can be represented as structured data through {@link FormSchema},
|
|
|
|
* including both structure (name, id, attributes, etc.) and state (field value).
|
|
|
|
* Can be used by for JSON data which is consumed by a front-end application.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-10-30 23:03:21 +01:00
|
|
|
class FormField extends RequestHandler {
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2016-03-21 11:12:23 +01:00
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_STRING = 'String';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_HIDDEN = 'Hidden';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_TEXT = 'Text';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_HTML = 'HTML';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_INTEGER = 'Integer';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_DECIMAL = 'Decimal';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_MULTISELECT = 'MultiSelect';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_SINGLESELECT = 'SingleSelect';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_DATE = 'Date';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_DATETIME = 'DateTime';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_TIME = 'Time';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_BOOLEAN = 'Boolean';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_CUSTOM = 'Custom';
|
|
|
|
|
|
|
|
/** @see $schemaDataType */
|
|
|
|
const SCHEMA_DATA_TYPE_STRUCTURAL = 'Structural';
|
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
/**
|
|
|
|
* @var Form
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $form;
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2007-10-05 00:17:44 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
protected $title;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-10-05 00:17:44 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $message;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $messageType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $extraClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a title attribute to the markup.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Implement in all subclasses
|
2007-10-05 00:17:44 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $description;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Extra CSS classes for the FormField container.
|
|
|
|
*
|
|
|
|
* @var array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected $extraClasses;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-01-14 14:07:41 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array $default_classes The default classes to apply to the FormField
|
|
|
|
*/
|
2016-02-22 02:13:35 +01:00
|
|
|
private static $default_classes = [];
|
2015-01-14 14:07:41 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Right-aligned, contextual label for the field.
|
|
|
|
*
|
|
|
|
* @var string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected $rightTitle;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Left-aligned, contextual label for the field.
|
|
|
|
*
|
|
|
|
* @var string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected $leftTitle;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-11 02:02:49 +02:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Stores a reference to the FieldList that contains this object.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @var FieldList
|
2012-05-14 04:42:54 +02:00
|
|
|
*/
|
|
|
|
protected $containerFieldList;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @var bool
|
2008-08-12 04:58:48 +02:00
|
|
|
*/
|
|
|
|
protected $readonly = false;
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @var bool
|
2008-08-12 04:58:48 +02:00
|
|
|
*/
|
|
|
|
protected $disabled = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-16 05:09:30 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Custom validation message for the field.
|
|
|
|
*
|
|
|
|
* @var string
|
2009-04-29 01:55:53 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
protected $customValidationMessage = '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Name of the template used to render this form field. If not set, then will look up the class
|
|
|
|
* ancestry for the first matching template where the template name equals the class name.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* To explicitly use a custom template or one named other than the form field see
|
|
|
|
* {@link setTemplate()}.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2011-03-23 05:12:25 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
protected $template;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Name of the template used to render this form field. If not set, then will look up the class
|
|
|
|
* ancestry for the first matching template where the template name equals the class name.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* To explicitly use a custom template or one named other than the form field see
|
|
|
|
* {@link setFieldHolderTemplate()}.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2011-03-23 05:12:25 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
protected $fieldHolderTemplate;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $smallFieldHolderTemplate;
|
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* All attributes on the form field (not the field holder).
|
|
|
|
*
|
|
|
|
* Partially determined based on other instance properties.
|
|
|
|
*
|
|
|
|
* @see getAttributes()
|
|
|
|
*
|
|
|
|
* @var array
|
2011-12-22 13:10:57 +01:00
|
|
|
*/
|
2016-02-22 02:13:35 +01:00
|
|
|
protected $attributes = [];
|
|
|
|
|
2016-03-28 10:52:51 +02:00
|
|
|
/**
|
|
|
|
* The data type backing the field. Represents the type of value the
|
2016-03-21 11:12:23 +01:00
|
|
|
* form expects to receive via a postback. Should be set in subclasses.
|
2016-03-28 10:52:51 +02:00
|
|
|
*
|
|
|
|
* The values allowed in this list include:
|
|
|
|
*
|
|
|
|
* - String: Single line text
|
|
|
|
* - Hidden: Hidden field which is posted back without modification
|
|
|
|
* - Text: Multi line text
|
|
|
|
* - HTML: Rich html text
|
|
|
|
* - Integer: Whole number value
|
|
|
|
* - Decimal: Decimal value
|
|
|
|
* - MultiSelect: Select many from source
|
|
|
|
* - SingleSelect: Select one from source
|
|
|
|
* - Date: Date only
|
|
|
|
* - DateTime: Date and time
|
|
|
|
* - Time: Time only
|
|
|
|
* - Boolean: Yes or no
|
|
|
|
* - Custom: Custom type declared by the front-end component. For fields with this type,
|
|
|
|
* the component property is mandatory, and will determine the posted value for this field.
|
|
|
|
* - Structural: Represents a field that is NOT posted back. This may contain other fields,
|
|
|
|
* or simply be a block of stand-alone content. As with 'Custom',
|
|
|
|
* the component property is mandatory if this is assigned.
|
|
|
|
*
|
2016-03-21 11:12:23 +01:00
|
|
|
* Each value has an equivalent constant, e.g. {@link self::SCHEMA_DATA_TYPE_STRING}.
|
|
|
|
*
|
2016-03-28 10:52:51 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $schemaDataType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of front-end component to render the FormField as.
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @skipUpgrade
|
2016-03-28 10:52:51 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $schemaComponent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structured schema data representing the FormField.
|
|
|
|
* Used to render the FormField as a ReactJS Component on the front-end.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $schemaData = [];
|
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
private static $casting = array(
|
|
|
|
'FieldHolder' => 'HTMLFragment',
|
|
|
|
'Field' => 'HTMLFragment',
|
2016-07-14 04:43:53 +02:00
|
|
|
'AttributesHTML' => 'HTMLFragment', // property $AttributesHTML version
|
|
|
|
'getAttributesHTML' => 'HTMLFragment', // method $getAttributesHTML($arg) version
|
2016-06-03 10:51:02 +02:00
|
|
|
'Value' => 'Text',
|
|
|
|
'extraClass' => 'Text',
|
|
|
|
'ID' => 'Text',
|
|
|
|
'isReadOnly' => 'Boolean',
|
|
|
|
'HolderID' => 'Text',
|
|
|
|
'Title' => 'Text',
|
|
|
|
'RightTitle' => 'Text',
|
|
|
|
'MessageType' => 'Text',
|
|
|
|
'Message' => 'HTMLFragment',
|
|
|
|
'Description' => 'HTMLFragment',
|
|
|
|
);
|
|
|
|
|
2016-03-28 10:52:51 +02:00
|
|
|
/**
|
|
|
|
* Structured schema state representing the FormField's current data and validation.
|
|
|
|
* Used to render the FormField as a ReactJS Component on the front-end.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $schemaState = [];
|
|
|
|
|
2012-04-12 02:18:55 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Takes a field name and converts camelcase to spaced words. Also resolves combined field
|
|
|
|
* names with dot syntax to spaced words.
|
2012-04-12 02:18:55 +02:00
|
|
|
*
|
|
|
|
* Examples:
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
2012-04-12 02:18:55 +02:00
|
|
|
* - 'TotalAmount' will return 'Total Amount'
|
|
|
|
* - 'Organisation.ZipCode' will return 'Organisation Zip Code'
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
2012-04-12 02:18:55 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function name_to_label($fieldName) {
|
|
|
|
if(strpos($fieldName, '.') !== false) {
|
|
|
|
$parts = explode('.', $fieldName);
|
2015-06-20 14:55:58 +02:00
|
|
|
|
|
|
|
$label = $parts[count($parts) - 2] . ' ' . $parts[count($parts) - 1];
|
2012-04-12 02:18:55 +02:00
|
|
|
} else {
|
|
|
|
$label = $fieldName;
|
|
|
|
}
|
2013-05-26 01:09:03 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
return preg_replace('/([a-z]+)([A-Z])/', '$1 $2', $label);
|
2012-04-12 02:18:55 +02:00
|
|
|
}
|
|
|
|
|
2012-11-15 02:19:57 +01:00
|
|
|
/**
|
|
|
|
* Construct and return HTML tag.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @param string $tag
|
|
|
|
* @param array $attributes
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param null|string $content
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2012-11-15 02:19:57 +01:00
|
|
|
*/
|
|
|
|
public static function create_tag($tag, $attributes, $content = null) {
|
|
|
|
$preparedAttributes = '';
|
2013-05-26 01:06:39 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
foreach($attributes as $attributeKey => $attributeValue) {
|
|
|
|
if(!empty($attributeValue) || $attributeValue === '0' || ($attributeKey == 'value' && $attributeValue !== null)) {
|
|
|
|
$preparedAttributes .= sprintf(
|
|
|
|
' %s="%s"', $attributeKey, Convert::raw2att($attributeValue)
|
|
|
|
);
|
2012-11-15 02:19:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:06:39 +02:00
|
|
|
if($content || $tag != 'input') {
|
2015-06-20 14:55:58 +02:00
|
|
|
return sprintf(
|
|
|
|
'<%s%s>%s</%s>', $tag, $preparedAttributes, $content, $tag
|
|
|
|
);
|
2013-05-26 01:06:39 +02:00
|
|
|
}
|
2015-06-20 14:55:58 +02:00
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'<%s%s />', $tag, $preparedAttributes
|
|
|
|
);
|
2012-11-15 02:19:57 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-10-13 09:59:53 +02:00
|
|
|
* Creates a new field.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @param string $name The internal field name, passed to forms.
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param null|string $title The human-readable field label.
|
2013-05-26 01:06:39 +02:00
|
|
|
* @param mixed $value The value of the field.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $title = null, $value = null) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->setName($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if($title === null) {
|
|
|
|
$this->title = self::name_to_label($name);
|
|
|
|
} else {
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($value !== null) {
|
|
|
|
$this->setValue($value);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
2015-01-14 14:07:41 +01:00
|
|
|
|
|
|
|
$this->setupDefaultClasses();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-20 11:33:55 +02:00
|
|
|
* Set up the default classes for the form. This is done on construct so that the default classes can be removed
|
2015-01-14 14:07:41 +01:00
|
|
|
* after instantiation
|
|
|
|
*/
|
|
|
|
protected function setupDefaultClasses() {
|
|
|
|
$defaultClasses = self::config()->get('default_classes');
|
|
|
|
if ($defaultClasses) {
|
|
|
|
foreach ($defaultClasses as $class) {
|
|
|
|
$this->addExtraClass($class);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
/**
|
2013-05-26 01:06:39 +02:00
|
|
|
* Return a link to this field.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
*
|
|
|
|
* @return string
|
2008-08-09 05:29:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link($action = null) {
|
2009-10-11 02:07:16 +02:00
|
|
|
return Controller::join_links($this->form->FormAction(), 'field/' . $this->name, $action);
|
2008-08-09 05:29:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the HTML ID of the field.
|
2013-05-26 01:09:03 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* The ID is generated as FormName_FieldName. All Field functions should ensure that this ID is
|
|
|
|
* included in the field.
|
2013-05-26 01:09:03 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ID() {
|
2013-05-26 01:09:03 +02:00
|
|
|
return $this->getTemplateHelper()->generateFieldID($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
|
|
|
/**
|
2013-05-26 01:09:03 +02:00
|
|
|
* Returns the HTML ID for the form field holder element.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-26 01:09:03 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function HolderID() {
|
|
|
|
return $this->getTemplateHelper()->generateFieldHolderID($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current {@link FormTemplateHelper} on either the parent
|
|
|
|
* Form or the global helper set through the {@link Injector} layout.
|
|
|
|
*
|
|
|
|
* To customize a single {@link FormField}, use {@link setTemplate} and
|
|
|
|
* provide a custom template name.
|
|
|
|
*
|
|
|
|
* @return FormTemplateHelper
|
|
|
|
*/
|
|
|
|
public function getTemplateHelper() {
|
|
|
|
if($this->form) {
|
|
|
|
return $this->form->getTemplateHelper();
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
return FormTemplateHelper::singleton();
|
2013-05-26 01:09:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the field name.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 12:38:52 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getName() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->name;
|
|
|
|
}
|
2011-05-05 16:48:45 +02:00
|
|
|
|
|
|
|
/**
|
2009-03-17 12:38:52 +01:00
|
|
|
* Returns the field message, used by form validation.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2009-03-17 12:38:52 +01:00
|
|
|
* Use {@link setError()} to set this property.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 12:38:52 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Message() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->message;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the field message type.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Arbitrary value which is mostly used for CSS classes in the rendered HTML, e.g "required".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 12:38:52 +01:00
|
|
|
* Use {@link setError()} to set this property.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-17 12:38:52 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function MessageType() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->messageType;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the field value.
|
|
|
|
*
|
|
|
|
* @return mixed
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Value() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-05-26 01:06:39 +02:00
|
|
|
* Method to save this form field into the given {@link DataObject}.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* By default, makes use of $this->dataValue()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param DataObject|DataObjectInterface $record DataObject to save data into
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->name) {
|
|
|
|
$record->setCastedField($this->name, $this->dataValue());
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the field value suitable for insertion into the data object.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @return mixed
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataValue() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the field label - used by templates.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Title() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->title;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-26 01:06:39 +02:00
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Set the title of this formfield.
|
|
|
|
* Note: This expects escaped HTML.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @param string $title Escaped HTML for title
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2013-05-26 01:06:39 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setTitle($title) {
|
|
|
|
$this->title = $title;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2012-06-01 05:00:21 +02:00
|
|
|
/**
|
|
|
|
* Gets the contextual label than can be used for additional field description.
|
|
|
|
* Can be shown to the right or under the field in question.
|
|
|
|
*
|
|
|
|
* @return string Contextual label text.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function RightTitle() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->rightTitle;
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2012-06-01 05:00:21 +02:00
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Sets the right title for this formfield
|
|
|
|
* Note: This expects escaped HTML.
|
2012-06-01 05:00:21 +02:00
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @param string $rightTitle Escaped HTML for title
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2012-06-01 05:00:21 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setRightTitle($rightTitle) {
|
|
|
|
$this->rightTitle = $rightTitle;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function LeftTitle() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->leftTitle;
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $leftTitle
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setLeftTitle($leftTitle) {
|
|
|
|
$this->leftTitle = $leftTitle;
|
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2016-07-25 07:24:26 +02:00
|
|
|
* Compiles all CSS-classes. Optionally includes a "form-group--no-label" class if no title was set on the
|
2015-06-20 14:55:58 +02:00
|
|
|
* FormField.
|
|
|
|
*
|
|
|
|
* Uses {@link Message()} and {@link MessageType()} to add validation error classes which can
|
|
|
|
* be used to style the contained tags.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function extraClass() {
|
2011-12-22 13:10:57 +01:00
|
|
|
$classes = array();
|
|
|
|
|
|
|
|
$classes[] = $this->Type();
|
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if($this->extraClasses) {
|
|
|
|
$classes = array_merge(
|
|
|
|
$classes,
|
|
|
|
array_values($this->extraClasses)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if(!$this->Title()) {
|
2016-07-25 07:24:26 +02:00
|
|
|
$classes[] = 'form-group--no-label';
|
2015-06-20 14:55:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
// Allow custom styling of any element in the container based on validation errors,
|
|
|
|
// e.g. red borders on input tags.
|
|
|
|
//
|
|
|
|
// CSS class needs to be different from the one rendered through {@link FieldHolder()}.
|
|
|
|
if($this->Message()) {
|
2015-10-26 20:15:27 +01:00
|
|
|
$classes[] .= 'holder-' . $this->MessageType();
|
2015-06-20 14:55:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
return implode(' ', $classes);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Add one or more CSS-classes to the FormField container.
|
|
|
|
*
|
|
|
|
* Multiple class names should be space delimited.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-01-28 08:00:47 +01:00
|
|
|
* @param string $class
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addExtraClass($class) {
|
2013-06-29 12:42:07 +02:00
|
|
|
$classes = preg_split('/\s+/', $class);
|
2014-01-28 08:00:47 +01:00
|
|
|
|
2013-06-29 12:42:07 +02:00
|
|
|
foreach ($classes as $class) {
|
2014-01-28 08:00:47 +01:00
|
|
|
$this->extraClasses[$class] = $class;
|
2013-06-29 12:42:07 +02:00
|
|
|
}
|
2014-01-28 08:00:47 +01:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Remove one or more CSS-classes from the FormField container.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-01-28 08:00:47 +01:00
|
|
|
* @param string $class
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeExtraClass($class) {
|
2013-06-29 12:42:07 +02:00
|
|
|
$classes = preg_split('/\s+/', $class);
|
2014-01-28 08:00:47 +01:00
|
|
|
|
2013-06-29 12:42:07 +02:00
|
|
|
foreach ($classes as $class) {
|
|
|
|
unset($this->extraClasses[$class]);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
/**
|
|
|
|
* Set an HTML attribute on the field element, mostly an <input> tag.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Some attributes are best set through more specialized methods, to avoid interfering with
|
|
|
|
* built-in behaviour:
|
|
|
|
*
|
2012-03-07 17:45:14 +01:00
|
|
|
* - 'class': {@link addExtraClass()}
|
|
|
|
* - 'title': {@link setDescription()}
|
|
|
|
* - 'value': {@link setValue}
|
|
|
|
* - 'name': {@link setName}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Caution: this doesn't work on most fields which are composed of more than one HTML form
|
|
|
|
* field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2011-12-22 13:10:57 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setAttribute($name, $value) {
|
2011-12-22 13:10:57 +01:00
|
|
|
$this->attributes[$name] = $value;
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an HTML attribute defined by the field, or added through {@link setAttribute()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Caution: this doesn't work on all fields, see {@link setAttribute()}.
|
|
|
|
*
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param string $name
|
|
|
|
* @return string
|
2011-12-22 13:10:57 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAttribute($name) {
|
2015-06-20 14:55:58 +02:00
|
|
|
$attributes = $this->getAttributes();
|
|
|
|
|
|
|
|
if(isset($attributes[$name])) {
|
|
|
|
return $attributes[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
/**
|
2015-02-08 07:14:31 +01:00
|
|
|
* Allows customization through an 'updateAttributes' hook on the base class.
|
|
|
|
* Existing attributes are passed in as the first argument and can be manipulated,
|
|
|
|
* but any attributes added through a subclass implementation won't be included.
|
|
|
|
*
|
2011-12-22 13:10:57 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAttributes() {
|
2015-06-20 14:55:58 +02:00
|
|
|
$attributes = array(
|
2011-12-22 13:10:57 +01:00
|
|
|
'type' => 'text',
|
|
|
|
'name' => $this->getName(),
|
2014-08-15 08:53:05 +02:00
|
|
|
'value' => $this->Value(),
|
2011-12-22 13:10:57 +01:00
|
|
|
'class' => $this->extraClass(),
|
|
|
|
'id' => $this->ID(),
|
|
|
|
'disabled' => $this->isDisabled(),
|
2014-03-18 05:00:53 +01:00
|
|
|
'readonly' => $this->isReadonly()
|
2011-12-22 13:10:57 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if($this->Required()) {
|
|
|
|
$attributes['required'] = 'required';
|
|
|
|
$attributes['aria-required'] = 'true';
|
2013-08-11 23:30:31 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 11:33:55 +02:00
|
|
|
$attributes = array_merge($attributes, $this->attributes);
|
2015-02-08 07:14:31 +01:00
|
|
|
|
2015-07-20 11:33:55 +02:00
|
|
|
$this->extend('updateAttributes', $attributes);
|
2015-02-08 07:14:31 +01:00
|
|
|
|
2015-07-20 11:33:55 +02:00
|
|
|
return $attributes;
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Custom attributes to process. Falls back to {@link getAttributes()}.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* If at least one argument is passed as a string, all arguments act as excludes, by name.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param array $attributes
|
|
|
|
*
|
|
|
|
* @return string
|
2011-12-22 13:10:57 +01:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function getAttributesHTML($attributes = null) {
|
|
|
|
$exclude = null;
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if(is_string($attributes)) {
|
|
|
|
$exclude = func_get_args();
|
2013-05-26 01:06:39 +02:00
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if(!$attributes || is_string($attributes)) {
|
|
|
|
$attributes = $this->getAttributes();
|
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
$attributes = (array) $attributes;
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
$attributes = array_filter($attributes, function ($v) {
|
2012-04-20 02:22:40 +02:00
|
|
|
return ($v || $v === 0 || $v === '0');
|
2014-08-15 08:53:05 +02:00
|
|
|
});
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2013-05-26 01:06:39 +02:00
|
|
|
if($exclude) {
|
2015-06-20 14:55:58 +02:00
|
|
|
$attributes = array_diff_key(
|
|
|
|
$attributes,
|
|
|
|
array_flip($exclude)
|
|
|
|
);
|
2013-05-26 01:06:39 +02:00
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2013-05-26 01:06:39 +02:00
|
|
|
// Create markup
|
2011-12-22 13:10:57 +01:00
|
|
|
$parts = array();
|
2013-05-26 01:06:39 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
foreach($attributes as $name => $value) {
|
|
|
|
if($value === true) {
|
|
|
|
$parts[] = sprintf('%s="%s"', $name, $name);
|
|
|
|
} else {
|
|
|
|
$parts[] = sprintf('%s="%s"', $name, Convert::raw2att($value));
|
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' ', $parts);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns a version of a title suitable for insertion into an HTML attribute.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function attrTitle() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return Convert::raw2att($this->title);
|
|
|
|
}
|
2013-05-26 01:06:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns a version of a title suitable for insertion into an HTML attribute.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function attrValue() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return Convert::raw2att($this->value);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the field value.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param mixed $value
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param null|array|DataObject $data {@see Form::loadDataFrom}
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value) {
|
2012-07-28 01:11:03 +02:00
|
|
|
$this->value = $value;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-11-22 04:33:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Set the field name.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2008-11-22 04:33:00 +01:00
|
|
|
$this->name = $name;
|
2013-05-26 01:06:39 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-11-22 04:33:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the container form.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* This is called automatically when fields are added to forms.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param Form $form
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setForm($form) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->form = $form;
|
2013-05-26 01:06:39 +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
|
|
|
|
2008-04-06 05:33:55 +02:00
|
|
|
/**
|
|
|
|
* Get the currently used form.
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getForm() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return $this->form;
|
2008-04-06 05:33:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-27 10:48:42 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Return true if security token protection is enabled on the parent {@link Form}.
|
2009-06-27 10:48:42 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function securityTokenEnabled() {
|
2011-10-29 06:10:02 +02:00
|
|
|
$form = $this->getForm();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-26 01:06:39 +02:00
|
|
|
if(!$form) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-29 06:10:02 +02:00
|
|
|
return $form->getSecurityToken()->isEnabled();
|
2009-06-27 10:48:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Sets the error message to be displayed on the form field.
|
|
|
|
*
|
|
|
|
* Allows HTML content, so remember to use Convert::raw2xml().
|
2014-01-30 22:38:34 +01:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param string $message
|
2013-05-26 01:06:39 +02:00
|
|
|
* @param string $messageType
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setError($message, $messageType) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->message = $message;
|
|
|
|
$this->messageType = $messageType;
|
|
|
|
|
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
|
|
|
|
2009-04-29 01:55:53 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Set the custom error message to show instead of the default format.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Different from setError() as that appends it to the standard error messaging.
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param string $customValidationMessage
|
|
|
|
*
|
|
|
|
* @return $this
|
2009-04-29 01:55:53 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setCustomValidationMessage($customValidationMessage) {
|
|
|
|
$this->customValidationMessage = $customValidationMessage;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-04-29 01:55:53 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 01:55:53 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Get the custom error message for this form field. If a custom message has not been defined
|
|
|
|
* then just return blank. The default error is defined on {@link Validator}.
|
2009-04-29 01:55:53 +02:00
|
|
|
*
|
2012-04-14 07:32:29 +02:00
|
|
|
* @return string
|
2009-04-29 01:55:53 +02:00
|
|
|
*/
|
|
|
|
public function getCustomValidationMessage() {
|
|
|
|
return $this->customValidationMessage;
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2011-04-16 05:09:30 +02:00
|
|
|
/**
|
2011-12-21 17:35:42 +01:00
|
|
|
* Set name of template (without path or extension).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Caution: Not consistently implemented in all subclasses, please check the {@link Field()}
|
|
|
|
* method on the subclass for support.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-26 01:06:39 +02:00
|
|
|
* @param string $template
|
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2011-04-16 05:09:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTemplate($template) {
|
2011-04-16 05:09:30 +02:00
|
|
|
$this->template = $template;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2011-04-16 05:09:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-16 05:09:30 +02:00
|
|
|
/**
|
2012-04-14 07:32:29 +02:00
|
|
|
* @return string
|
2011-04-16 05:09:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTemplate() {
|
2011-04-16 05:09:30 +02:00
|
|
|
return $this->template;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFieldHolderTemplate() {
|
|
|
|
return $this->fieldHolderTemplate;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Set name of template (without path or extension) for the holder, which in turn is
|
|
|
|
* responsible for rendering {@link Field()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Caution: Not consistently implemented in all subclasses, please check the {@link Field()}
|
|
|
|
* method on the subclass for support.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param string $fieldHolderTemplate
|
2013-05-26 01:06:39 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2012-04-14 07:32:29 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setFieldHolderTemplate($fieldHolderTemplate) {
|
|
|
|
$this->fieldHolderTemplate = $fieldHolderTemplate;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSmallFieldHolderTemplate() {
|
|
|
|
return $this->smallFieldHolderTemplate;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Set name of template (without path or extension) for the small holder, which in turn is
|
|
|
|
* responsible for rendering {@link Field()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Caution: Not consistently implemented in all subclasses, please check the {@link Field()}
|
|
|
|
* method on the subclass for support.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param string $smallFieldHolderTemplate
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return $this
|
2012-04-14 07:32:29 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setSmallFieldHolderTemplate($smallFieldHolderTemplate) {
|
|
|
|
$this->smallFieldHolderTemplate = $smallFieldHolderTemplate;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the form field.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Although FieldHolder is generally what is inserted into templates, all of the field holder
|
2015-06-20 14:55:58 +02:00
|
|
|
* templates make use of $Field. It's expected that FieldHolder will give you the "complete"
|
2007-07-19 12:40:28 +02:00
|
|
|
* representation of the field on the form, whereas Field will give you the core editing widget,
|
|
|
|
* such as an input tag.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param array $properties
|
2016-07-12 08:51:08 +02:00
|
|
|
* @return DBHTMLText
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2015-06-20 14:55:58 +02:00
|
|
|
$context = $this;
|
|
|
|
|
|
|
|
if(count($properties)) {
|
|
|
|
$context = $context->customise($properties);
|
|
|
|
}
|
|
|
|
|
2013-11-29 01:20:30 +01:00
|
|
|
$this->extend('onBeforeRender', $this);
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2016-01-06 22:27:46 +01:00
|
|
|
$result = $context->renderWith($this->getTemplates());
|
|
|
|
|
|
|
|
// Trim whitespace from the result, so that trailing newlines are supressed. Works for strings and HTMLText values
|
|
|
|
if(is_string($result)) {
|
|
|
|
$result = trim($result);
|
|
|
|
} else if($result instanceof DBField) {
|
|
|
|
$result->setValue(trim($result->getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns a "field holder" for this field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 05:12:25 +01:00
|
|
|
* Forms are constructed by concatenating a number of these field holders.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
2011-03-23 05:12:25 +01:00
|
|
|
* The default field holder is a label and a form field inside a div.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
2011-03-23 05:12:25 +01:00
|
|
|
* @see FieldHolder.ss
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param array $properties
|
|
|
|
*
|
2016-07-12 08:51:08 +02:00
|
|
|
* @return DBHTMLText
|
2011-03-23 05:12:25 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2015-06-20 14:55:58 +02:00
|
|
|
$context = $this;
|
2012-04-14 07:32:29 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if(count($properties)) {
|
|
|
|
$context = $this->customise($properties);
|
|
|
|
}
|
2012-04-14 07:32:29 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
return $context->renderWith($this->getFieldHolderTemplates());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* Returns a restricted field holder used within things like FieldGroups.
|
|
|
|
*
|
|
|
|
* @param array $properties
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function SmallFieldHolder($properties = array()) {
|
2015-06-20 14:55:58 +02:00
|
|
|
$context = $this;
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
if(count($properties)) {
|
|
|
|
$context = $this->customise($properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context->renderWith($this->getSmallFieldHolderTemplates());
|
2012-04-14 07:32:29 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns an array of templates to use for rendering {@link FieldHolder}.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTemplates() {
|
|
|
|
return $this->_templates($this->getTemplate());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns an array of templates to use for rendering {@link FieldHolder}.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldHolderTemplates() {
|
|
|
|
return $this->_templates(
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->getFieldHolderTemplate(),
|
2012-04-14 07:32:29 +02:00
|
|
|
'_holder'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns an array of templates to use for rendering {@link SmallFieldHolder}.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2012-04-14 07:32:29 +02:00
|
|
|
public function getSmallFieldHolderTemplates() {
|
|
|
|
return $this->_templates(
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->getSmallFieldHolderTemplate(),
|
2012-04-14 07:32:29 +02:00
|
|
|
'_holder_small'
|
|
|
|
);
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Generate an array of class name strings to use for rendering this form field into HTML.
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param string $customTemplate
|
|
|
|
* @param string $customTemplateSuffix
|
2012-04-14 07:32:29 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return array
|
2012-04-14 07:32:29 +02:00
|
|
|
*/
|
2016-08-23 04:32:26 +02:00
|
|
|
protected function _templates($customTemplate = null, $customTemplateSuffix = null) {
|
|
|
|
$templates = SSViewer::get_templates_by_class(get_class($this), $customTemplateSuffix, __CLASS__);
|
|
|
|
// Prefer any custom template
|
2015-06-20 14:55:58 +02:00
|
|
|
if($customTemplate) {
|
2016-08-23 04:32:26 +02:00
|
|
|
// Prioritise direct template
|
|
|
|
array_unshift($templates, $customTemplate);
|
2012-04-14 07:32:29 +02:00
|
|
|
}
|
2016-08-23 04:32:26 +02:00
|
|
|
return $templates;
|
2012-04-14 07:32:29 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this field is a composite field.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* To create composite field types, you should subclass {@link CompositeField}.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isComposite() {
|
2011-03-23 05:12:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this field has its own data.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* Some fields, such as titles and composite fields, don't actually have any data. It doesn't
|
|
|
|
* make sense for data-focused methods to look at them. By overloading hasData() to return
|
|
|
|
* false, you can prevent any data-focused methods from looking at it.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @see FieldList::collateDataFields()
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function hasData() {
|
2011-03-23 05:12:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return bool
|
2008-08-12 04:58:48 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isReadonly() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return $this->readonly;
|
2008-08-12 04:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Sets a read-only flag on this FormField.
|
|
|
|
*
|
|
|
|
* Use performReadonlyTransformation() to transform this instance.
|
|
|
|
*
|
|
|
|
* Setting this to false has no effect on the field.
|
|
|
|
*
|
|
|
|
* @param bool $readonly
|
|
|
|
*
|
|
|
|
* @return $this
|
2008-08-12 04:58:48 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setReadonly($readonly) {
|
|
|
|
$this->readonly = $readonly;
|
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-08-12 04:58:48 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return bool
|
2008-08-12 04:58:48 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isDisabled() {
|
2012-04-12 02:18:55 +02:00
|
|
|
return $this->disabled;
|
2008-08-12 04:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Sets a disabled flag on this FormField.
|
|
|
|
*
|
|
|
|
* Use performDisabledTransformation() to transform this instance.
|
|
|
|
*
|
|
|
|
* Setting this to false has no effect on the field.
|
|
|
|
*
|
|
|
|
* @param bool $disabled
|
|
|
|
*
|
|
|
|
* @return $this
|
2008-08-12 04:58:48 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setDisabled($disabled) {
|
|
|
|
$this->disabled = $disabled;
|
|
|
|
|
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-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns a read-only version of this field.
|
|
|
|
*
|
|
|
|
* @return FormField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2015-07-20 11:33:55 +02:00
|
|
|
$readonlyClassName = $this->class . '_Readonly';
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2013-04-05 11:08:17 +02:00
|
|
|
if(ClassInfo::exists($readonlyClassName)) {
|
|
|
|
$clone = $this->castedCopy($readonlyClassName);
|
|
|
|
} else {
|
2016-08-19 00:51:35 +02:00
|
|
|
$clone = $this->castedCopy('SilverStripe\\Forms\\ReadonlyField');
|
2015-07-20 11:33:55 +02:00
|
|
|
}
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2015-07-20 11:33:55 +02:00
|
|
|
$clone->setReadonly(true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-05 11:08:17 +02:00
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-04-12 02:18:55 +02:00
|
|
|
* Return a disabled version of this field.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* Tries to find a class of the class name of this field suffixed with "_Disabled", failing
|
|
|
|
* that, finds a method {@link setDisabled()}.
|
2012-04-12 02:18:55 +02:00
|
|
|
*
|
|
|
|
* @return FormField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performDisabledTransformation() {
|
2012-12-13 13:51:28 +01:00
|
|
|
$disabledClassName = $this->class . '_Disabled';
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2012-04-12 02:18:55 +02:00
|
|
|
if(ClassInfo::exists($disabledClassName)) {
|
2012-12-13 13:51:28 +01:00
|
|
|
$clone = $this->castedCopy($disabledClassName);
|
2012-04-12 02:18:55 +02:00
|
|
|
} else {
|
2012-12-13 13:51:28 +01:00
|
|
|
$clone = clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-12-13 13:51:28 +01:00
|
|
|
|
2015-07-20 11:33:55 +02:00
|
|
|
$clone->setDisabled(true);
|
2012-04-12 02:18:55 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
return $clone;
|
|
|
|
}
|
2012-04-12 02:18:55 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
/**
|
|
|
|
* @param FormTransformation $transformation
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function transform(FormTransformation $transformation) {
|
|
|
|
return $transformation->transform($this);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function hasClass($class) {
|
|
|
|
$patten = '/' . strtolower($class) . '/i';
|
|
|
|
|
|
|
|
$subject = strtolower($this->class . ' ' . $this->extraClass());
|
|
|
|
|
2007-11-23 02:10:19 +01:00
|
|
|
return preg_match($patten, $subject);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns the field type.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* The field type is the class name with the word Field dropped off the end, all lowercase.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* It's handy for assigning HTML classes. Doesn't signify the <input type> attribute.
|
|
|
|
*
|
|
|
|
* @see {link getAttributes()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 05:12:25 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$type = new ReflectionClass($this);
|
|
|
|
return strtolower(preg_replace('/Field$/', '', $type->getShortName()));
|
2011-03-23 05:12:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 10:00:51 +01:00
|
|
|
/**
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use FormField::create_tag()
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @param string $tag
|
|
|
|
* @param array $attributes
|
|
|
|
* @param null|string $content
|
|
|
|
*
|
|
|
|
* @return string
|
2013-03-20 10:00:51 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function createTag($tag, $attributes, $content = null) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use FormField::create_tag()');
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2012-11-15 02:19:57 +01:00
|
|
|
return self::create_tag($tag, $attributes, $content);
|
2015-06-19 01:59:27 +02:00
|
|
|
}
|
2012-04-12 02:18:55 +02:00
|
|
|
|
2008-03-06 03:06:47 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Abstract method each {@link FormField} subclass must implement, determines whether the field
|
|
|
|
* is valid or not based on the value.
|
|
|
|
*
|
2012-04-12 02:18:55 +02:00
|
|
|
* @todo Make this abstract.
|
|
|
|
*
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param Validator $validator
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return bool
|
2008-03-06 03:06:47 +01:00
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2009-02-02 00:49:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Describe this field, provide help text for it.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* By default, renders as a <span class="description"> underneath the form field.
|
|
|
|
*
|
|
|
|
* @param string $description
|
|
|
|
*
|
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setDescription($description) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->description = $description;
|
2015-06-20 14:55:58 +02:00
|
|
|
|
2012-04-12 02:18:55 +02:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-02-16 21:47:33 +01:00
|
|
|
|
|
|
|
/**
|
2012-04-14 07:32:29 +02:00
|
|
|
* @return string
|
2012-02-16 21:47:33 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDescription() {
|
2012-02-16 21:47:33 +01:00
|
|
|
return $this->description;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2015-06-20 14:55:58 +02:00
|
|
|
return sprintf(
|
|
|
|
'%s (%s: %s : <span style="color:red;">%s</span>) = %s',
|
|
|
|
$this->class,
|
|
|
|
$this->name,
|
|
|
|
$this->title,
|
|
|
|
$this->message,
|
|
|
|
$this->value
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* This function is used by the template processor. If you refer to a field as a $ variable, it
|
2007-07-19 12:40:28 +02:00
|
|
|
* will return the $Field value.
|
2015-06-20 14:55:58 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function forTemplate() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->Field();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-17 12:38:52 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* @return bool
|
2009-03-17 12:38:52 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Required() {
|
2016-07-06 06:34:09 +02:00
|
|
|
if($this->form && ($validator = $this->form->getValidator())) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $validator->fieldIsRequired($this->name);
|
|
|
|
}
|
2015-06-20 14:55:58 +02:00
|
|
|
|
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-04-12 02:18:55 +02:00
|
|
|
|
2008-09-11 02:02:49 +02:00
|
|
|
/**
|
2012-05-14 04:42:54 +02:00
|
|
|
* Set the FieldList that contains this field.
|
2008-09-11 02:02:49 +02:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param FieldList $containerFieldList
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return $this
|
2012-05-14 04:42:54 +02:00
|
|
|
*/
|
2015-06-20 14:55:58 +02:00
|
|
|
public function setContainerFieldList($containerFieldList) {
|
|
|
|
$this->containerFieldList = $containerFieldList;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-09-11 02:02:49 +02:00
|
|
|
}
|
2012-05-14 04:42:54 +02:00
|
|
|
|
2014-05-28 03:58:40 +02:00
|
|
|
/**
|
|
|
|
* Get the FieldList that contains this field.
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getContainerFieldList() {
|
|
|
|
return $this->containerFieldList;
|
|
|
|
}
|
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
/**
|
|
|
|
* @return null|FieldList
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function rootFieldList() {
|
2015-06-20 14:55:58 +02:00
|
|
|
if(is_object($this->containerFieldList)) {
|
|
|
|
return $this->containerFieldList->rootFieldList();
|
|
|
|
}
|
|
|
|
|
|
|
|
user_error(
|
|
|
|
"rootFieldList() called on $this->class object without a containerFieldList",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
2008-09-11 02:02:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
/**
|
2015-06-20 14:55:58 +02:00
|
|
|
* Returns another instance of this field, but "cast" to a different class. The logic tries to
|
|
|
|
* retain all of the instance properties, and may be overloaded by subclasses to set additional
|
|
|
|
* ones.
|
|
|
|
*
|
|
|
|
* Assumes the standard FormField parameter signature with its name as the only mandatory
|
|
|
|
* argument. Mainly geared towards creating *_Readonly or *_Disabled subclasses of the same
|
|
|
|
* type, or casting to a {@link ReadonlyField}.
|
2012-12-13 13:51:28 +01:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* Does not copy custom field templates, since they probably won't apply to the new instance.
|
2012-12-13 13:51:28 +01:00
|
|
|
*
|
2015-06-20 14:55:58 +02:00
|
|
|
* @param mixed $classOrCopy Class name for copy, or existing copy instance to update
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-12-13 13:51:28 +01:00
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function castedCopy($classOrCopy) {
|
2015-06-20 14:55:58 +02:00
|
|
|
$field = $classOrCopy;
|
|
|
|
|
|
|
|
if(!is_object($field)) {
|
|
|
|
$field = new $classOrCopy($this->name);
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
$field
|
2015-06-20 14:55:58 +02:00
|
|
|
->setValue($this->value)
|
2012-12-13 13:51:28 +01:00
|
|
|
->setForm($this->form)
|
|
|
|
->setTitle($this->Title())
|
|
|
|
->setLeftTitle($this->LeftTitle())
|
|
|
|
->setRightTitle($this->RightTitle())
|
2016-07-25 07:24:26 +02:00
|
|
|
->addExtraClass($this->extraClass) // Don't use extraClass(), since this merges calculated values
|
2012-12-13 13:51:28 +01:00
|
|
|
->setDescription($this->getDescription());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 14:55:58 +02:00
|
|
|
// Only include built-in attributes, ignore anything set through getAttributes().
|
|
|
|
// Those might change important characteristics of the field, e.g. its "type" attribute.
|
|
|
|
foreach($this->attributes as $attributeKey => $attributeValue) {
|
|
|
|
$field->setAttribute($attributeKey, $attributeValue);
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
return $field;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-28 10:52:51 +02:00
|
|
|
/**
|
|
|
|
* Sets the component type the FormField will be rendered as on the front-end.
|
|
|
|
*
|
|
|
|
* @param string $componentType
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function setSchemaComponent($componentType) {
|
|
|
|
$this->schemaComponent = $componentType;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the type of front-end component the FormField will be rendered as.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSchemaComponent() {
|
|
|
|
return $this->schemaComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the schema data used for rendering the field on the front-end.
|
|
|
|
* Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}.
|
|
|
|
* Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored.
|
|
|
|
* If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`.
|
|
|
|
*
|
|
|
|
* @param array $schemaData - The data to be merged with $this->schemaData.
|
|
|
|
* @return FormField
|
|
|
|
*
|
|
|
|
* @todo Add deep merging of arrays like `data` and `attributes`.
|
|
|
|
*/
|
|
|
|
public function setSchemaData($schemaData = []) {
|
|
|
|
$current = $this->getSchemaData();
|
|
|
|
|
|
|
|
$this->schemaData = array_merge($current, array_intersect_key($schemaData, $current));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the schema data used to render the FormField on the front-end.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSchemaData() {
|
|
|
|
return array_merge($this->getSchemaDataDefaults(), $this->schemaData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Throw exception if value is missing, once a form field schema is mandatory across the CMS
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSchemaDataType() {
|
|
|
|
return $this->schemaDataType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the defaults for $schemaData.
|
|
|
|
* The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaData()} are ignored.
|
|
|
|
* Instead the `data` array should be used to pass around ad hoc data.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSchemaDataDefaults() {
|
|
|
|
return [
|
|
|
|
'name' => $this->getName(),
|
|
|
|
'id' => $this->ID(),
|
|
|
|
'type' => $this->getSchemaDataType(),
|
|
|
|
'component' => $this->getSchemaComponent(),
|
2016-07-06 06:34:09 +02:00
|
|
|
'holder_id' => $this->HolderID(),
|
2016-03-28 10:52:51 +02:00
|
|
|
'title' => $this->Title(),
|
|
|
|
'source' => null,
|
2016-07-06 06:34:09 +02:00
|
|
|
'extraClass' => $this->extraClass(),
|
2016-09-20 02:56:45 +02:00
|
|
|
'description' => $this->obj('Description')->getSchemaValue(),
|
2016-03-28 10:52:51 +02:00
|
|
|
'rightTitle' => $this->RightTitle(),
|
|
|
|
'leftTitle' => $this->LeftTitle(),
|
2016-07-06 06:34:09 +02:00
|
|
|
'readOnly' => $this->isReadonly(),
|
2016-03-28 10:52:51 +02:00
|
|
|
'disabled' => $this->isDisabled(),
|
|
|
|
'customValidationMessage' => $this->getCustomValidationMessage(),
|
|
|
|
'attributes' => [],
|
|
|
|
'data' => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the schema data used for rendering the field on the front-end.
|
|
|
|
* Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}.
|
|
|
|
* Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored.
|
|
|
|
* If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`.
|
|
|
|
*
|
2016-07-06 06:34:09 +02:00
|
|
|
* @param array $schemaState The data to be merged with $this->schemaData.
|
2016-03-28 10:52:51 +02:00
|
|
|
* @return FormField
|
|
|
|
*
|
|
|
|
* @todo Add deep merging of arrays like `data` and `attributes`.
|
|
|
|
*/
|
|
|
|
public function setSchemaState($schemaState = []) {
|
|
|
|
$current = $this->getSchemaState();
|
|
|
|
|
|
|
|
$this->schemaState = array_merge($current, array_intersect_key($schemaState, $current));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the schema state used to render the FormField on the front-end.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSchemaState() {
|
|
|
|
return array_merge($this->getSchemaStateDefaults(), $this->schemaState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the defaults for $schemaState.
|
|
|
|
* The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaState()} are ignored.
|
|
|
|
* Instead the `data` array should be used to pass around ad hoc data.
|
|
|
|
* Includes validation data if the field is associated to a {@link Form},
|
|
|
|
* and {@link Form->validate()} has been called.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSchemaStateDefaults() {
|
|
|
|
$field = $this;
|
|
|
|
$form = $this->getForm();
|
|
|
|
$validator = $form ? $form->getValidator() : null;
|
|
|
|
$errors = $validator ? (array)$validator->getErrors() : [];
|
|
|
|
$messages = array_filter(array_map(function($error) use ($field) {
|
|
|
|
if($error['fieldName'] === $field->getName()) {
|
|
|
|
return [
|
|
|
|
'value' => $error['message'],
|
|
|
|
'type' => $error['messageType']
|
|
|
|
];
|
|
|
|
}
|
2016-07-06 06:34:09 +02:00
|
|
|
return null;
|
2016-03-28 10:52:51 +02:00
|
|
|
}, $errors));
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $this->ID(),
|
|
|
|
'value' => $this->Value(),
|
|
|
|
'valid' => (count($messages) === 0),
|
|
|
|
'messages' => (array)$messages,
|
|
|
|
'data' => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2012-02-27 22:14:02 +01:00
|
|
|
}
|