Permalink
Browse files

API CHANGE FormField::Field() and FormField::FieldHolder() now render…

… into templates on each FormField instead of creating HTML from PHP
  • Loading branch information...
1 parent b3c08db commit 9e548f501e558748f43c0d63e08cd496b00312ed @halkyon halkyon committed with chillu Mar 23, 2011
Showing with 338 additions and 563 deletions.
  1. +9 −73 forms/CheckboxField.php
  2. +30 −30 forms/CheckboxSetField.php
  3. +2 −2 forms/DatalessField.php
  4. +29 −51 forms/DropdownField.php
  5. +14 −2 forms/EmailField.php
  6. +4 −20 forms/FileField.php
  7. +5 −7 forms/FileIFrameField.php
  8. +23 −68 forms/FormAction.php
  9. +69 −82 forms/FormField.php
  10. +8 −12 forms/HeaderField.php
  11. +6 −8 forms/HiddenField.php
  12. +5 −13 forms/LabelField.php
  13. +3 −2 forms/LiteralField.php
  14. +1 −0 forms/NullableField.php
  15. +26 −46 forms/OptionsetField.php
  16. +1 −2 forms/ReadonlyField.php
  17. +4 −23 forms/ResetFormAction.php
  18. +13 −19 forms/TextField.php
  19. +17 −44 forms/TextareaField.php
  20. +14 −22 forms/TreeDropdownField.php
  21. 0 templates/{FileIFrameField.ss → FileIFrameField_iframe.ss}
  22. +1 −0 templates/forms/CheckboxField.ss
  23. +5 −0 templates/forms/CheckboxFieldHolder.ss
  24. +8 −0 templates/forms/CheckboxSetField.ss
  25. +5 −0 templates/forms/DropdownField.ss
  26. +6 −0 templates/forms/FieldHolder.ss
  27. +2 −0 templates/forms/FileField.ss
  28. +5 −0 templates/forms/FormAction.ss
  29. +2 −0 templates/forms/FormField.ss
  30. +1 −0 templates/forms/HeaderField.ss
  31. +1 −0 templates/forms/HiddenField.ss
  32. +1 −0 templates/forms/LabelField.ss
  33. +8 −0 templates/forms/OptionsetField.ss
  34. +1 −0 templates/forms/TextField.ss
  35. +5 −0 templates/forms/TextareaField.ss
  36. +3 −0 templates/forms/TreeDropdownField.ss
  37. +0 −8 tests/forms/CheckboxSetFieldTest.php
  38. +0 −8 tests/forms/DropdownFieldTest.php
  39. +0 −20 tests/forms/FormFieldTest.php
  40. +1 −1 tests/model/LabelFieldTest.php
