Skip to content

Commit 09682e9

Browse files
committed
fix(textarea): support for min/max size and regex
1 parent d77cde0 commit 09682e9

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

inc/field/textareafield.class.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,40 @@ public function isValid(): bool {
243243
return false;
244244
}
245245

246-
// All is OK
246+
return $this->isValidValue($this->value);
247+
}
248+
249+
public function isValidValue($value): bool {
250+
if (strlen($value) == 0) {
251+
return true;
252+
}
253+
254+
$parameters = $this->getParameters();
255+
256+
$stripped = strip_tags($value);
257+
258+
// Check the field matches the format regex
259+
$regex = $parameters['regex']->fields['regex'];
260+
if ($regex !== null && strlen($regex) > 0) {
261+
if (!preg_match($regex, $stripped)) {
262+
Session::addMessageAfterRedirect(sprintf(__('Specific format does not match: %s', 'formcreator'), $this->question->fields['name']), false, ERROR);
263+
return false;
264+
}
265+
}
266+
267+
// Check the field is in the range
268+
$rangeMin = $parameters['range']->fields['range_min'];
269+
$rangeMax = $parameters['range']->fields['range_max'];
270+
if ($rangeMin > 0 && strlen($stripped) < $rangeMin) {
271+
Session::addMessageAfterRedirect(sprintf(__('The text is too short (minimum %d characters): %s', 'formcreator'), $rangeMin, $this->question->fields['name']), false, ERROR);
272+
return false;
273+
}
274+
275+
if ($rangeMax > 0 && strlen($stripped) > $rangeMax) {
276+
Session::addMessageAfterRedirect(sprintf(__('The text is too long (maximum %d characters): %s', 'formcreator'), $rangeMax, $this->question->fields['name']), false, ERROR);
277+
return false;
278+
}
279+
247280
return true;
248281
}
249282

0 commit comments

Comments
 (0)