Permalink
Browse files

API Copying instance props on FormField readonly/disabled transformat…

…ions

Introduced new FormField->castedCopy() method
which tries to replicate the existing form field instance
as closely as possible.

Primarily, the fix was targeted at consistently passing
through FormField->description to all of its variations.
  • Loading branch information...
1 parent 6f9d01f commit 559abecd56eec1c9a153a515c0645f1c84088e39 @chillu chillu committed Dec 13, 2012
View
@@ -40,12 +40,6 @@ public function performReadonlyTransformation() {
$field->setForm($this->form);
return $field;
}
-
- public function performDisabledTransformation() {
- $clone = clone $this;
- $clone->setDisabled(true);
- return $clone;
- }
}
@@ -276,10 +276,8 @@ public function performReadonlyTransformation() {
}
}
- $title = ($this->title) ? $this->title : '';
-
- $field = new ReadonlyField($this->name, $title, $values);
- $field->setForm($this->form);
+ $field = $this->castedCopy('ReadonlyField');
+ $field->setValue($values);
return $field;
}
View
@@ -265,6 +265,8 @@ public function performReadonlyTransformation() {
$clone->children = $newChildren;
$clone->readonly = true;
+ $clone->addExtraClass($this->extraClass());
+ $clone->setDescription($this->getDescription());
return $clone;
}
@@ -285,6 +287,11 @@ public function performDisabledTransformation() {
$clone->children = $newChildren;
$clone->readonly = true;
+ $clone->addExtraClass($this->extraClass());
+ $clone->setDescription($this->getDescription());
+ foreach($this->attributes as $k => $v) {
+ $clone->setAttribute($k, $v);
+ }
return $clone;
}
@@ -320,10 +320,10 @@ public function saveInto(DataObjectInterface $record) {
* Makes a pretty readonly field with some stars in it
*/
public function performReadonlyTransformation() {
- $stars = '*****';
+ $field = $this->castedCopy('ReadonlyField')
+ ->setTitle($this->title ? $this->title : _t('Member.PASSWORD'))
+ ->setValue('*****');
- $field = new ReadonlyField($this->name, $this->title ? $this->title : _t('Member.PASSWORD'), $stars);
- $field->setForm($this->form);
return $field;
}
}
View
@@ -40,9 +40,7 @@ public function Type() {
* Create a new class for this field
*/
public function performReadonlyTransformation() {
- $field = new CurrencyField_Readonly($this->name, $this->title, $this->value);
- $field -> addExtraClass($this->extraClass());
- return $field;
+ return $this->castedCopy('CurrencyField_Readonly');
}
public function validate($validator) {
View
@@ -279,13 +279,25 @@ public function dataValue() {
}
public function performReadonlyTransformation() {
- $field = new DateField_Disabled($this->name, $this->title, $this->dataValue());
- $field->setForm($this->form);
+ $field = $this->castedCopy('DateField_Disabled');
+ $field->setValue($this->dataValue());
$field->readonly = true;
return $field;
}
+ public function castedCopy($class) {
+ $copy = new $class($this->name);
+ if($copy->hasMethod('setConfig')) {
+ $config = $this->getConfig();
+ foreach($config as $k => $v) {
+ $copy->setConfig($k, $v);
+ }
+ }
+
+ return parent::castedCopy($copy);
+ }
+
/**
* Validate an array with expected keys 'day', 'month' and 'year.
* Used because Zend_Date::isDate() doesn't provide this.
View
@@ -283,8 +283,22 @@ public function validate($validator) {
}
public function performReadonlyTransformation() {
- $field = new DatetimeField_Readonly($this->name, $this->title, $this->dataValue());
- $field->setForm($this->form);
+ $field = $this->castedCopy('DatetimeField_Readonly');
+ $field->setValue($this->dataValue());
+
+ $dateFieldConfig = $this->getDateField()->getConfig();
+ if($dateFieldConfig) {
+ foreach($dateFieldConfig as $k => $v) {
+ $field->getDateField()->setConfig($k, $v);
+ }
+ }
+
+ $timeFieldConfig = $this->getTimeField()->getConfig();
+ if($timeFieldConfig) {
+ foreach($timeFieldConfig as $k => $v) {
+ $field->getTimeField()->setConfig($k, $v);
+ }
+ }
return $field;
}
View
@@ -239,10 +239,10 @@ public function getEmptyString() {
}
public function performReadonlyTransformation() {
- $field = new LookupField($this->name, $this->title, $this->getSource());
- $field->setValue($this->value);
- $field->setForm($this->form);
+ $field = $this->castedCopy('LookupField');
+ $field->setSource($this->getSource());
$field->setReadonly(true);
+
return $field;
}
}
View
@@ -357,7 +357,7 @@ public function getAttributes() {
'id' => $this->ID(),
'disabled' => $this->isDisabled(),
);
-
+
return array_merge($attrs, $this->attributes);
}
@@ -709,10 +709,9 @@ public function setDisabled($bool) {
* Returns a readonly version of this field
*/
public function performReadonlyTransformation() {
- $field = new ReadonlyField($this->name, $this->title, $this->value);
- $field->addExtraClass($this->extraClass());
- $field->setForm($this->form);
- return $field;
+ $copy = $this->castedCopy('ReadonlyField');
+ $copy->setReadonly(true);
+ return $copy;
}
/**
@@ -723,14 +722,15 @@ public function performReadonlyTransformation() {
* @return FormField
*/
public function performDisabledTransformation() {
- $clone = clone $this;
- $disabledClassName = $clone->class . '_Disabled';
+ $disabledClassName = $this->class . '_Disabled';
if(ClassInfo::exists($disabledClassName)) {
- return new $disabledClassName($this->name, $this->title, $this->value);
+ $clone = $this->castedCopy($disabledClassName);
} else {
+ $clone = clone $this;
$clone->setDisabled(true);
- return $clone;
}
+
+ return $clone;
}
public function transform(FormTransformation $trans) {
@@ -774,7 +774,8 @@ public function validate($validator) {
/**
* Describe this field, provide help text for it.
- * By default, renders as a "title" attribute on the form field.
+ * By default, renders as a <span class="description">
+ * underneath the form field.
*
* @return string Description
*/
@@ -828,5 +829,43 @@ public function rootFieldList() {
if(is_object($this->containerFieldList)) return $this->containerFieldList->rootFieldList();
else user_error("rootFieldList() called on $this->class object without a containerFieldList", E_USER_ERROR);
}
+
+ /**
+ * 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}.
+ *
+ * Does not copy custom field templates, since they probably won't apply to
+ * the new instance.
+ *
+ * @param String $classOrCopy Class name for copy, or existing copy instance to update
+ * @return FormField
+ */
+ public function castedCopy($classOrCopy) {
+ $field = (is_object($classOrCopy)) ? $classOrCopy : new $classOrCopy($this->name);
+ $field
+ ->setValue($this->value) // get value directly from property, avoid any conversions
+ ->setForm($this->form)
+ ->setTitle($this->Title())
+ ->setLeftTitle($this->LeftTitle())
+ ->setRightTitle($this->RightTitle())
+ ->addExtraClass($this->extraClass())
+ ->setDescription($this->getDescription());
+
+ // Only include built-in attributes, ignore anything
+ // set through getAttributes(), since those might change important characteristics
+ // of the field, e.g. its "type" attribute.
+ foreach($this->attributes as $k => $v) {
+ $field->setAttribute($k, $v);
+ }
+ $field->dontEscape = $this->dontEscape;
+
+ return $field;
+ }
}
@@ -204,9 +204,9 @@ public function saveInto(DataObjectInterface $record) {
* @return HtmlEditorField_Readonly
*/
public function performReadonlyTransformation() {
- $field = new HtmlEditorField_Readonly($this->name, $this->title, $this->value);
- $field->setForm($this->form);
+ $field = $this->castedCopy('HtmlEditorField_Readonly');
$field->dontEscape = true;
+
return $field;
}
@@ -24,7 +24,7 @@ public function __construct($action, $title = "", $extraClass = '') {
}
public function performReadonlyTransformation() {
- return new InlineFormAction_ReadOnly( $this->name, $this->title );
+ return $this->castedCopy('InlineFormAction_ReadOnly');
}
public function Field($properties = array()) {
View
@@ -32,7 +32,7 @@ public function Field($properties = array()) {
// Don't check if string arguments are matching against the source,
// as they might be generated HTML diff views instead of the actual values
- if($this->value && !$mapped) {
+ if($this->value && !is_array($this->value) && !$mapped) {
$mapped = array(trim($this->value));
$values = array();
}
View
@@ -92,9 +92,8 @@ public function Field($properties = array()) {
public function performReadonlyTransformation() {
// Source and values are DataObject sets.
- $items = $this->getSource();
- $field = new LookupField($this->name, $this->title ? $this->title : '', $items, $this->value);
- $field->setForm($this->form);
+ $field = $this->castedCopy('LookupField');
+ $field->setValue($this->getSource());
$field->setReadonly(true);
return $field;
View
@@ -31,11 +31,9 @@ public function getAttributes() {
* Makes a pretty readonly field with some stars in it
*/
public function performReadonlyTransformation() {
- $stars = '*****';
-
- $field = new ReadonlyField($this->name, $this->title ? $this->title : '', $stars);
- $field->setForm($this->form);
- $field->setReadonly(true);
+ $field = $this->castedCopy('ReadonlyField');
+ $field->setValue('*****');
+
return $field;
}
View
@@ -44,20 +44,6 @@ public function getAttributes() {
);
}
- /**
- * Performs a disabled transformation on this field. You shouldn't be able to
- * copy from this field, and it should not send any data when you submit the
- * form it's attached to.
- *
- * The element shouldn't be both disabled and readonly at the same time.
- */
- public function performDisabledTransformation() {
- $clone = clone $this;
- $clone->setDisabled(true);
- $clone->setReadonly(false);
- return $clone;
- }
-
public function Type() {
return parent::Type() . ($this->readonly ? ' readonly' : '');
}
View
@@ -191,7 +191,19 @@ public function getConfig($name = null) {
* Creates a new readonly field specified below
*/
public function performReadonlyTransformation() {
- return new TimeField_Readonly($this->name, $this->title, $this->dataValue(), $this->getConfig('timeformat'));
+ return $this->castedCopy('TimeField_Readonly');
+ }
+
+ public function castedCopy($class) {
+ $copy = parent::castedCopy($class);
+ if($copy->hasMethod('setConfig')) {
+ $config = $this->getConfig();
+ foreach($config as $k => $v) {
+ $copy->setConfig($k, $v);
+ }
+ }
+
+ return $copy;
}
}
Oops, something went wrong.

0 comments on commit 559abec

Please sign in to comment.