Skip Navigation

[Resolved] Custom data validation during CRED form submission

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

Problem:
Custom data validation during CRED form submission

Solution:
You can use the Toolset form's hook "cred_filter_field_before_add_to_form" to dynamically set the required field for your form.

You can find proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/custom-data-validation-during-cred-form-submission/#post-2207949

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 3 years, 1 month 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.

Our next available supporter will start replying to tickets in about 0.96 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by Saul Baizman 3 years ago.

Assisted by: Minesh.

Author
Posts
#2207231

Hi there,

I have a CRED post form which has several custom fields on it. Some of the custom field definitions do not require the fields to have values. However, in the CRED form we want to require the fields to have values. Can I write some custom JavaScript to validate the data? If so, how?

I tried to use jQuery to intervene in the submission process, but this test code did not work:

    jQuery('#cred_form_5814_1_1_form_submit_1').on('submit',function (event) {
        event.preventDefault();
        console.log('cred form has been submitted');
        return false ;
    } ) ;

Thank you!

Saul

#2207949

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

You can use the Toolset form's hook "cred_filter_field_before_add_to_form" to dynamically set the required field for your form.

You should try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
    
 $target_fields = array('your-field-slug');

    if(in_array($field['id'],   $target_fields)){
        
      
       $required = array('required' => array(
                            'active' => 1,
                            'value' => true,
                            'message' => "This field is required."
                        	));

      $field['data']['validate'] =  $required;
       
         
    }
    return $field;
}

Where:
- replace your original field slug with the 'your-field-slug'.

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

#2208425

I'll give this code a try. Thanks, Minesh!

#2208551

Hi Minesh,

I applied the code and it worked great, for the most part. I tried to use the code for the Post Body field, and I encountered two issues:

+ Clicking the Submit button is supposed to jump the visitor to the first field that's required and is empty. This doesn't happen with the Post Body field. Clicking Submit doesn't move the user anywhere.

+ After clicking Submit, any empty fields that are required display an error message. Typing in those fields makes the message disappear. When entering some information into the Post Body field, the error message remains visible.

Another thing I discovered is that this code does not work for custom taxonomies. Do you know how I can test whether one taxonomy item has been selected?

Lastly, I needed to wrap the if statement in another if statement to prevent array undefined index PHP notices:

if ( isset ( $field['id'] ) ) {
... // if(in_array($field['id'], $target_fields)){
}

Saul

#2208719

Minesh
Supporter

Languages: English (English )

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

The thing is that post body field will be validated on server side if you use the Toolset Form's validation hook. You can not hook the required field validation for the post body field as it's a different kind of field i.e. WYSIWYG.

If you want to validate the post body field, it needs to be validated on server side using the Toolset form's hook "cred_form_validate" as Javasript validation for post body field is not supported.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#2208823

Thanks for your reply, Minesh. I'll validate the post content field via the cred_form_validate hook per your suggestion.

Another thing I discovered is that the code snippet you provided does not work for custom taxonomies. Do you know how I can test whether one taxonomy item has been selected *before* the form has been submitted? Is there a JavaScript event I can interrupt? The jQuery I attempted to use in the first message in this thread did not work.

Thanks.

Saul

#2210013

Minesh
Supporter

Languages: English (English )

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

For taxonomies as well, you will have to use the Toolset form's hook "cred_form_validate":
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Please check the following related ticket:
=> https://toolset.com/forums/topic/cred-taxonomy-location-field-how-to-make-required/#post-470489

#2210291

My issue is resolved now. Thanks, Minesh!