Skip Navigation

[Closed] Required fields not working in CRED

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.

Tagged: 

This topic contains 9 replies, has 3 voices.

Last updated by Minesh 5 years, 9 months ago.

Assigned support staff: Minesh.

Author
Posts
#304515

I'm trying to make fields in one of my CRED forms required, but it's only working on the Title.

I've tried using required="true" in the field, for example: [cred_field field="type-of-technology" post="project" value="" urlparam="" required="true"] but it's not working. There's a different solution offered here: https://toolset.com/forums/topic/cred-how-to-make-fields-required-2/ but that doesn't work either.

There's no option in the 'add field' generator to do this either.

Please can someone help me with this, as making fields required in a form is pretty essential.

Thanks

#304583

pierre.s
Supporter
required-field.png

Hello,

Thank you for your message!

For a field you created with WP Types, you have to tick the "required" option in the field setup (see attached screenshot).

For generic fields, you will find an option for it in the modal box.

Does this help?

Regards,
Pierre

#304603

Hi,

Thanks for the quick reply. I didn't realise these needed to be set via the Types section - that works great for most of the fields, however I'm not getting the 'required' option for a checkbox field sets I've used. Any idea how I can include these as being required?

Thanks

#304622

pierre.s
Supporter

I think you can't set a checkbox to be required because it's a binary option. The user can either select or not select this option.

Does this make sense to you?

Regards,
Pierre

#304627

Not really, we still want them to make a choice, and the form uses checkboxes so they can choose more than one option. We use this method with our Gravity Forms and they allow this to be a required field.

Thanks.

#304667

pierre.s
Supporter

I am afraid this feature doesn't exist in Types. As a workaround, I would suggest to add a custom validation using the "cred_form_validate" filer hook:

add_filter('cred_form_validate','checkboxes_validation', 2, 10);
function checkboxes_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'] != 12 )
	// 		return;

 	// set error message per field
	if ( $fields['wpcf-validate-checkboxes']['value'] == '' )
		$errors['validate-checkboxes'] = 'Please enter at least one checkbox';
 
	return array( $fields, $errors );
}

You can limit this validation rule to a specific form. Change the "validate-checkboxes" field id to the one you are using for your checkboxes.

Let me know if this matches your requirements.

Regards,
Pierre

#307718

Hi Pierre,

Thank you for the reply - this works for one field, but when I repeat the code for a second field I get the following error on the website:

Fatal error: Cannot redeclare checkboxes_validation() (previously declared in....

Could you show me how I can include two fields in the custom function?

Thank you.

#308846

Minesh
Supporter

Languages: English (English )

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

Hello Mike, this is Minesh here to help you further. I will take care of your ticket now.

As I understand you want to validate multiple fields at a same time when you submit your CRED form. Could you please confirm.

If no:
Could you please explain your issue with example or few screenshots will be helpful.

#312071

Hi,

Yes that's the issue, I just need to know how to modify the code provided above to include two fields - it works with one, but I don't know how to add a second.

Thanks

#312200

Minesh
Supporter

Languages: English (English )

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

could you please provide me some information which is the second field you want to validate as well as a link to your CRED form:

*** Please take FULL BACKUP of your database and your website.***
In order to investigate your issue if we require to check things on your install in order to see what could be going wrong there.

If you agree to this please use the form fields I have enabled below this comment box to provide temporary access details (wp-admin and FTP)?

I have set next reply as private.

The topic ‘[Closed] Required fields not working in CRED’ is closed to new replies.