1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 14:00:56 +01:00

Normalize validation/requried API

Summary: Makes Maniphest look more like standard fields to make the migration easier.

Test Plan: Edited tasks and users with required and invalid fields.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D7035
This commit is contained in:
epriestley 2013-09-19 11:55:54 -07:00
parent 30159d9ad9
commit 2088b99078
8 changed files with 76 additions and 76 deletions

View file

@ -1063,7 +1063,6 @@ phutil_register_library_map(array(
'PhabricatorCustomFieldNumericIndexStorage' => 'infrastructure/customfield/storage/PhabricatorCustomFieldNumericIndexStorage.php', 'PhabricatorCustomFieldNumericIndexStorage' => 'infrastructure/customfield/storage/PhabricatorCustomFieldNumericIndexStorage.php',
'PhabricatorCustomFieldStorage' => 'infrastructure/customfield/storage/PhabricatorCustomFieldStorage.php', 'PhabricatorCustomFieldStorage' => 'infrastructure/customfield/storage/PhabricatorCustomFieldStorage.php',
'PhabricatorCustomFieldStringIndexStorage' => 'infrastructure/customfield/storage/PhabricatorCustomFieldStringIndexStorage.php', 'PhabricatorCustomFieldStringIndexStorage' => 'infrastructure/customfield/storage/PhabricatorCustomFieldStringIndexStorage.php',
'PhabricatorCustomFieldValidationException' => 'infrastructure/customfield/exception/PhabricatorCustomFieldValidationException.php',
'PhabricatorDaemon' => 'infrastructure/daemon/PhabricatorDaemon.php', 'PhabricatorDaemon' => 'infrastructure/daemon/PhabricatorDaemon.php',
'PhabricatorDaemonCombinedLogController' => 'applications/daemon/controller/PhabricatorDaemonCombinedLogController.php', 'PhabricatorDaemonCombinedLogController' => 'applications/daemon/controller/PhabricatorDaemonCombinedLogController.php',
'PhabricatorDaemonConsoleController' => 'applications/daemon/controller/PhabricatorDaemonConsoleController.php', 'PhabricatorDaemonConsoleController' => 'applications/daemon/controller/PhabricatorDaemonConsoleController.php',
@ -3182,7 +3181,6 @@ phutil_register_library_map(array(
'PhabricatorCustomFieldNumericIndexStorage' => 'PhabricatorCustomFieldIndexStorage', 'PhabricatorCustomFieldNumericIndexStorage' => 'PhabricatorCustomFieldIndexStorage',
'PhabricatorCustomFieldStorage' => 'PhabricatorLiskDAO', 'PhabricatorCustomFieldStorage' => 'PhabricatorLiskDAO',
'PhabricatorCustomFieldStringIndexStorage' => 'PhabricatorCustomFieldIndexStorage', 'PhabricatorCustomFieldStringIndexStorage' => 'PhabricatorCustomFieldIndexStorage',
'PhabricatorCustomFieldValidationException' => 'Exception',
'PhabricatorDaemon' => 'PhutilDaemon', 'PhabricatorDaemon' => 'PhutilDaemon',
'PhabricatorDaemonCombinedLogController' => 'PhabricatorDaemonController', 'PhabricatorDaemonCombinedLogController' => 'PhabricatorDaemonController',
'PhabricatorDaemonConsoleController' => 'PhabricatorDaemonController', 'PhabricatorDaemonConsoleController' => 'PhabricatorDaemonController',

View file

@ -273,42 +273,62 @@ class ManiphestAuxiliaryFieldDefaultSpecification
return $this->setValue($value); return $this->setValue($value);
} }
public function validate() { public function validateApplicationTransactions(
PhabricatorApplicationTransactionEditor $editor,
$type,
array $xactions) {
$errors = parent::validateApplicationTransactions(
$editor,
$type,
$xactions);
$has_value = false;
$value = null;
foreach ($xactions as $xaction) {
$has_value = true;
$value = $xaction->getNewValue();
switch ($this->getFieldType()) { switch ($this->getFieldType()) {
case self::TYPE_INT: case self::TYPE_INT:
if ($this->getValue() && !is_numeric($this->getValue())) { if ($value && !is_numeric($value)) {
throw new PhabricatorCustomFieldValidationException( $errors[] = new PhabricatorApplicationTransactionValidationError(
pht( $type,
'%s must be an integer value.', pht('Invalid'),
$this->getLabel())); pht('%s must be an integer value.', $this->getLabel()),
$xaction);
$this->setError(pht('Invalid'));
} }
break; break;
case self::TYPE_BOOL:
return true;
case self::TYPE_STRING:
return true;
case self::TYPE_SELECT:
return true;
case self::TYPE_DATE: case self::TYPE_DATE:
if ((int)$this->getValue() <= 0 && $this->isRequired()) { if ((int)$value <= 0 && $this->isRequired()) {
throw new PhabricatorCustomFieldValidationException( $errors[] = new PhabricatorApplicationTransactionValidationError(
pht( $type,
'%s must be a valid date.', pht('Invalid'),
$this->getLabel())); pht('%s must be a valid date.', $this->getLabel()),
} $xaction);
break; $this->setError(pht('Invalid'));
case self::TYPE_USER:
case self::TYPE_USERS:
if (!is_array($this->getValue())) {
throw new PhabricatorCustomFieldValidationException(
pht(
'%s is not a valid list of user PHIDs.',
$this->getLabel()));
} }
break; break;
} }
} }
if ($this->isRequired()) {
if (!$has_value) {
$value = $this->getOldValueForApplicationTransactions();
}
if (!$value) {
$errors[] = new PhabricatorApplicationTransactionValidationError(
$type,
pht('Required'),
pht('%s is required.', $this->getLabel()),
null);
$this->setError(pht('Required'));
}
}
return $errors;
}
public function setDefaultValue($value) { public function setDefaultValue($value) {
switch ($this->getFieldType()) { switch ($this->getFieldType()) {
case self::TYPE_DATE: case self::TYPE_DATE:

View file

@ -63,10 +63,6 @@ abstract class ManiphestAuxiliaryFieldSpecification
return $this->value; return $this->value;
} }
public function validate() {
return true;
}
public function isRequired() { public function isRequired() {
return false; return false;
} }
@ -173,7 +169,11 @@ abstract class ManiphestAuxiliaryFieldSpecification
} }
public function renderPropertyViewValue() { public function renderPropertyViewValue() {
return $this->renderForDetailView(); $value = $this->renderForDetailView();
if (!strlen($value)) {
return null;
}
return $value;
} }
public function renderPropertyViewLabel() { public function renderPropertyViewLabel() {

View file

@ -142,22 +142,24 @@ final class ManiphestTaskEditController extends ManiphestController {
$aux_field->readValueFromRequest($request); $aux_field->readValueFromRequest($request);
$aux_new_value = $aux_field->getNewValueForApplicationTransactions(); $aux_new_value = $aux_field->getNewValueForApplicationTransactions();
// TODO: What's going on here? // TODO: We're faking a call to the ApplicaitonTransaction validation
if ((int)$aux_old_value === $aux_new_value) { // logic here. We need valid objects to pass, but they aren't used
unset($aux_fields[$aux_arr_key]); // in a meaningful way. For now, build User objects. Once the Maniphest
continue; // objects exist, this will switch over automatically. This is a big
} // hack but shouldn't be long for this world.
$placeholder_editor = new PhabricatorUserProfileEditor();
if ($aux_field->isRequired() && !$aux_field->getValue()) { $field_errors = $aux_field->validateApplicationTransactions(
$errors[] = pht('%s is required.', $aux_field->getLabel()); $placeholder_editor,
$aux_field->setError(pht('Required')); PhabricatorTransactions::TYPE_CUSTOMFIELD,
} array(
id(new PhabricatorUserTransaction())
->setOldValue($aux_old_value)
->setNewValue($aux_new_value),
));
try { foreach ($field_errors as $error) {
$aux_field->validate(); $errors[] = $error->getMessage();
} catch (Exception $e) {
$errors[] = $e->getMessage();
$aux_field->setError(pht('Invalid'));
} }
$old_values[$aux_field->getFieldKey()] = $aux_old_value; $old_values[$aux_field->getFieldKey()] = $aux_old_value;
@ -490,12 +492,6 @@ final class ManiphestTaskEditController extends ManiphestController {
->setDatasource('/typeahead/common/projects/')); ->setDatasource('/typeahead/common/projects/'));
foreach ($aux_fields as $aux_field) { foreach ($aux_fields as $aux_field) {
if ($aux_field->isRequired() &&
!$aux_field->getError() &&
!$aux_field->getValue()) {
$aux_field->setError(true);
}
$aux_control = $aux_field->renderEditControl(); $aux_control = $aux_field->renderEditControl();
$form->appendChild($aux_control); $form->appendChild($aux_control);
} }

View file

@ -36,14 +36,6 @@ abstract class ManiphestCustomField
public function setHandles(array $handles) { public function setHandles(array $handles) {
} }
public function isRequired() {
return false;
}
public function validate() {
return true;
}
/** /**
* Render a verb to appear in email titles when a transaction involving this * Render a verb to appear in email titles when a transaction involving this
* field occurs. Specifically, Maniphest emails are formatted like this: * field occurs. Specifically, Maniphest emails are formatted like this:

View file

@ -1,6 +0,0 @@
<?php
final class PhabricatorCustomFieldValidationException
extends Exception {
}

View file

@ -271,7 +271,7 @@ abstract class PhabricatorStandardCustomField
$xactions); $xactions);
if ($this->getRequired()) { if ($this->getRequired()) {
$value = null; $value = $this->getOldValueForApplicationTransactions();
$transaction = null; $transaction = null;
foreach ($xactions as $xaction) { foreach ($xactions as $xaction) {
$value = $xaction->getNewValue(); $value = $xaction->getNewValue();

View file

@ -63,7 +63,7 @@ final class PhabricatorStandardCustomFieldDate
->setName($this->getFieldKey()) ->setName($this->getFieldKey())
->setUser($this->getViewer()) ->setUser($this->getViewer())
->setCaption($this->getCaption()) ->setCaption($this->getCaption())
->setAllowNull(true); ->setAllowNull(!$this->getRequired());
$control->setValue($this->getFieldValue()); $control->setValue($this->getFieldValue());