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

Helper method for max text field length and validate alias length

Summary: Ref T8992, Validate alias text field length.

Test Plan: Create Phurl with alias of more than 64 characters. Get error. Reduce length of alias to successfully save Phurl.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Maniphest Tasks: T8992

Differential Revision: https://secure.phabricator.com/D14403
This commit is contained in:
lkassianik 2015-11-04 09:58:44 -08:00
parent e80970eba0
commit 5c6d2be18f
2 changed files with 58 additions and 0 deletions

View file

@ -119,6 +119,19 @@ final class PhabricatorPhurlURLEditor
}
break;
case PhabricatorPhurlURLTransaction::TYPE_ALIAS:
$overdrawn = $this->validateIsTextFieldTooLong(
$object->getName(),
$xactions,
64);
if ($overdrawn) {
$errors[] = new PhabricatorApplicationTransactionValidationError(
$type,
pht('Alias Too Long'),
pht('The alias can be no longer than 64 characters.'),
nonempty(last($xactions), null));
}
foreach ($xactions as $xaction) {
if ($xaction->getOldValue() != $xaction->getNewValue()) {
$new_alias = $xaction->getNewValue();

View file

@ -2177,6 +2177,51 @@ abstract class PhabricatorApplicationTransactionEditor
return true;
}
/**
* Check that text field input isn't longer than a specified length.
*
* A text field input is invalid if the length of the input is longer than a
* specified length. This length can be determined by the space allotted in
* the database, or given arbitrarily.
* This method is intended to make implementing @{method:validateTransaction}
* more convenient:
*
* $overdrawn = $this->validateIsTextFieldTooLong(
* $object->getName(),
* $xactions,
* $field_length);
*
* This will return `true` if the net effect of the object and transactions
* is a field that is too long.
*
* @param wild Current field value.
* @param list<PhabricatorApplicationTransaction> Transactions editing the
* field.
* @param integer for maximum field length.
* @return bool True if the field will be too long after edits.
*/
protected function validateIsTextFieldTooLong(
$field_value,
array $xactions,
$length) {
if ($xactions) {
$new_value_length = phutil_utf8_strlen(last($xactions)->getNewValue());
if ($new_value_length <= $length) {
return false;
} else {
return true;
}
}
$old_value_length = phutil_utf8_strlen($field_value);
if ($old_value_length <= $length) {
return false;
}
return true;
}
/* -( Implicit CCs )------------------------------------------------------- */