HomePhabricator

Provide an Editor extension point for transaction validation

Description

Provide an Editor extension point for transaction validation

Summary:
Depends on D20040. Ref T13242. See PHI1039. See PHI873. Two reasonable cases have arisen recently where extending validation rules would help solve problems.

We can do this in a pretty straightforward way with a standard extension pattern.

Test Plan:
Used this extension to keep ducks away from projects:

<?php

final class NoDucksEditorExtension
  extends PhabricatorEditorExtension {

  const EXTENSIONKEY = 'no.ducks';

  public function getExtensionName() {
    return pht('No Ducks!');
  }

  public function supportsObject(
    PhabricatorApplicationTransactionEditor $editor,
    PhabricatorApplicationTransactionInterface $object) {
    return ($object instanceof PhabricatorProject);
  }

  public function validateTransactions($object, array $xactions) {
    $errors = array();

    $name_type = PhabricatorProjectNameTransaction::TRANSACTIONTYPE;

    $old_value = $object->getName();
    foreach ($xactions as $xaction) {
      if ($xaction->getTransactionType() !== $name_type) {
        continue;
      }

      $new_value = $xaction->getNewValue();
      if ($old_value === $new_value) {
        continue;
      }

      if (preg_match('/duck/i', $new_value)) {
        $errors[] = $this->newInvalidTransactionError(
          $xaction,
          pht('Project names must not contain the substring "duck".'));
      }
    }

    return $errors;
  }

}

Screen Shot 2019-01-27 at 10.28.48 AM.png (1×1 px, 217 KB)

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13242

Differential Revision: https://secure.phabricator.com/D20041

Details

Provenance
epriestleyAuthored on Jan 27 2019, 6:21 PM
epriestleyPushed on Jan 30 2019, 2:18 PM
Reviewer
amckinley
Differential Revision
D20041: Provide an Editor extension point for transaction validation
Parents
rPc9760e8d643b: Support subtypes in Projects
Branches
Unknown
Tags
Unknown
Tasks
T13242: 2019 Week 5 Bonus Content
Build Status
Buildable 21761
Build 29695: Run Core Tests