From 3e3139d7c74037aff5af059dd44411c03729230b Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Mon, 27 Apr 2015 11:59:06 +1200 Subject: [PATCH] Clean up TextField --- forms/TextField.php | 66 ++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/forms/TextField.php b/forms/TextField.php index 336be1aeb..c5b45715a 100644 --- a/forms/TextField.php +++ b/forms/TextField.php @@ -1,4 +1,5 @@ maxLength = $maxLength; - - parent::__construct($name, $title, $value, $form); + if($maxLength) { + $this->setMaxLength($maxLength); + } + + if($form) { + $this->setForm($form); + } + + parent::__construct($name, $title, $value); } - + /** - * @param int $length + * @param int $maxLength + * + * @return static */ - public function setMaxLength($length) { - $this->maxLength = $length; - + public function setMaxLength($maxLength) { + $this->maxLength = $maxLength; + return $this; } - + /** - * @return int + * @return null|int */ public function getMaxLength() { return $this->maxLength; } + /** + * @return array + */ public function getAttributes() { + $maxLength = $this->getMaxLength(); + + $attributes = array(); + + if($maxLength) { + $attributes['maxLength'] = $maxLength; + $attributes['size'] = min($maxLength, 30); + } + return array_merge( parent::getAttributes(), - array( - 'maxlength' => $this->getMaxLength(), - 'size' => ($this->getMaxLength()) ? min($this->getMaxLength(), 30) : null - ) + $attributes ); } + /** + * @return string + */ public function InternallyLabelledField() { - if(!$this->value) $this->value = $this->Title(); + if(!$this->value) { + $this->value = $this->Title(); + } + return $this->Field(); } - }