Skip to content

Commit

Permalink
fix(textarea): support for min/max size and regex
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jan 8, 2024
1 parent d77cde0 commit 09682e9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion inc/field/textareafield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,40 @@ public function isValid(): bool {
return false;
}

// All is OK
return $this->isValidValue($this->value);
}

public function isValidValue($value): bool {
if (strlen($value) == 0) {
return true;
}

$parameters = $this->getParameters();

$stripped = strip_tags($value);

// Check the field matches the format regex
$regex = $parameters['regex']->fields['regex'];
if ($regex !== null && strlen($regex) > 0) {
if (!preg_match($regex, $stripped)) {
Session::addMessageAfterRedirect(sprintf(__('Specific format does not match: %s', 'formcreator'), $this->question->fields['name']), false, ERROR);
return false;
}
}

// Check the field is in the range
$rangeMin = $parameters['range']->fields['range_min'];
$rangeMax = $parameters['range']->fields['range_max'];
if ($rangeMin > 0 && strlen($stripped) < $rangeMin) {
Session::addMessageAfterRedirect(sprintf(__('The text is too short (minimum %d characters): %s', 'formcreator'), $rangeMin, $this->question->fields['name']), false, ERROR);
return false;
}

if ($rangeMax > 0 && strlen($stripped) > $rangeMax) {
Session::addMessageAfterRedirect(sprintf(__('The text is too long (maximum %d characters): %s', 'formcreator'), $rangeMax, $this->question->fields['name']), false, ERROR);
return false;
}

return true;
}

Expand Down

0 comments on commit 09682e9

Please sign in to comment.