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
This commit is contained in:
Sean Harvey 2012-04-12 12:18:55 +12:00
parent 8369cded32
commit 865cde0c39

View File

@ -88,6 +88,30 @@ class FormField extends RequestHandler {
*/ */
protected $attributes = array(); 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. * Create a new field.
* @param name The internal field name, passed to forms. * @param name The internal field name, passed to forms.
@ -607,18 +631,20 @@ class FormField extends RequestHandler {
} }
/** /**
* 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() { function performDisabledTransformation() {
$clone = clone $this; $clone = clone $this;
$disabledClassName = $clone->class . '_Disabled'; $disabledClassName = $clone->class . '_Disabled';
if( ClassInfo::exists( $disabledClassName ) ) if(ClassInfo::exists($disabledClassName)) {
return new $disabledClassName($this->name, $this->title, $this->value); return new $disabledClassName($this->name, $this->title, $this->value);
elseif($clone->hasMethod('setDisabled')){ } else {
$clone->setDisabled(true); $clone->setDisabled(true);
return $clone; return $clone;
}else{
return $this->performReadonlyTransformation();
} }
} }
@ -664,12 +690,14 @@ class FormField extends RequestHandler {
} }
/** /**
* Validation Functions for each field type by default * Abstract method each {@link FormField} subclass must implement,
* formfield doesnt have a validation function * determines whether the field is valid or not based on the value.
* @todo Make this abstract.
* *
* @todo shouldn't this be an abstract method? * @param Validator
* @return boolean
*/ */
function validate() { function validate($validator) {
return true; return true;
} }
@ -722,30 +750,6 @@ class FormField extends RequestHandler {
} }
} }
/**
* 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. * Set the FieldList that contains this field.
* *