Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set default maximum length for text fields
  • Loading branch information
mahagr committed Mar 17, 2022
1 parent c083410 commit 3e7f67f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -3,8 +3,13 @@

1. [](#new)
* Added `|replace_last(search, replace)` filter
1. [](#improved)
2. [](#improved)
* Added multi-language support for page routes in `Utils::url()`
* Set default maximum length for text fields
- `password`: 256
- `email`: 320
- `text`, `url`, `hidden`, `commalist`: 2048
- `text` (multiline), `textarea`: 65536

# v1.7.31
## 03/14/2022
Expand Down
22 changes: 20 additions & 2 deletions system/src/Grav/Common/Data/Validation.php
Expand Up @@ -246,7 +246,9 @@ public static function typeText($value, array $params, array $field)
return false;
}

$max = (int)($params['max'] ?? 0);
$multiline = isset($params['multiline']) && $params['multiline'];

$max = (int)($params['max'] ?? ($multiline ? 65536 : 2048));
if ($max && $len > $max) {
return false;
}
Expand All @@ -256,7 +258,7 @@ public static function typeText($value, array $params, array $field)
return false;
}

if ((!isset($params['multiline']) || !$params['multiline']) && preg_match('/\R/um', $value)) {
if (!$multiline && preg_match('/\R/um', $value)) {
return false;
}

Expand Down Expand Up @@ -317,6 +319,10 @@ protected static function filterCommaList($value, array $params, array $field)
*/
public static function typeCommaList($value, array $params, array $field)
{
if (!isset($params['max'])) {
$params['max'] = 2048;
}

return is_array($value) ? true : self::typeText($value, $params, $field);
}

Expand Down Expand Up @@ -379,6 +385,10 @@ public static function typeTextarea($value, array $params, array $field)
*/
public static function typePassword($value, array $params, array $field)
{
if (!isset($params['max'])) {
$params['max'] = 256;
}

return self::typeText($value, $params, $field);
}

Expand Down Expand Up @@ -621,6 +631,10 @@ public static function typeColor($value, array $params, array $field)
*/
public static function typeEmail($value, array $params, array $field)
{
if (!isset($params['max'])) {
$params['max'] = 320;
}

$values = !is_array($value) ? explode(',', preg_replace('/\s+/', '', $value)) : $value;

foreach ($values as $val) {
Expand All @@ -642,6 +656,10 @@ public static function typeEmail($value, array $params, array $field)
*/
public static function typeUrl($value, array $params, array $field)
{
if (!isset($params['max'])) {
$params['max'] = 2048;
}

return self::typeText($value, $params, $field) && filter_var($value, FILTER_VALIDATE_URL);
}

Expand Down

0 comments on commit 3e7f67f

Please sign in to comment.