Skip Navigation

[Resolved] Post submission form – Add minimum number of words to textarea

This thread is resolved. Here is a description of the problem and solution.

Problem:

Is it possible to set a minimum number of words to a textarea in Cred Post form?

[php]
[cred_generic_field field='description' type='textarea' class='form-control' urlparam='']
{
"required":1,
"validate_format":0,
"default":""
}
[/cred_generic_field]

Solution:

It is possible with Forms API hook: cred_form_validate, for example:

https://toolset.com/forums/topic/post-submission-form-add-minimum-number-of-words-to-textarea/#post-1142918

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

http://php.net/manual/en/function.mb-strlen.php

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 3 voices.

Last updated by leilaG 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1140570

Hello, is it possible to set a minimum number of words to a textarea in Cred Post form?

<div class="form-group"><label>Description</label>
[cred_generic_field field='description' type='textarea' class='form-control' urlparam='']
{
"required":1,
"validate_format":0,
"default":""
}
[/cred_generic_field]

#1141084

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - To validate the field type "textarea" actually there is no other validation other than "required" available with Generic field. So any extra validations you want needs to be added through the Forms API hook: cred_form_validate

More info:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#1141177

Hi, do you have an example of a hook to set minimum characters allowed in a text field?

Thanks

#1141244

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You mean maximum characters allowed - correct? Not minimum.

#1142401

No, we mean minimum characters, it's for our clients that send us news articles and some are sending us a news article with one paragraph, so we want to set a minimum amount of characters.

Is that possible?

Thanks

#1142918

Hello,

It is possible, for example:

add_filter('cred_form_validate','my_validation',999,2);
function my_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;

    //validate if specific form
    if ($form_data['id']==1234)
    {
        //check description value
        if (isset($fields['description']['value']) && mb_strlen ($fields['description']['value']) < 100 )
        {
            //set error message for my_field
            $errors['description']='You must input more than 100 characters';
        }
    }
    //return result
    return array($fields,$errors);
}

Please replace 1234 with your Toolset form ID

More help:
hidden link
Gets the length of a string.

#1144942

That worked perfectly 🙂

My issue is resolved now. Thank you!