[Resolved] Do not allow to save a post on the frontend if field has less than 20 characters

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.

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

Problem: How to validate the CRED form with number of characters input

Solution: To validate the CRED form you can use CRED hook cred_form_validate

You can find the proposed solution with the reply here

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

Related documentation: Front-end Content Submission and Editing Forms

This topic contains 11 replies, has 2 voices, and was last updated by  Nicholas 9 months, 1 week ago. The support staff assigned to this topic is Minesh.

Viewing 12 posts - 1 through 12 (of 12 total)
Author
Posts
#546003

Hello this ticket is a continuation of this ticket:
https://toolset.com/forums/topic/min-and-max-characterswords/
I was wondering

Is there a way to set a min character count?
So for example if the post_excerpt field has less than 30 characters the post can't be saved.

Regards,
Nicholas

#546012

Minesh
Forum moderator

Supporter languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you can use CRED API hook: cred_form_validate to validate your fields.

Please check kind of the same ticket that might help you - you should modify code as per your requirement:
=> https://toolset.com/forums/topic/how-to-do-simple-validation/#post-379193

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

#548475

Is there a way to show how many characters the user has already typed instead of how many there are left?
And then make use of the API hook
=> https://toolset.com/forums/topic/how-to-do-simple-validation/#post-379193

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Regards,
Nicholas

#548760

Minesh
Forum moderator

Supporter languages: English (English )

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

Ok - I've made change to your form and now it will show: Characters added.
=> hidden link

#549382

Hello Minesh. Sorry for the late reply.

I tried out this code

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
     
    //validate if specific form
    if ($form_data['id']==283)
    {
            //check my_field value
        if (strlen(trim($fields['wpcf-id-number']['value'])) != 13 )
        {
            //set error message for my_field
            $errors['wpcf-id-number']='Please enter exact 13 digits';
        }
  
    }
      
    //return result
    return array($fields,$errors);
}

Unfortunately it's not working for me. In my case I have characters instead of digits and I need to implement this code on multiple fields.

wpcf-artist-1 -->min. 250 characters otherwise the form can't be submitted
wpcf-artist-2 ->min. 250 characters
_post_excerpt ->min. 250 characters

#549470

Minesh
Forum moderator

Supporter languages: English (English )

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

Could you please share problem URL.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#549955

Minesh
Forum moderator

Supporter languages: English (English )

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

Do you mean:

wpcf-artist-1 -->user should input min. 250 characters otherwise the form can't be submitted
wpcf-artist-2 ->user should input min. 250 characters otherwise the form can't be submitted
_post_excerpt ->user should input min. 250 characters otherwise the form can't be submitted

#549983

My bad
wpcf-artist-1 min. 50 characters
wpcf-artist-2 min. 50

_post_excerpt min 250 characters

#550359

Minesh
Forum moderator

Supporter languages: English (English )

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

I've modified the code in your functions.php file as given under:

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$field_data;
     
    //validate if specific form
    if ($form_data['id']==118)
    {   
       
        //check my_field value
        if (strlen(trim($fields['wpcf-artist-1']['value'])) > 50 ){
            //set error message for my_field
            $errors['wpcf-artist-1']='Please enter exact 50 characters';
        }else if (strlen(trim($fields['wpcf-artist-2']['value'])) > 50 ){
            //set error message for my_field
            $errors['wpcf-artist-2']='Please enter exact 50 characters';
        }else if (strlen(trim($_POST['post_content'])) > 250 ){
            //set error message for my_field
            $errors['post_content']='Please enter exact 250 characters';
        }
  
    }
      
    //return result
    return array($fields,$errors);
}

Could you please check now and confirm your issue is resolved.

#550470

Hello Minesh.
Very nice.

I am using the following code to allow users to set the post status of their post on the frontend.
If the user wants to save the post as a draft the function you shared above should not be triggered. Only if the user want's to save his post as published.
How would I integrate this functionality with your code snippet?

[cred_generic_field field='post_status' type='select' class='choose_post_status' urlparam='']
                                        {
                                        "required":0,
                                        "validate_format":0,
                                        "default":["[wpv-post-status]"],
                                        "options":[
                                        {"value":"draft","label":"Save As Draft"},
                                        {"value":"pending","label":"Publish For Review"}
                                        ]
                                        }
                                        [/cred_generic_field]
* Set post status via CRED generic field
	 */

	function customize_cred_form_118( $post_id, $form_data ){

	    if ( isset( $_POST['post_status'] ) ) {

	        $updates = array(
	            'ID'            =>   $post_id,
	            'post_status'   =>   $_POST['post_status']
	            );
	        wp_update_post( $updates );
	    }
	}
	add_action( 'cred_save_data_118', 'customize_cred_form_118', 10, 2 );

Regards,
Nicholas

#550613

Minesh
Forum moderator

Supporter languages: English (English )

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

As your original issue is resolved and you asked help for the totally different issue.

May I kindly ask you to open a new ticket for your each new question.

This will help other users searching on the forum. Thank you for understanding.

#550615

Yes I understand. Thank you for your help Minesh.

Viewing 12 posts - 1 through 12 (of 12 total)