From c5b80985df528955e3697b88c623bae4e0a4eaf1 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 18 Sep 2013 15:32:14 -0700 Subject: [PATCH] Allow standard fields to be required Summary: Ref T418. Test Plan: See screenshot. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T418 Differential Revision: https://secure.phabricator.com/D7030 --- .../PhabricatorStandardCustomField.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/infrastructure/customfield/standard/PhabricatorStandardCustomField.php b/src/infrastructure/customfield/standard/PhabricatorStandardCustomField.php index 69076a92c0..7d73234655 100644 --- a/src/infrastructure/customfield/standard/PhabricatorStandardCustomField.php +++ b/src/infrastructure/customfield/standard/PhabricatorStandardCustomField.php @@ -12,6 +12,8 @@ abstract class PhabricatorStandardCustomField private $strings; private $caption; private $fieldError; + private $required; + private $default; abstract public function getFieldType(); @@ -102,6 +104,10 @@ abstract class PhabricatorStandardCustomField case 'caption': $this->setCaption($value); break; + case 'required': + $this->setRequired($value); + $this->setFieldError(true); + break; case 'type': // We set this earlier on. break; @@ -124,6 +130,15 @@ abstract class PhabricatorStandardCustomField return $this->fieldError; } + public function setRequired($required) { + $this->required = $required; + return $this; + } + + public function getRequired() { + return $this->required; + } + /* -( PhabricatorCustomField )--------------------------------------------- */ @@ -252,7 +267,34 @@ abstract class PhabricatorStandardCustomField $type, $xactions); + if ($this->getRequired()) { + $value = null; + $transaction = null; + foreach ($xactions as $xaction) { + $value = $xaction->getNewValue(); + if (!$this->isValueEmpty($value)) { + $transaction = $xaction; + break; + } + } + if ($this->isValueEmpty($value)) { + $errors[] = new PhabricatorApplicationTransactionValidationError( + $type, + pht('Required'), + pht('%s is required.', $this->getFieldName()), + $transaction); + $this->setFieldError(pht('Required')); + } + } + return $errors; } + protected function isValueEmpty($value) { + if (is_array($value)) { + return empty($value); + } + return !strlen($value); + } + }