diff --git a/forms/NullableField.php b/forms/NullableField.php index ad1489121..5fa41f50d 100644 --- a/forms/NullableField.php +++ b/forms/NullableField.php @@ -1,75 +1,98 @@ Value() or $field->dataValue() - * - * You can specify the label to use for the "is null" checkbox. If you want to use I8N for this label then specify it - * like this: - * - * $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null"); - * + * + * You can specify the label to use for the "is null" checkbox. If you want to use i18n for this + * label then specify it like this: + * + * $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null")); + * * @author Pete Bacon Darwin + * * @package forms * @subpackage fields-basic */ class NullableField extends FormField { /** * The field that holds the value of this field + * * @var FormField */ protected $valueField; - + /** * The label to show next to the is null check box. + * * @var string */ protected $isNullLabel; - /** * Create a new nullable field - * @param $valueField - * @return NullableField + * + * @param FormField $valueField + * @param null|string $isNullLabel */ public function __construct(FormField $valueField, $isNullLabel = null) { $this->valueField = $valueField; - $this->isNullLabel = $isNullLabel; - if ( is_null($this->isNullLabel) ) { - // Set a default label if one is not provided. + + if(isset($isNullLabel)) { + $this->setIsNullLabel($isNullLabel); + } else { $this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null'); } - parent::__construct($valueField->getName(), $valueField->Title(), $valueField->Value(), - $valueField->getForm(), $valueField->RightTitle()); - $this->readonly = $valueField->isReadonly(); + + parent::__construct( + $valueField->getName(), + $valueField->Title(), + $valueField->Value() + ); + + $this->setForm($valueField->getForm()); + $this->setRightTitle($valueField->RightTitle()); + $this->setReadonly($valueField->isReadonly()); } - + /** * Get the label used for the Is Null checkbox. + * * @return string */ public function getIsNullLabel() { return $this->isNullLabel; } + /** * Set the label used for the Is Null checkbox. + * * @param $isNulLabel string + * + * @return $this */ - public function setIsNullLabel(string $isNulLabel){ + public function setIsNullLabel($isNulLabel) { $this->isNullLabel = $isNulLabel; + return $this; } - + /** * Get the id used for the Is Null check box. + * * @return string */ public function getIsNullId() { @@ -77,53 +100,80 @@ class NullableField extends FormField { } /** - * (non-PHPdoc) - * @see framework/forms/FormField#Field() + * @param array $properties + * + * @return string */ public function Field($properties = array()) { - if ( $this->isReadonly()) { + if($this->isReadonly()) { $nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId()); } else { $nullableCheckbox = new CheckboxField($this->getIsNullId()); } + $nullableCheckbox->setValue(is_null($this->dataValue())); - return $this->valueField->Field() . ' ' . $nullableCheckbox->Field() - . ' ' . $this->getIsNullLabel().''; + return sprintf( + '%s %s %s', + $this->valueField->Field(), + $nullableCheckbox->Field(), + $this->getIsNullLabel() + ); } /** * Value is sometimes an array, and sometimes a single value, so we need to handle both cases + * + * @param mixed $value + * @param null|array $data + * + * @return $this */ public function setValue($value, $data = null) { - if ( is_array($data) && array_key_exists($this->getIsNullId(), $data) && $data[$this->getIsNullId()] ) { + $id = $this->getIsNullId(); + + if(is_array($data) && array_key_exists($id, $data) && $data[$id]) { $value = null; } + $this->valueField->setValue($value); + parent::setValue($value); return $this; } - + /** - * (non-PHPdoc) - * @see forms/FormField#setName($name) + * @param string $name + * + * @return $this */ public function setName($name) { - // We need to pass through the name change to the underlying value field. $this->valueField->setName($name); + parent::setName($name); return $this; } /** - * (non-PHPdoc) - * @see framework/forms/FormField#debug() + * @return string */ public function debug() { - $result = "$this->class ($this->name: $this->title : $this->message) = "; - $result .= (is_null($this->value)) ? "<>" : $this->value; - return result; + $result = sprintf( + '%s (%s: $s : %s) = ', + $this->class, + $this->name, + $this->title, + $this->message + ); + + if($this->value === null) { + $result .= "<>"; + } else { + $result .= (string) $this->value; + } + + return $result; } }