ENHANCEMENT Fluent interface in Form API by returning instance from all setters

This commit is contained in:
Ingo Schommer 2012-02-17 13:35:26 +01:00
parent f2717f0788
commit 7602d081a2
30 changed files with 154 additions and 4 deletions

View File

@ -12,6 +12,7 @@ class CheckboxField extends FormField {
function setValue($value) {
$this->value = ($value) ? 1 : 0;
return $this;
}
function dataValue() {

View File

@ -135,6 +135,7 @@ class CheckboxSetField extends OptionsetField {
function setDisabled($val) {
$this->disabled = $val;
return $this;
}
/**
@ -146,6 +147,7 @@ class CheckboxSetField extends OptionsetField {
*/
function setDefaultItems($items) {
$this->defaultItems = $items;
return $this;
}
/**
@ -166,6 +168,8 @@ class CheckboxSetField extends OptionsetField {
}
parent::setValue($value, $obj);
return $this;
}
/**

View File

@ -340,10 +340,12 @@ JS;
function setController($controller) {
$this->controller = $controller;
return $this;
}
function setTemplatePopup($template) {
$this->templatePopup = $template;
return $this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -448,6 +450,7 @@ JS;
*/
function setRelationAutoSetting($value) {
Deprecation::notice('3.0', 'Manipulate the DataList instead.');
return $this;
}
/**

View File

@ -62,6 +62,7 @@ class CompositeField extends FormField {
public function setID($id) {
$this->id = $id;
return $this;
}
public function Field() {
@ -81,6 +82,7 @@ class CompositeField extends FormField {
*/
public function setChildren($children) {
$this->children = $children;
return $this;
}
function extraClasses() {
@ -165,14 +167,21 @@ class CompositeField extends FormField {
function setForm($form) {
foreach($this->children as $f) if(is_object($f)) $f->setForm($form);
parent::setForm($form);
return $this;
}
function setColumnCount($columnCount) {
$this->columnCount = $columnCount;
return $this;
}
function isComposite() { return true; }
function hasData() { return false; }
function isComposite() {
return true;
}
function hasData() {
return false;
}
public function fieldByName($name) {
return $this->children->fieldByName($name);

View File

@ -138,6 +138,7 @@ class ConfirmedPasswordField extends FormField {
*/
function setCanBeEmpty($value) {
$this->canBeEmpty = (bool)$value;
return $this;
}
/**
@ -149,6 +150,7 @@ class ConfirmedPasswordField extends FormField {
*/
public function setShowOnClickTitle($title) {
$this->showOnClickTitle = $title;
return $this;
}
/**
@ -162,6 +164,7 @@ class ConfirmedPasswordField extends FormField {
foreach($this->children as $field) {
$field->setRightTitle($title);
}
return $this;
}
/**
@ -176,6 +179,7 @@ class ConfirmedPasswordField extends FormField {
}
}
}
return $this;
}
/**
@ -196,6 +200,8 @@ class ConfirmedPasswordField extends FormField {
}
$this->children->fieldByName($this->getName() . '[_Password]')->setValue($this->value);
$this->children->fieldByName($this->getName() . '[_ConfirmPassword]')->setValue($this->value);
return $this;
}
function jsValidation() {

View File

@ -15,6 +15,7 @@ class CurrencyField extends TextField {
function setValue($val) {
if(!$val) $val = 0.00;
$this->value = '$' . number_format((double)preg_replace('/[^0-9.\-]/', '', $val), 2);
return $this;
}
/**
* Overwrite the datavalue before saving to the db ;-)

View File

@ -58,6 +58,7 @@ class DatalessField extends FormField {
*/
function setAllowHTML($bool) {
$this->allowHTML = $bool;
return $this;
}
/**

View File

@ -229,6 +229,8 @@ class DateField extends TextField {
}
}
}
return $this;
}
/**
@ -470,6 +472,7 @@ JS;
*/
function setLocale($locale) {
$this->locale = $locale;
return $this;
}
/**
@ -493,6 +496,7 @@ JS;
}
$this->config[$name] = $val;
return $this;
}
/**

View File

@ -70,6 +70,8 @@ class DatetimeField extends FormField {
$this->dateField->setForm($form);
$this->timeField->setForm($form);
$this->timezoneField->setForm($form);
return $this;
}
function FieldHolder() {
@ -172,6 +174,8 @@ class DatetimeField extends FormField {
$this->timeField->setValue($valueObj->get($this->timeField->getConfig('timeformat'), $this->locale));
}
}
return $this;
}
function Value() {
@ -187,6 +191,7 @@ class DatetimeField extends FormField {
$this->dateField->setDisabled($bool);
$this->timeField->setDisabled($bool);
if($this->timezoneField) $this->timezoneField->setDisabled($bool);
return $this;
}
function setReadonly($bool) {
@ -194,6 +199,7 @@ class DatetimeField extends FormField {
$this->dateField->setReadonly($bool);
$this->timeField->setReadonly($bool);
if($this->timezoneField) $this->timezoneField->setReadonly($bool);
return $this;
}
/**
@ -220,6 +226,7 @@ class DatetimeField extends FormField {
function setLocale($locale) {
$this->dateField->setLocale($locale);
$this->timeField->setLocale($locale);
return $this;
}
function getLocale() {
@ -240,6 +247,8 @@ class DatetimeField extends FormField {
$this->timezoneField->setValue($val);
$this->setValue($this->dataValue());
}
return $this;
}
/**

View File

@ -194,6 +194,7 @@ class DropdownField extends FormField {
*/
function setSource($source) {
$this->source = $source;
return $this;
}
/**
@ -201,6 +202,7 @@ class DropdownField extends FormField {
*/
function setHasEmptyDefault($bool) {
$this->hasEmptyDefault = $bool;
return $this;
}
/**
@ -220,6 +222,7 @@ class DropdownField extends FormField {
function setEmptyString($str) {
$this->setHasEmptyDefault(true);
$this->emptyString = $str;
return $this;
}
/**
@ -242,5 +245,6 @@ class DropdownField extends FormField {
*/
function setDisabled($disabled = true) {
$this->disabled = $disabled;
return $this;
}
}

View File

@ -121,6 +121,7 @@ class FieldGroup extends CompositeField {
function setZebra($zebra) {
if($zebra == 'odd' || $zebra == 'even') $this->zebra = $zebra;
else user_error("setZebra passed '$zebra'. It should be passed 'odd' or 'even'", E_USER_WARNING);
return $this;
}
function FieldHolder() {

View File

@ -401,6 +401,7 @@ class FieldList extends ArrayList {
*/
public function setForm($form) {
foreach($this as $field) $field->setForm($form);
return $this;
}
/**
@ -413,6 +414,7 @@ class FieldList extends ArrayList {
$fieldName = $field->getName();
if(isset($data[$fieldName])) $field->setValue($data[$fieldName]);
}
return $this;
}
/**
@ -458,6 +460,7 @@ class FieldList extends ArrayList {
function setContainerField($field) {
$this->containerField = $field;
return $this;
}
/**

View File

@ -424,6 +424,7 @@ class Form extends RequestHandler {
*/
public function setRedirectToFormOnValidationError($bool) {
$this->redirectToFormOnValidationError = $bool;
return $this;
}
/**
@ -483,6 +484,7 @@ class Form extends RequestHandler {
$this->validator = $validator;
$this->validator->setForm($this);
}
return $this;
}
/**
@ -562,6 +564,7 @@ class Form extends RequestHandler {
*/
function setFields($fields) {
$this->fields = $fields;
return $this;
}
/**
@ -598,6 +601,7 @@ class Form extends RequestHandler {
*/
function setActions($actions) {
$this->actions = $actions;
return $this;
}
/**
@ -605,6 +609,7 @@ class Form extends RequestHandler {
*/
function unsetAllActions(){
$this->actions = new FieldList();
return $this;
}
/**
@ -625,6 +630,7 @@ class Form extends RequestHandler {
*/
function setAttribute($name, $value) {
$this->attributes[$name] = $value;
return $this;
}
/**
@ -723,6 +729,7 @@ class Form extends RequestHandler {
*/
function setTarget($target) {
$this->target = $target;
return $this;
}
/**
@ -731,6 +738,7 @@ class Form extends RequestHandler {
*/
function setLegend($legend) {
$this->legend = $legend;
return $this;
}
/**
@ -741,6 +749,7 @@ class Form extends RequestHandler {
*/
function setTemplate($template) {
$this->template = $template;
return $this;
}
/**
@ -784,6 +793,7 @@ class Form extends RequestHandler {
*/
public function setEncType($encType) {
$this->encType = $encType;
return $this;
}
/**
@ -831,6 +841,7 @@ class Form extends RequestHandler {
*/
function setFormMethod($method) {
$this->formMethod = strtolower($method);
return $this;
}
/**
@ -860,6 +871,7 @@ class Form extends RequestHandler {
*/
function setFormAction($path) {
$this->formActionPath = $path;
return $this;
}
/**
@ -947,6 +959,7 @@ class Form extends RequestHandler {
function setMessage($message, $type) {
$this->message = $message;
$this->messageType = $type;
return $this;
}
/**
@ -1283,6 +1296,7 @@ class Form extends RequestHandler {
*/
function setButtonClicked($funcName) {
$this->buttonClickedFunc = $funcName;
return $this;
}
function buttonClicked() {
@ -1306,6 +1320,7 @@ class Form extends RequestHandler {
*/
function disableDefaultAction() {
$this->hasDefaultAction = false;
return $this;
}
/**
@ -1317,6 +1332,7 @@ class Form extends RequestHandler {
*/
function disableSecurityToken() {
$this->securityToken = new NullSecurityToken();
return $this;
}
/**
@ -1326,6 +1342,7 @@ class Form extends RequestHandler {
*/
function enableSecurityToken() {
$this->securityToken = new SecurityToken();
return $this;
}
/**
@ -1417,6 +1434,8 @@ class Form extends RequestHandler {
$this->extraClasses[] = $value;
}
return $this;
}
/**
@ -1428,6 +1447,7 @@ class Form extends RequestHandler {
function removeExtraClass($class) {
$classes = explode(' ', $class);
$this->extraClasses = array_diff($this->extraClasses, $classes);
return $this;
}
function debug() {

View File

@ -39,6 +39,7 @@ class FormAction extends FormField {
*/
function setButtonContent($content) {
$this->buttonContent = (string) $content;
return $this;
}
@ -67,6 +68,7 @@ class FormAction extends FormField {
*/
function setFullAction($fullAction) {
$this->action = $fullAction;
return $this;
}
function Field($properties = array()) {

View File

@ -200,6 +200,7 @@ class FormField extends RequestHandler {
function setTitle($val) {
$this->title = $val;
return $this;
}
function RightTitle() {
@ -208,6 +209,7 @@ class FormField extends RequestHandler {
function setRightTitle($val) {
$this->rightTitle = $val;
return $this;
}
function LeftTitle() {
@ -216,6 +218,7 @@ class FormField extends RequestHandler {
function setLeftTitle($val) {
$this->leftTitle = $val;
return $this;
}
/**
@ -226,6 +229,7 @@ class FormField extends RequestHandler {
*/
public function setTabIndex($index) {
$this->tabIndex = $index;
return $this;
}
/**
@ -281,6 +285,7 @@ class FormField extends RequestHandler {
*/
function addExtraClass($class) {
$this->extraClasses[$class] = $class;
return $this;
}
/**
@ -290,6 +295,7 @@ class FormField extends RequestHandler {
*/
function removeExtraClass($class) {
if(isset($this->extraClasses) && array_key_exists($class, $this->extraClasses)) unset($this->extraClasses[$class]);
return $this;
}
/**
@ -305,6 +311,7 @@ class FormField extends RequestHandler {
*/
function setAttribute($name, $value) {
$this->attributes[$name] = $value;
return $this;
}
/**
@ -378,6 +385,7 @@ class FormField extends RequestHandler {
*/
function setValue($value) {
$this->value = $value; return $this;
return $this;
}
/**
@ -385,6 +393,7 @@ class FormField extends RequestHandler {
*/
function setName($name) {
$this->name = $name;
return $this;
}
/**
@ -394,6 +403,7 @@ class FormField extends RequestHandler {
*/
function setForm($form) {
$this->form = $form;
return $this;
}
/**
@ -423,6 +433,7 @@ class FormField extends RequestHandler {
*/
public function setFieldHolderTemplate($template) {
$this->fieldHolderTemplate = $template;
return $this;
}
/**
@ -443,6 +454,7 @@ class FormField extends RequestHandler {
function setError($message, $messageType) {
$this->message = $message;
$this->messageType = $messageType;
return $this;
}
/**
@ -454,6 +466,7 @@ class FormField extends RequestHandler {
*/
public function setCustomValidationMessage($msg) {
$this->customValidationMessage = $msg;
return $this;
}
/**
@ -477,6 +490,7 @@ class FormField extends RequestHandler {
*/
function setTemplate($template) {
$this->template = $template;
return $this;
}
/**
@ -569,6 +583,7 @@ class FormField extends RequestHandler {
*/
function setReadonly($bool) {
$this->readonly = $bool;
return $this;
}
/**
@ -585,6 +600,7 @@ class FormField extends RequestHandler {
*/
function setDisabled($bool) {
$this->disabled = $bool;
return $this;
}
/**
@ -738,6 +754,7 @@ class FormField extends RequestHandler {
*/
function setContainerFieldSet($containerFieldSet) {
$this->containerFieldSet = $containerFieldSet;
return $this;
}
function rootFieldSet() {

View File

@ -121,7 +121,8 @@ class HtmlEditorConfig {
* @return mixed - $v returned for chaining
*/
function setOption($k,$v) {
return $this->settings[$k] = $v;
$this->settings[$k] = $v;
return $this;
}
/**
@ -133,6 +134,7 @@ class HtmlEditorConfig {
foreach ($a as $k=>$v) {
$this->settings[$k] = $v;
}
return $this;
}
/**
@ -173,6 +175,7 @@ class HtmlEditorConfig {
unset($this->plugins[$plugin]);
}
}
return $this;
}
/**
@ -198,6 +201,7 @@ class HtmlEditorConfig {
$line = array_shift($buttons);
}
$this->buttons[$line] = is_array($buttons) ? $buttons : array($buttons);
return $this;
}
/**
@ -214,6 +218,7 @@ class HtmlEditorConfig {
foreach ($inserts as $button) {
$this->buttons[$line][] = $button;
}
return $this;
}
/**

View File

@ -118,6 +118,7 @@ class ListboxField extends DropdownField {
*/
function setSize($size) {
$this->size = $size;
return $this;
}
/**
@ -126,6 +127,7 @@ class ListboxField extends DropdownField {
*/
function setMultiple($bool) {
$this->multiple = $bool;
return $this;
}
function setSource($source) {
@ -137,6 +139,8 @@ class ListboxField extends DropdownField {
}
parent::setSource($source);
return $this;
}
/**
@ -180,6 +184,7 @@ class ListboxField extends DropdownField {
parent::setValue($val);
}
return $this;
}
}

View File

@ -42,6 +42,7 @@ class LiteralField extends DatalessField {
*/
function setContent($content) {
$this->content = $content;
return $this;
}
/**
@ -55,7 +56,8 @@ class LiteralField extends DatalessField {
* Synonym of {@link setContent()} so that LiteralField is more compatible with other field types.
*/
function setValue($value) {
return $this->setContent($value);
$this->setContent($value);
return $this;
}
function performReadonlyTransformation() {

View File

@ -87,6 +87,8 @@ class MoneyField extends FormField {
// decimal and thousands signs, while respecting the stored
// precision in the database without truncating it during display
// and subsequent save operations
return $this;
}
/**
@ -129,6 +131,8 @@ class MoneyField extends FormField {
$this->fieldAmount->setReadonly($bool);
$this->fieldCurrency->setReadonly($bool);
return $this;
}
function setDisabled($bool) {
@ -136,6 +140,8 @@ class MoneyField extends FormField {
$this->fieldAmount->setDisabled($bool);
$this->fieldCurrency->setDisabled($bool);
return $this;
}
/**
@ -148,6 +154,8 @@ class MoneyField extends FormField {
$oldVal = $this->fieldCurrency->Value();
$this->fieldCurrency = $this->FieldCurrency($this->name);
$this->fieldCurrency->setValue($oldVal);
return $this;
}
/**
@ -159,6 +167,7 @@ class MoneyField extends FormField {
function setLocale($locale) {
$this->_locale = $locale;
return $this;
}
function getLocale() {

View File

@ -61,6 +61,7 @@ class NullableField extends FormField {
*/
function setIsNullLabel(string $isNulLabel){
$this->isNullLabel = $isNulLabel;
return $this;
}
/**
@ -95,6 +96,8 @@ class NullableField extends FormField {
}
$this->valueField->setValue($value);
parent::setValue($value);
return $this;
}
/**
@ -105,6 +108,8 @@ class NullableField extends FormField {
// We need to pass through the name change to the underlying value field.
$this->valueField->setName($name);
parent::setName($name);
return $this;
}
/**

View File

@ -108,6 +108,7 @@ class OptionsetField extends DropdownField {
*/
function setDisabledItems($items) {
$this->disabledItems = $items;
return $this;
}
/**

View File

@ -56,6 +56,7 @@ class Tab extends CompositeField {
public function setTabSet($val) {
$this->tabSet = $val;
return $this;
}
/**

View File

@ -95,7 +95,9 @@ class TabSet extends CompositeField {
public function setTabSet($val) {
$this->tabSet = $val;
return $this;
}
public function getTabSet() {
if(isset($this->tabSet)) return $this->tabSet;
}

View File

@ -454,6 +454,7 @@ class TableField extends TableListField {
*/
function setExtraData($extraData) {
$this->extraData = $extraData;
return $this;
}
/**
@ -480,6 +481,7 @@ class TableField extends TableListField {
function setTransformationConditions($conditions) {
$this->transformationConditions = $conditions;
return $this;
}
function jsValidation() {
@ -580,6 +582,7 @@ JS;
function setRequiredFields($fields) {
$this->requiredFields = $fields;
return $this;
}
}

View File

@ -385,10 +385,12 @@ JS
*/
function setCustomQuery(DataList $dataList) {
$this->dataList = $dataList;
return $this;
}
function setCustomCsvQuery(DataList $dataList) {
$this->customCsvQuery = $query;
return $this;
}
function setCustomSourceItems(SS_List $items) {
@ -400,6 +402,8 @@ JS
} else {
user_error('TableList::setCustomSourceItems() should be passed a SS_List', E_USER_WARNING);
}
return $this;
}
/**
@ -499,6 +503,7 @@ JS
*/
function setClick_AjaxLoad($urlBase, $formID) {
$this->clickAction = "this.ajaxRequest('" . addslashes($urlBase) . "', '" . addslashes($formID) . "')";
return $this;
}
/**
@ -506,6 +511,7 @@ JS
*/
function setClick_PopupLoad($urlBase) {
$this->clickAction = "var w = window.open(baseHref() + '$urlBase' + this.id.replace(/.*-(\d*)$/,'$1'), 'popup'); w.focus();";
return $this;
}
function performReadonlyTransformation() {
@ -701,6 +707,7 @@ JS
function setPermissions($arr) {
$this->permissions = $arr;
return $this;
}
/**
@ -717,6 +724,7 @@ JS
*/
function setShowPagination($bool) {
$this->showPagination = (bool)$bool;
return $this;
}
/**
@ -731,6 +739,7 @@ JS
function setPageSize($pageSize) {
$this->pageSize = $pageSize;
return $this;
}
function PageSize() {
@ -748,6 +757,7 @@ JS
function setExtraLinkParams($params){
Deprecation::notice('2.4', 'Put the query string onto your FormAction instead().');
$this->extraLinkParams = $params;
return $this;
}
/**
@ -907,6 +917,7 @@ JS
*/
function setFieldListCsv($fields) {
$this->fieldListCsv = $fields;
return $this;
}
/**
@ -914,6 +925,7 @@ JS
*/
function setCsvSeparator($csvSeparator) {
$this->csvSeparator = $csvSeparator;
return $this;
}
/**
@ -928,6 +940,7 @@ JS
*/
function removeCsvHeader() {
$this->csvHasHeader = false;
return $this;
}
/**
@ -1099,14 +1112,17 @@ JS
function setFieldCasting($casting) {
$this->fieldCasting = $casting;
return $this;
}
function setFieldFormatting($formatting) {
$this->fieldFormatting = $formatting;
return $this;
}
function setCSVFieldFormatting($formatting) {
$this->csvFieldFormatting = $formatting;
return $this;
}
/**
@ -1114,6 +1130,7 @@ JS
*/
function setFieldList($fieldList) {
$this->fieldList = $fieldList;
return $this;
}
/**
@ -1147,6 +1164,7 @@ JS
function setTemplate($template) {
$this->template = $template;
return $this;
}
function CurrentLink() {
@ -1241,6 +1259,7 @@ JS
function setHighlightConditions($conditions) {
$this->highlightConditions = $conditions;
return $this;
}
/**

View File

@ -27,6 +27,7 @@ class TextField extends FormField {
*/
function setMaxLength($length) {
$this->maxLength = $length;
return $this;
}
/**

View File

@ -100,6 +100,7 @@ class TextareaField extends FormField {
*/
function setRows($rows) {
$this->rows = $rows;
return $this;
}
/**
@ -109,6 +110,7 @@ class TextareaField extends FormField {
*/
function setColumns($cols) {
$this->cols = $cols;
return $this;
}
function Value() {

View File

@ -116,6 +116,8 @@ class TimeField extends TextField {
$this->value = null;
$this->valueObj = null;
}
return $this;
}
/**
@ -165,6 +167,7 @@ class TimeField extends TextField {
*/
function setLocale($locale) {
$this->locale = $locale;
return $this;
}
/**
@ -173,6 +176,7 @@ class TimeField extends TextField {
*/
function setConfig($name, $val) {
$this->config[$name] = $val;
return $this;
}
/**

View File

@ -98,6 +98,7 @@ class TreeDropdownField extends FormField {
*/
public function setTreeBaseID($ID) {
$this->baseID = (int) $ID;
return $this;
}
/**
@ -112,6 +113,7 @@ class TreeDropdownField extends FormField {
}
$this->filterCallback = $callback;
return $this;
}
/**
@ -126,6 +128,7 @@ class TreeDropdownField extends FormField {
}
$this->searchCallback = $callback;
return $this;
}
public function getShowSearch() {
@ -137,6 +140,7 @@ class TreeDropdownField extends FormField {
*/
public function setShowSearch($bool) {
$this->showSearch = $bool;
return $this;
}
/**

View File

@ -79,6 +79,7 @@ abstract class Validator extends Object {
} else {
user_error("Validator::setJavascriptValidationHandler() passed bad handler '$handler'", E_USER_WARNING);
}
return $this;
}
/**
@ -96,6 +97,7 @@ abstract class Validator extends Object {
*/
function setForm($form) {
$this->form = $form;
return $this;
}
/**