Permalink
Browse files

BUGFIX FormField::name_to_label() is now declared as static as it was…

… never used as an instance method

API CHANGE FormField::validate() $validator argument is now required for FormField classes
  • Loading branch information...
1 parent 8369cde commit 865cde0c39d75c383e08ee11414b9868ea6c8daa @halkyon halkyon committed Apr 12, 2012
Showing with 47 additions and 43 deletions.
  1. +47 −43 forms/FormField.php
View
@@ -89,6 +89,30 @@ class FormField extends RequestHandler {
protected $attributes = array();
/**
+ * Takes a fieldname and converts camelcase to spaced
+ * words. Also resolves combined fieldnames with dot syntax
+ * to spaced words.
+ *
+ * Examples:
+ * - 'TotalAmount' will return 'Total Amount'
+ * - 'Organisation.ZipCode' will return 'Organisation Zip Code'
+ *
+ * @param string $fieldName
+ * @return string
+ */
+ public static function name_to_label($fieldName) {
+ if(strpos($fieldName, '.') !== false) {
+ $parts = explode('.', $fieldName);
+ $label = $parts[count($parts)-2] . ' ' . $parts[count($parts)-1];
+ } else {
+ $label = $fieldName;
+ }
+ $label = preg_replace("/([a-z]+)([A-Z])/","$1 $2", $label);
+
+ return $label;
+ }
+
+ /**
* Create a new field.
* @param name The internal field name, passed to forms.
* @param title The field label.
@@ -582,17 +606,17 @@ function setReadonly($bool) {
/**
* @return boolean
*/
- function isDisabled() {
- return $this->disabled;
+ function isDisabled() {
+ return $this->disabled;
}
/**
* Sets disabed-flag on form-field. Please use performDisabledTransformation()
* to actually transform this instance.
* @param $bool boolean Setting "false" has no effect on the field-state.
*/
- function setDisabled($bool) {
- $this->disabled = $bool;
+ function setDisabled($bool) {
+ $this->disabled = $bool;
return $this;
}
@@ -607,21 +631,23 @@ function performReadonlyTransformation() {
}
/**
- * Return a disabled version of this field
+ * Return a disabled version of this field.
+ * Tries to find a class of the class name of this field suffixed with "_Disabled",
+ * failing that, finds a method {@link setDisabled()}.
+ *
+ * @return FormField
*/
function performDisabledTransformation() {
$clone = clone $this;
$disabledClassName = $clone->class . '_Disabled';
- if( ClassInfo::exists( $disabledClassName ) )
- return new $disabledClassName( $this->name, $this->title, $this->value );
- elseif($clone->hasMethod('setDisabled')){
+ if(ClassInfo::exists($disabledClassName)) {
+ return new $disabledClassName($this->name, $this->title, $this->value);
+ } else {
$clone->setDisabled(true);
return $clone;
- }else{
- return $this->performReadonlyTransformation();
}
}
-
+
function transform(FormTransformation $trans) {
return $trans->transform($this);
}
@@ -662,14 +688,16 @@ function createTag($tag, $attributes, $content = null) {
if($content || $tag != 'input') return "<$tag$preparedAttributes>$content</$tag>";
else return "<$tag$preparedAttributes />";
}
-
+
/**
- * Validation Functions for each field type by default
- * formfield doesnt have a validation function
- *
- * @todo shouldn't this be an abstract method?
+ * Abstract method each {@link FormField} subclass must implement,
+ * determines whether the field is valid or not based on the value.
+ * @todo Make this abstract.
+ *
+ * @param Validator
+ * @return boolean
*/
- function validate() {
+ function validate($validator) {
return true;
}
@@ -690,7 +718,7 @@ function describe($description) {
*/
function setDescription($description) {
$this->description = $description;
- return $this;
+ return $this;
}
/**
@@ -721,31 +749,7 @@ function Required() {
return $validator->fieldIsRequired($this->name);
}
}
-
- /**
- * Takes a fieldname and converts camelcase to spaced
- * words. Also resolves combined fieldnames with dot syntax
- * to spaced words.
- *
- * Examples:
- * - 'TotalAmount' will return 'Total Amount'
- * - 'Organisation.ZipCode' will return 'Organisation Zip Code'
- *
- * @param string $fieldName
- * @return string
- */
- public function name_to_label($fieldName) {
- if(strpos($fieldName, '.') !== false) {
- $parts = explode('.', $fieldName);
- $label = $parts[count($parts)-2] . ' ' . $parts[count($parts)-1];
- } else {
- $label = $fieldName;
- }
- $label = preg_replace("/([a-z]+)([A-Z])/","$1 $2", $label);
-
- return $label;
- }
-
+
/**
* Set the FieldList that contains this field.
*

0 comments on commit 865cde0

Please sign in to comment.