Skip Navigation

[Resolved] How to make a taxonomy and image upload field required?

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

Problem: I would like to require Users to upload an image and select at least one taxonomy term in my Form.

Solution: You can make a custom field image required by editing the custom field in Toolset > Custom fields > Post fields. You can use a custom code snippet with the Forms API to validate at least one term checkbox is checked:

<?php
/**
 * New custom code snippet.
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
 
function term_checkboxes_form_validation( $field_data, $form_data ) {
 
    $tax = 'app-name';
 
    $forms = array( 15 );
 
    $msg = 'At least one checkbox is required.';
 
 
 
    // you should not edit below this line
 
 
 
    // field data are field values and errors
 
    list($fields,$errors)=$field_data;
 
    // validate if correct CRED form ID
 
    if ( in_array( $form_data['id'], $forms ) ) {
 
      if ( !isset($fields[$tax]['value'][0]))
 
        $errors[$tax] = $msg;
 
    }
 
 
 
    return array( $fields, $errors );
 
}

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

This support ticket is created 5 years, 7 months 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Mark Lee 5 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1224064

Hello, I have a taxonomy and image upload field on my form. How can I make these fields required to fill up before the user can submit the form? I referred to https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate doc but I have no coding knowledge. Could you kindly provide me with the exact steps to achieve this please?

#1224077

Okay here's a snippet that will force the User to select at least one taxonomy term checkbox in the Form:

add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);
function term_checkboxes_form_validation( $field_data, $form_data ) {
    $tax = 'YOUR-TAXONOMY-SLUG';
    $forms = array( 12345 );
    $msg = 'At least one checkbox is required.';

    // you should not edit below this line

    // field data are field values and errors
    list($fields,$errors)=$field_data;
    // validate if correct CRED form ID
    if ( in_array( $form_data['id'], $forms ) ) {
      if ( !isset($fields[$tax]['value'][0]))
        $errors[$tax] = $msg;
    }

    return array( $fields, $errors );
}

Change YOUR-TAXONOMY-SLUG to match your taxonomy slug, change 12345 to match the numeric ID of this Form, and change At least one checkbox is required. if you want to customize the error message.

Then go to Toolset > Settings > Custom Code tab and create a new snippet. Paste the code at the bottom of the snippet, save it, and activate the snippet to run everywhere.

#1224083

ok replaced the slug and id

but i got this error syntax error, unexpected '&' in /home/bear1304/public_html/tall.design/wp-content/toolset-customizations/required.php on line 7

A problem occurred when executing snippet "required". The result of include_once is: ""

<?php
/**
 * New custom code snippet.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_filter('cred_form_validate','term_checkboxes_form_validation', 10, 2);

function term_checkboxes_form_validation( $field_data, $form_data ) {

    $tax = 'app-name';

    $forms = array( 15 );

    $msg = 'At least one checkbox is required.';



    // you should not edit below this line



    // field data are field values and errors

    list($fields,$errors)=$field_data;

    // validate if correct CRED form ID

    if ( in_array( $form_data['id'], $forms ) ) {

      if ( !isset($fields[$tax]['value'][0]))

        $errors[$tax] = $msg;

    }



    return array( $fields, $errors );

}
#1224088

Ah ignore me, I copied the code from the email notification and it had all the unicode.

Thank you for the swift assistance 🙂

#1224094

Okay great, let me know if your issue is not resolved.

#1225292

My issue is resolved now. Thank you!