mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API Add FormField::canSubmitValue()
API Add HTMLText::getProcessShortcodes() / setProcessShortcodes() API Split TextareaField::Value() into ValueEntities() with shortcodes disabled
This commit is contained in:
parent
8e5f786b8d
commit
f43a91a4f8
@ -191,9 +191,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
self::$is_running_test = true;
|
self::$is_running_test = true;
|
||||||
|
|
||||||
// i18n needs to be set to the defaults or tests fail
|
// i18n needs to be set to the defaults or tests fail
|
||||||
i18n::set_locale(i18n::default_locale());
|
i18n::set_locale(i18n::config()->default_locale);
|
||||||
i18n::config()->date_format = null;
|
i18n::config()->date_format = 'yyyy-MM-dd';
|
||||||
i18n::config()->time_format = null;
|
i18n::config()->time_format = 'H:mm';
|
||||||
|
|
||||||
// Set default timezone consistently to avoid NZ-specific dependencies
|
// Set default timezone consistently to avoid NZ-specific dependencies
|
||||||
date_default_timezone_set('UTC');
|
date_default_timezone_set('UTC');
|
||||||
|
@ -47,14 +47,23 @@ class FieldList extends ArrayList {
|
|||||||
/**
|
/**
|
||||||
* Return a sequential set of all fields that have data. This excludes wrapper composite fields
|
* Return a sequential set of all fields that have data. This excludes wrapper composite fields
|
||||||
* as well as heading / help text fields.
|
* as well as heading / help text fields.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function dataFields() {
|
public function dataFields() {
|
||||||
if(!$this->sequentialSet) $this->collateDataFields($this->sequentialSet);
|
if(!$this->sequentialSet) {
|
||||||
|
$this->collateDataFields($this->sequentialSet);
|
||||||
|
}
|
||||||
return $this->sequentialSet;
|
return $this->sequentialSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function saveableFields() {
|
public function saveableFields() {
|
||||||
if(!$this->sequentialSaveableSet) $this->collateDataFields($this->sequentialSaveableSet, true);
|
if(!$this->sequentialSaveableSet) {
|
||||||
|
$this->collateDataFields($this->sequentialSaveableSet, true);
|
||||||
|
}
|
||||||
return $this->sequentialSaveableSet;
|
return $this->sequentialSaveableSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,13 +73,20 @@ class FieldList extends ArrayList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function collateDataFields(&$list, $saveableOnly = false) {
|
protected function collateDataFields(&$list, $saveableOnly = false) {
|
||||||
|
// Initialise list
|
||||||
|
if (!isset($list)) {
|
||||||
|
$list = array();
|
||||||
|
}
|
||||||
|
/** @var FormField $field */
|
||||||
foreach($this as $field) {
|
foreach($this as $field) {
|
||||||
if($field->isComposite()) $field->collateDataFields($list, $saveableOnly);
|
if($field->isComposite()) {
|
||||||
|
$field->collateDataFields($list, $saveableOnly);
|
||||||
|
}
|
||||||
|
|
||||||
if($saveableOnly) {
|
if($saveableOnly) {
|
||||||
$isIncluded = ($field->hasData() && !$field->isReadonly() && !$field->isDisabled());
|
$isIncluded = $field->canSubmitValue();
|
||||||
} else {
|
} else {
|
||||||
$isIncluded = ($field->hasData());
|
$isIncluded = $field->hasData();
|
||||||
}
|
}
|
||||||
if($isIncluded) {
|
if($isIncluded) {
|
||||||
$name = $field->getName();
|
$name = $field->getName();
|
||||||
|
@ -355,18 +355,8 @@ class Form extends RequestHandler {
|
|||||||
$vars = $request->requestVars();
|
$vars = $request->requestVars();
|
||||||
}
|
}
|
||||||
|
|
||||||
// construct an array of allowed fields that can be populated from request data.
|
// Ensure we only process saveable fields (non structural, readonly, or disabled)
|
||||||
// readonly or disabled fields should not be loading data from requests
|
$allowedFields = array_keys($this->Fields()->saveableFields());
|
||||||
$allowedFields = array();
|
|
||||||
$dataFields = $this->Fields()->dataFields();
|
|
||||||
if ($dataFields) {
|
|
||||||
/** @var FormField $field */
|
|
||||||
foreach ($this->Fields()->dataFields() as $name => $field) {
|
|
||||||
if (!$field->isReadonly() && !$field->isDisabled()) {
|
|
||||||
$allowedFields[] = $name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Populate the form
|
// Populate the form
|
||||||
$this->loadDataFrom($vars, true, $allowedFields);
|
$this->loadDataFrom($vars, true, $allowedFields);
|
||||||
@ -1392,7 +1382,7 @@ class Form extends RequestHandler {
|
|||||||
* For backwards compatibility reasons, this parameter can also be set to === true, which is the same as passing
|
* For backwards compatibility reasons, this parameter can also be set to === true, which is the same as passing
|
||||||
* CLEAR_MISSING
|
* CLEAR_MISSING
|
||||||
*
|
*
|
||||||
* @param FieldList $fieldList An optional list of fields to process. This can be useful when you have a
|
* @param array $fieldList An optional list of fields to process. This can be useful when you have a
|
||||||
* form that has some fields that save to one object, and some that save to another.
|
* form that has some fields that save to one object, and some that save to another.
|
||||||
* @return Form
|
* @return Form
|
||||||
*/
|
*/
|
||||||
|
@ -1277,4 +1277,13 @@ class FormField extends RequestHandler {
|
|||||||
return $field;
|
return $field;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the value of this formfield accepts front-end submitted values and is saveable.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function canSubmitValue() {
|
||||||
|
return $this->hasData() && !$this->isReadonly() && !$this->isDisabled();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,6 @@ class HtmlEditorField extends TextareaField {
|
|||||||
*/
|
*/
|
||||||
private static $sanitise_server_side = false;
|
private static $sanitise_server_side = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* @config
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private static $casting = array(
|
|
||||||
'Value' => 'HTMLText',
|
|
||||||
);
|
|
||||||
|
|
||||||
protected $rows = 30;
|
protected $rows = 30;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,8 +19,13 @@
|
|||||||
*/
|
*/
|
||||||
class TextareaField extends FormField {
|
class TextareaField extends FormField {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Value should be XML
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private static $casting = array(
|
private static $casting = array(
|
||||||
'Value' => 'HTMLText',
|
'Value' => 'HTMLText(array(\'shortcodes\' => false))',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,7 +98,9 @@ class TextareaField extends FormField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* Return value with all values encoded in html entities
|
||||||
|
*
|
||||||
|
* @return string Raw HTML
|
||||||
*/
|
*/
|
||||||
public function Value() {
|
public function Value() {
|
||||||
return htmlentities($this->value, ENT_COMPAT, 'UTF-8');
|
return htmlentities($this->value, ENT_COMPAT, 'UTF-8');
|
||||||
|
@ -34,6 +34,26 @@ class HTMLText extends Text {
|
|||||||
|
|
||||||
protected $processShortcodes = true;
|
protected $processShortcodes = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if shortcodes are enabled
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function getProcessShortcodes() {
|
||||||
|
return $this->processShortcodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set shortcodes on or off by default
|
||||||
|
*
|
||||||
|
* @param bool $process
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setProcessShortcodes($process) {
|
||||||
|
$this->processShortcodes = (bool)$process;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected $whitelist = false;
|
protected $whitelist = false;
|
||||||
|
|
||||||
public function __construct($name = null, $options = array()) {
|
public function __construct($name = null, $options = array()) {
|
||||||
|
@ -16,7 +16,7 @@ class TextareaFieldTest extends SapphireTest {
|
|||||||
/**
|
/**
|
||||||
* Quick smoke test to ensure that text with special html chars is being displayed properly in readonly fields.
|
* Quick smoke test to ensure that text with special html chars is being displayed properly in readonly fields.
|
||||||
*/
|
*/
|
||||||
public function testReadonlyDisplaySepcialHTML() {
|
public function testReadonlyDisplaySpecialHTML() {
|
||||||
$inputText = "These are some special <html> chars including 'single' & \"double\" quotations";
|
$inputText = "These are some special <html> chars including 'single' & \"double\" quotations";
|
||||||
$field = new TextareaField("Test", "Test");
|
$field = new TextareaField("Test", "Test");
|
||||||
$field = $field->performReadonlyTransformation();
|
$field = $field->performReadonlyTransformation();
|
||||||
@ -25,4 +25,22 @@ class TextareaFieldTest extends SapphireTest {
|
|||||||
. ' "double" quotations', $field->Field());
|
. ' "double" quotations', $field->Field());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValueEntities() {
|
||||||
|
$inputText = "These <b>are</b> some unicodes: äöü";
|
||||||
|
$field = new TextareaField("Test", "Test");
|
||||||
|
$field->setValue($inputText);
|
||||||
|
|
||||||
|
// Value should be safe-encoding only, but ValueEntities should be more aggressive
|
||||||
|
$this->assertEquals(
|
||||||
|
"These <b>are</b> some unicodes: äöü",
|
||||||
|
$field->obj('Value')->forTemplate()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Shortcodes are disabled
|
||||||
|
$this->assertEquals(
|
||||||
|
false,
|
||||||
|
$field->obj('Value')->getProcessShortcodes()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user