Skip to content

Commit

Permalink
BUGFIX FormField::name_to_label() is now declared as static as it was…
Browse files Browse the repository at this point in the history
… never used as an instance method

API CHANGE FormField::validate() $validator argument is now required for FormField classes
  • Loading branch information
Sean Harvey committed Apr 12, 2012
1 parent 8369cde commit 865cde0
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions forms/FormField.php
Expand Up @@ -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.
Expand Down Expand Up @@ -582,17 +606,17 @@ function setReadonly($bool) {
/** /**
* @return boolean * @return boolean
*/ */
function isDisabled() { function isDisabled() {
return $this->disabled; return $this->disabled;
} }


/** /**
* Sets disabed-flag on form-field. Please use performDisabledTransformation() * Sets disabed-flag on form-field. Please use performDisabledTransformation()
* to actually transform this instance. * to actually transform this instance.
* @param $bool boolean Setting "false" has no effect on the field-state. * @param $bool boolean Setting "false" has no effect on the field-state.
*/ */
function setDisabled($bool) { function setDisabled($bool) {
$this->disabled = $bool; $this->disabled = $bool;
return $this; return $this;
} }


Expand All @@ -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() { 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();
} }
} }

function transform(FormTransformation $trans) { function transform(FormTransformation $trans) {
return $trans->transform($this); return $trans->transform($this);
} }
Expand Down Expand Up @@ -662,14 +688,16 @@ function createTag($tag, $attributes, $content = null) {
if($content || $tag != 'input') return "<$tag$preparedAttributes>$content</$tag>"; if($content || $tag != 'input') return "<$tag$preparedAttributes>$content</$tag>";
else return "<$tag$preparedAttributes />"; else return "<$tag$preparedAttributes />";
} }

/** /**
* 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;
} }


Expand All @@ -690,7 +718,7 @@ function describe($description) {
*/ */
function setDescription($description) { function setDescription($description) {
$this->description = $description; $this->description = $description;
return $this; return $this;
} }


/** /**
Expand Down Expand Up @@ -721,31 +749,7 @@ function Required() {
return $validator->fieldIsRequired($this->name); 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. * Set the FieldList that contains this field.
* *
Expand Down

0 comments on commit 865cde0

Please sign in to comment.