View
@@ -5,8 +5,6 @@
* @subpackage fields-basic
*/
class CheckboxField extends FormField {
-
- protected $disabled;
function setValue($value) {
$this->value = ($value) ? 1 : 0;
@@ -15,54 +13,18 @@ function setValue($value) {
function dataValue() {
return ($this->value) ? 1 : NULL;
}
-
+
function Value() {
return ($this->value) ? 1 : 0;
}
-
- function Field() {
- $attributes = array(
- 'type' => 'checkbox',
- 'class' => ($this->extraClass() ? $this->extraClass() : ''),
- 'id' => $this->id(),
- 'name' => $this->getName(),
- 'value' => 1,
- 'checked' => $this->value ? 'checked' : '',
- 'tabindex' => $this->getTabIndex()
- );
-
- if($this->disabled) $attributes['disabled'] = 'disabled';
-
- return $this->createTag('input', $attributes);
- }
- /**
- * Checkboxes use the RightLabelledFieldHolder template, to put the field on the left
- * and the label on the right. See {@link FormField::FieldHolder} for more information about
- * how FieldHolder works.
- */
- function FieldHolder() {
- if($this->labelLeft) {
- return parent::FieldHolder();
- } else {
- extract($this->getXMLValues(array( 'Name', 'Field', 'Title', 'Message', 'MessageType' )),
- EXTR_SKIP);
- $messageBlock = isset($Message) ? "<span class=\"message $MessageType\">$Message</span>" : '';
- $Type = $this->XML_val('Type');
- $extraClass = $this->XML_val('extraClass');
- return <<<HTML
-<div id="$Name" class="field $Type $extraClass">
- $Field
- <label class="right" for="{$this->id()}">$Title</label>
- $messageBlock
-</div>
-HTML;
-
- }
+ function Field($properties = array()) {
+ return $this->customise($properties)->renderWith('CheckboxField');
}
- function useLabelLeft( $labelLeft = true ) {
- $this->labelLeft = $labelLeft;
+ function FieldHolder() {
+ $this->setFieldHolderTemplate(($this->fieldHolderTemplate) ? $this->fieldHolderTemplate : 'CheckboxFieldHolder');
+ return parent::FieldHolder();
}
/**
@@ -79,7 +41,6 @@ function SmallFieldHolder() {
/**
* Returns a readonly version of this field
*/
-
function performReadonlyTransformation() {
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
$field->setForm($this->form);
@@ -91,6 +52,7 @@ function performDisabledTransformation() {
$clone->setDisabled(true);
return $clone;
}
+
}
/**
@@ -99,39 +61,13 @@ function performDisabledTransformation() {
* @subpackage fields-basic
*/
class CheckboxField_Readonly extends ReadonlyField {
+
function performReadonlyTransformation() {
return clone $this;
}
function setValue($val) {
$this->value = (int)($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
}
-}
-/**
- * Single checkbox field, disabled
- * @package forms
- * @subpackage fields-basic
- */
-class CheckboxField_Disabled extends CheckboxField {
-
- protected $disabled = true;
-
- /**
- * Returns a single checkbox field - used by templates.
- */
- function Field() {
- $attributes = array(
- 'type' => 'checkbox',
- 'class' => ($this->extraClass() ? $this->extraClass() : ''),
- 'id' => $this->id(),
- 'name' => $this->getName(),
- 'tabindex' => $this->getTabIndex(),
- 'checked' => ($this->value) ? 'checked' : false,
- 'disabled' => 'disabled'
- );
-
- return $this->createTag('input', $attributes);
- }
-}
-?>
+}
View
@@ -29,9 +29,6 @@
* array. Is it also appropriate to accept so many different
* types of data when just using an array would be appropriate?
*
- * @todo Make use of FormField->createTag() to generate the
- * HTML tag(s) for this field.
- *
* @package forms
* @subpackage fields-basic
*/
@@ -47,14 +44,13 @@ class CheckboxSetField extends OptionsetField {
/**
* @todo Explain different source data that can be used with this field,
* e.g. SQLMap, ArrayList or an array.
- *
- * @todo Should use CheckboxField FieldHolder rather than constructing own markup.
*/
- function Field() {
+ function Field($properties = array()) {
Requirements::css(SAPPHIRE_DIR . '/css/CheckboxSetField.css');
$source = $this->source;
$values = $this->value;
+ $items = array();
// Get values from the join, if available
if(is_object($this->form)) {
@@ -109,30 +105,35 @@ function Field() {
$options = "<li>No options available</li>";
}
- if($source) foreach($source as $index => $item) {
- if($item instanceof DataObject) {
- $key = $item->ID;
- $value = $item->Title;
- } else {
- $key = $index;
- $value = $item;
- }
-
- $odd = ($odd + 1) % 2;
- $extraClass = $odd ? 'odd' : 'even';
- $extraClass .= ' val' . str_replace(' ', '', $key);
- $itemID = $this->id() . '_' . ereg_replace('[^a-zA-Z0-9]+', '', $key);
- $checked = '';
-
- if(isset($items)) {
- $checked = (in_array($key, $items) || in_array($key, $this->defaultItems)) ? ' checked="checked"' : '';
- }
+ if($source) {
+ foreach($source as $value => $item) {
+ if($item instanceof DataObject) {
+ $value = $item->ID;
+ $title = $item->Title;
+ } else {
+ $title = $item;
+ }
+
+ $itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
+ $odd = ($odd + 1) % 2;
+ $extraClass = $odd ? 'odd' : 'even';
+ $extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value);
- $disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? $disabled = ' disabled="disabled"' : '';
- $options .= "<li class=\"$extraClass\"><input id=\"$itemID\" name=\"$this->name[$key]\" type=\"checkbox\" value=\"$key\"$checked $disabled class=\"checkbox\" /> <label for=\"$itemID\">$value</label></li>\n";
+ $options[] = new ArrayData(array(
+ 'ID' => $itemID,
+ 'Class' => $extraClass,
+ 'Name' => $this->name,
+ 'Value' => $value,
+ 'Title' => $title,
+ 'isChecked' => in_array($value, $items) || in_array($value, $this->defaultItems),
+ 'isDisabled' => $this->disabled || in_array($value, $this->disabledItems)
+ ));
+ }
}
-
- return "<ul id=\"{$this->id()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n$options</ul>\n";
+
+ $properties = array_merge($properties, array('Options' => new ArrayList($options)));
+
+ return $this->customise($properties)->renderWith('CheckboxSetField');
}
function setDisabled($val) {
@@ -287,5 +288,4 @@ function ExtraOptions() {
return FormField::ExtraOptions();
}
-}
-?>
+}
View
@@ -57,5 +57,5 @@ function setAllowHTML($bool) {
function getAllowHTML() {
return $this->allowHTML;
}
-}
-?>
+
+}
View
@@ -130,67 +130,45 @@ function __construct($name, $title = null, $source = array(), $value = "", $form
parent::__construct($name, ($title===null) ? $name : $title, $value, $form);
}
- /**
- * Returns a <select> tag containing all the appropriate <option> tags.
- * Makes use of {@link FormField->createTag()} to generate the <select>
- * tag and option elements inside is as the content of the <select>.
- *
- * @return string HTML tag for this dropdown field
- */
- function Field() {
- $options = '';
-
+ function Field($properties = array()) {
$source = $this->getSource();
+ $options = array();
if($source) {
- // For SQLMap sources, the empty string needs to be added specially
+ // SQLMap needs this to add an empty value to the options
if(is_object($source) && $this->emptyString) {
- $options .= $this->createTag('option', array('value' => ''), $this->emptyString);
+ $options[] = new ArrayData(array(
+ 'Value' => $this->emptyString,
+ 'Title' => '',
+ ));
}
-
+
foreach($source as $value => $title) {
-
- // Blank value of field and source (e.g. "" => "(Any)")
+ $selected = false;
if($value === '' && ($this->value === '' || $this->value === null)) {
- $selected = 'selected';
+ $selected = true;
} else {
- // Normal value from the source
- if($value) {
- $selected = ($value == $this->value) ? 'selected' : null;
- } else {
- // Do a type check comparison, we might have an array key of 0
- $selected = ($value === $this->value) ? 'selected' : null;
- }
-
- $this->isSelected = ($selected) ? true : false;
+ // check against value, fallback to a type check comparison when !value
+ $selected = ($value) ? $value == $this->value : $value === $this->value;
+ $this->isSelected = $selected;
}
-
- $options .= $this->createTag(
- 'option',
- array(
- 'selected' => $selected,
- 'value' => $value
- ),
- Convert::raw2xml($title)
- );
+
+ $options[] = new ArrayData(array(
+ 'Title' => $title,
+ 'Value' => $value,
+ 'Selected' => $selected,
+ ));
}
}
-
- $attributes = array(
- 'class' => ($this->extraClass() ? $this->extraClass() : ''),
- 'id' => $this->id(),
- 'name' => $this->name,
- 'tabindex' => $this->getTabIndex()
- );
-
- if($this->disabled) $attributes['disabled'] = 'disabled';
- return $this->createTag('select', $attributes, $options);
+ $properties = array_merge($properties, array('Options' => new ArrayList($options)));
+
+ return $this->customise($properties)->renderWith('DropdownField');
}
-
+
/**
* @return boolean
*/
- function isSelected(){
+ function isSelected() {
return $this->isSelected;
}
@@ -227,7 +205,7 @@ function setHasEmptyDefault($bool) {
function getHasEmptyDefault() {
return $this->hasEmptyDefault;
}
-
+
/**
* Set the default selection label, e.g. "select...".
* Defaults to an empty string. Automatically sets
@@ -239,7 +217,7 @@ function setEmptyString($str) {
$this->setHasEmptyDefault(true);
$this->emptyString = $str;
}
-
+
/**
* @return string
*/
@@ -254,13 +232,13 @@ function performReadonlyTransformation() {
$field->setReadonly(true);
return $field;
}
-
- function extraClass(){
+
+ function extraClass() {
$ret = parent::extraClass();
if($this->extraClass) $ret .= " $this->extraClass";
return $ret;
}
-
+
/**
* Set form being disabled
*/
View
@@ -39,7 +39,19 @@ function jsValidation() {
}
JS;
}
-
+
+ public function Field($properties = array()) {
+ return $this->customise($properties)->renderWith('TextField');
+ }
+
+ /**
+ * Returns the field type - used by templates.
+ * @return string
+ */
+ function Type() {
+ return 'text';
+ }
+
/**
* Validates for RFC 2822 compliant email adresses.
*
@@ -69,5 +81,5 @@ function validate($validator){
return true;
}
}
+
}
-?>
Oops, something went wrong.

0 comments on commit 9e548f5

Please sign in to comment.