ENH Use text field's title for validation messages.

Where a field has a title, this is preferred over the field name in validation messages, as that is the "name" of the field as presented to the user.
This commit is contained in:
GuySartorelli 2021-06-29 17:45:01 +12:00 committed by GitHub
parent 82e0d8f24b
commit e4e4b0924d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -142,12 +142,13 @@ class TextField extends FormField implements TippableFieldInterface
public function validate($validator) public function validate($validator)
{ {
if (!is_null($this->maxLength) && mb_strlen($this->value) > $this->maxLength) { if (!is_null($this->maxLength) && mb_strlen($this->value) > $this->maxLength) {
$name = strip_tags($this->Title() ? $this->Title() : $this->getName());
$validator->validationError( $validator->validationError(
$this->name, $this->name,
_t( _t(
'SilverStripe\\Forms\\TextField.VALIDATEMAXLENGTH', 'SilverStripe\\Forms\\TextField.VALIDATEMAXLENGTH',
'The value for {name} must not exceed {maxLength} characters in length', 'The value for {name} must not exceed {maxLength} characters in length',
['name' => $this->getName(), 'maxLength' => $this->maxLength] ['name' => $name, 'maxLength' => $this->maxLength]
), ),
"validation" "validation"
); );