JReviews logo
Guides My site
← All guides Community guide

Validate listing and review text length

E Alejandro
Thread started by Ernest. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Limit the length of listing summaries, descriptions, and review comments before they save.

My site

Platform
JReviews version

Stop visitors from submitting oversized listing summaries, descriptions, or review comments. The examples below allow at most 5,000 plain-text characters in each field you keep.

Choose the fields and limits

Choose the maximum for each field. Replace 5000 and its error message together. These examples require PHP's mbstring extension, which counts characters in text such as accented and non-Latin words correctly. Confirm that your hosting account has mbstring enabled before adding the code; your host can check this PHP setting.

These complete examples enforce a maximum only. A minimum requires visitors to write more text and needs a separate validation rule. Have a PHP developer add and test that rule if your site needs one.

Start a new override file with <?php followed by defined('MVC_FRAMEWORK') or die;. In an existing file, add the callbacks without repeating those lines.

JReviews 6 Early Access

Validate the listing summary and description

Enable Listing Form in Early Access.

Joomla. Use templates/jreviews_overrides/hooks.php.

WordPress. Use jreviews_overrides/hooks.php.

fwd_add_action('jreviews:listing_form.validation', function ($validator, $creating): void {
    $validator->after(function ($validator): void {
        foreach (['summary', 'description'] as $field) {
            $text = strip_tags((string) ($validator->getData()[$field] ?? ''));
            if (mb_strlen($text) > 5000) {
                $validator->errors()->add($field, ucfirst($field).' needs at most 5000 characters.');
            }
        }
    });
}, 20, 2);

To limit only the summary, change ['summary', 'description'] to ['summary']. To limit only the description, change it to ['description']. Missing or empty values pass this maximum-length rule; the form's own required-field settings still apply.

Validate review comments

Enable Listing Detail Page in Early Access for review-comment validation.

fwd_add_action('jreviews:comment_form.validation', function ($validator, $creating): void {
    $validator->after(function ($validator): void {
        $text = strip_tags((string) ($validator->getData()['comments'] ?? ''));
        if (mb_strlen($text) > 5000) {
            $validator->errors()->add('comments', 'The review comment needs at most 5000 characters.');
        }
    });
}, 20, 2);

JReviews 5 and v6 Legacy

Validate the listing summary and description

Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.

WordPress. Use jreviews_overrides/filters/filter_functions.php.

Add only the callbacks for fields you want to limit. Empty values pass the maximum-length rule; the form's own required-field settings still apply.

Listing summary

function maximum_summary_length($validation, $params)
{
    $summary = S2Array::get($params,'data.Listing.introtext');

    $summary = strip_tags((string) $summary);

    if ( mb_strlen($summary) > 5000 )
    {
        $validation[] = 'The summary needs to be at most 5000 characters';
    }

    return $validation;
}

Clickfwd\Hook\Filter::add('listing_submit_validation', 'maximum_summary_length', 10);

Listing description

function maximum_description_length($validation, $params)
{
    $description = S2Array::get($params,'data.Listing.fulltext');

    $description = strip_tags((string) $description);

    if ( mb_strlen($description) > 5000 )
    {
        $validation[] = 'The description needs to be at most 5000 characters';
    }

    return $validation;
}

Clickfwd\Hook\Filter::add('listing_submit_validation', 'maximum_description_length', 10);

Validate review comments

function maximum_comment_length($validation, $params)
{
    $comment = S2Array::get($params,'data.Review.comments');

    $comment = strip_tags((string) $comment);

    if ( mb_strlen($comment) > 5000 )
    {
        $validation[] = 'The review comment needs to be at most 5000 characters';
    }

    return $validation;
}

Clickfwd\Hook\Filter::add('review_submit_validation', 'maximum_comment_length', 10);

The callbacks apply when visitors create or edit content through these forms.

Update the character limit and error message together

Change 5000 and its matching error message together. These examples count characters after removing HTML tags. Minimum-length and word-count rules are optional PHP developer adaptations; they are not part of the copyable maximum-length examples.

Test boundaries and failed submissions

For the examples above, submit 5,000 and 5,001 plain-text characters in each selected field. The first value passes this rule. The second displays its error and does not save. Repeat on an edit and with multibyte text. Confirm that HTML tags are not counted and hidden fields do not prevent submission. If you add a 100-character minimum, also test 99 and 100 characters and HTML-only input. These checks run when the form is submitted. They do not display a live character counter.

Learn more