Skip Navigation

[Resolved] Need to make all fields on form required

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
- 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 5 replies, has 1 voice.

Last updated by katherineS-2 4 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2800294

Tell us what you are trying to do?

I want to make all fields on form required

Is there any documentation that you are following?

Tried looking over this
https://toolset.com/forums/topic/form-validation-make-taxonomy-field-required-bug/

Is there a similar example that we can see?

What is the link to your site?

#2800365

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

What form you are using? Where you added the form? what fields exactly you want to make required?

Normally - as you shared with reference ticket, you can use the Toolset form hook "cred_form_validate" to validate all fields.

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

#2800409

This is my form. hidden link

#2800440

Minesh
Supporter

Languages: English (English )

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

You mean all of the fields added on form you want to set as required field? If yes:

Can you please send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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 have set the next reply to private which means only you and I have access to it.

#2800458

It will take some time to back up site and get access.

This is what I have been trying. I wasn't sure about this line. $form_data['id'] == 111182

add_filter('cred_form_validate', 'my_validation', 10, 2);

function my_validation($error_fields, $form_data) {
list($fields, $errors) = $error_fields;

// Log the form ID to verify that this function is being called.
error_log('Form ID: ' . $form_data['id']);

// Only validate for the specific form.
if ($form_data['id'] == 111182) {
if (empty(trim($fields['wpcf-school-district']['value']))) {
$errors['wpcf-school-district'] = 'This field cannot be empty';
error_log('Validation error: Company field is empty');
}
}

return array($fields, $errors);
}

#2800581

Minesh
Supporter

Languages: English (English )

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

Add the following hook to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/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){
 
$form_html_id = $field['form_html_id'];
$form_id = 0;
   
    if ( isset($form_html_id) ) {
        $parts = explode( '_', $form_html_id);
        $form_id = (int) $parts[2];
    }
   
    if ( $form_id == 111182) {
 
   $req_fields_slugs = array('school-district', 'specialty','job-url','job-posted-date','job-types','company-email');
 
    if(in_array($field['id'], $req_fields_slugs)){
        $field['data']['validate']['required'] = array (
            'active' => 1,
            'message' => 'This field is required'
        );
    }
      
}
    return $field;
}

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

After that you should try to submit the form without providing any values to any form field and check if that help you to resolve your issue.

#2800618

I just ended up throwing some javascript validations.

jQuery(document).ready(function(){
jQuery('#cred_form_111182_1_1').on('submit', function(e) {
// Define validation rules for each field.
var validations = [
{ selector: 'input[name="wpcf-school-district"]', message: 'School district is required and cannot be empty' },
{ selector: 'select[name="wpcf-specialty"]', message: 'Specialty is required and cannot be empty' },
{ selector: 'input[name="wpcf-company-email"]', message: 'Your email is required and cannot be empty' }
];

// Remove any existing error messages.
jQuery('.validation-error').remove();

// Flag to track if any field has an error.
var errorsFound = false;

// Loop through each validation rule.
jQuery.each(validations, function(index, rule) {
if(rule.isRadio) {
// For radio groups, check if none is selected.
if(jQuery(rule.selector).length === 0) {
errorsFound = true;
// Insert error message before the first radio button in the group.
jQuery('input[name="wpcf-job-types"]').first().before('' + rule.message + '');
}
} else {
var field = jQuery(rule.selector);
// Check if the field is empty (trimming whitespace).
if(field.length && field.val().trim() === ''){
errorsFound = true;
// Insert error message above the field.
field.before('' + rule.message + '');
}
}
});

// If any errors were found, prevent form submission.
if(errorsFound){
e.preventDefault();
}
});
});