Validating textarea

aijosh

Member
I'm trying to ensure a minimum number of text users should enter. The validation kept failing even though user passed conditions. So I decided to display the value of strlen and I'm seeing a difference of over 100 characters. strlen is not counting the spaces. I also tried str_word_count() and I get 1 as the word count.

$value = $this->data['tablename___textarea'];
$minLength = 400;

if (strlen($value) < $minLength) {
$thisValidation->setMessage($value);
return false; // Validation failed
}

return true; // Validation passed

How am I to properly validate this?
 
Last edited:
string(261) "Imtryingtoensureaminimumnumberoftextusersshouldenter.Thevalidationkeptfailingeventhoughuserpassedconditions.SoIdecidedtodisplaythevalueofstrlenandImseeingadifferenceofover100characters.strlenisnotcountingthespaces.Ialsotriedstr_word_countandIget1asthewordcount."
 
So you are getting the correct string length. You notice though that you are returning false so the validation fails. Your comment states that the validation passed which contradicts what you are returning.
 
No. The string lenght is wrong because it is counting wrong.

"Fabrik Base 4.5 is now available." is not the same as "FabrikBase4.5isnowavailable."

The textarea on the frontend shows the user a count of 33 characters but the validation treats it as 28 characters
 
Was using JCE.

Changed to
1. None
2. TinyMCE
3. Codemirror

Same results

Text entered in text area on frontend = Fabrik Base 4.5 is now available.

1. The counter on the textarea in the front end counts 33. (Actually 34 because it counts 1 when empty)

2. var_dump($value);
string(28) "FabrikBase4.5isnowavailable."

3. Content of Database
Fabrik Base 4.5 is now available.
 
I'm trying to ensure a minimum number of text users should enter. The validation kept failing even though user passed conditions. So I decided to display the value of strlen and I'm seeing a difference of over 100 characters. strlen is not counting the spaces. I also tried str_word_count() and I get 1 as the word count.



How am I to properly validate this?
Are you using php validation?
use the placeholder instead of $value = $this->data['tablename___textarea'];
Code:
$value = '{tablename___textarea}'; // Use placeholder
$minLength = 32;

if (strlen($value) < $minLength) {
    $thisValidation->setMessage($value);
    return false; // Validation failed
}

return true; // Validation passed

so if I write:
"Fabrik Base 4.5 is now available" (32 characters)
the validation is correct

while if I write:
"Fabrik Base 4.5 is now availabl" (31 characters)

validation fails

in your example in case of error it returns the value of the textarea field
 
Using placeholder works. :oops:
$value = '{tablename___textarea}';

The documentation might need to be reviewed because I intentinally did not use the placeholder and followed what the wiki said.

https://fabrikar.com/forums/index.php?wiki/php-validation/
PHP -PHP code to run.
  • The element's submitted value is found in the variable $data.
  • Other element values can be found in $_REQUEST (use JFactory::getApplication()->input->get() to access).
 
Sorry, I hadn't seen the documentation before.
It actually says that placeholders can be used in the PHP code and in the condition fields.
The use of:$value = $this->data['tablename___textarea'];is to directly access values in other contexts.

In any case, the wiki is open. Feel free to update and improve it (even with new examples).
 
Back
Top