From 99cc9f8be7846c9b0076a0259b10709ef3ce579e Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Tue, 19 Mar 2019 10:52:47 -0400 Subject: [PATCH] Add support for `noTrim` option to InputfieldText/InputfieldTextarea per processwire/processwire-issues#459 --- wire/modules/Inputfield/InputfieldText.module | 7 +++++-- wire/modules/Inputfield/InputfieldTextarea.module | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/wire/modules/Inputfield/InputfieldText.module b/wire/modules/Inputfield/InputfieldText.module index 40988af2..a914901c 100644 --- a/wire/modules/Inputfield/InputfieldText.module +++ b/wire/modules/Inputfield/InputfieldText.module @@ -11,6 +11,7 @@ * @property string $pattern HTML5 pattern attribute * @property string $initValue Optional initial/default value * @property bool $stripTags Should HTML tags be stripped from value? + * @property bool $noTrim By default whitespace is trimmed from value, set this to true to bypass that behavior (default=false). * @property bool $useLanguages When combined with multi-language support, setting this to true will provide one input per language. Get/set each language value with the "value[languageID]" property, and just "value" for default language. * @property bool|int $requiredAttr When combined with "required" option, this also makes it use the HTML5 "required" attribute. (default=false) * @property int $showCount Show a character counter (1) or word counter (2) or neither (0). Recommended value is 1 when using minlength or maxlength. @@ -51,6 +52,7 @@ class InputfieldText extends Inputfield { $this->set('requiredAttr', 0); $this->set('initValue', ''); // optional initial value $this->set('stripTags', false); // strip tags from input? + $this->set('noTrim', false); $this->set('showCount', self::showCountNone); // if multi-language, support placeholders for each language @@ -163,7 +165,7 @@ class InputfieldText extends Inputfield { * * @param array|string $key * @param array|int|string $value - * @return $this + * @return Inputfield|InputfieldText|$this * */ public function setAttribute($key, $value) { @@ -195,6 +197,7 @@ class InputfieldText extends Inputfield { 'maxLength' => $this->maxlength, 'maxBytes' => $this->maxlength*4, 'stripTags' => false, + 'trim' => $this->noTrim ? false : true, )); } @@ -251,7 +254,7 @@ class InputfieldText extends Inputfield { } if($maxlength > 0) { - $dirtyValue = trim($input->$name); + $dirtyValue = $this->noTrim ? (string) $input->$name : trim($input->$name); $dirtyLength = $this->getValueLength($dirtyValue); if($dirtyLength > $maxlength) { $this->error(sprintf( diff --git a/wire/modules/Inputfield/InputfieldTextarea.module b/wire/modules/Inputfield/InputfieldTextarea.module index a35226b3..02abae61 100644 --- a/wire/modules/Inputfield/InputfieldTextarea.module +++ b/wire/modules/Inputfield/InputfieldTextarea.module @@ -110,6 +110,7 @@ class InputfieldTextarea extends InputfieldText { 'maxLength' => $maxlength, 'maxBytes' => $maxlength*4, 'stripTags' => false, + 'trim' => $this->noTrim ? false : true )); } else { if(strpos($value, "\r\n") !== false) { @@ -117,7 +118,7 @@ class InputfieldTextarea extends InputfieldText { } } if($this->stripTags) $value = strip_tags($value); - return trim($value); + return $this->noTrim ? $value : trim($value); } /**