Skip Navigation

[Resolved] Unable to make CRED Generic Checkboxes Field Required

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

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 3 replies, has 2 voices.

Last updated by Minesh 6 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#630345

I have a single line repeating field with slug voyage-vessel. This field is populated through a CRED Generic Field that has dynamically generated options (code below).

    <div class="form-group">
		<label>Which Vessel/s is this Voyage Available on?<span style="color: red;">*</span></label>
			<div class="checkbox-scroll-area">
          	  <p>Scroll for more...</p>
              [cred_generic_field field='voyage-vessel' type='checkboxes' class='' urlparam='']
				{
				"required":0,
				"validate_format":0,
				"default":[],
				"options":[ [wpv-view name="dynamically-generated-list-of-vessels"] ]
			  }
			  [/cred_generic_field]
    	    </div>
  	</div>

I am trying to make this field required.

I have tried changing the required value in the above code to 1 but this did not work.

I have also tried validating using php as below...

// CRED Forms - Voyage Taxonomies Required 
add_filter('cred_form_validate', 'cred_voyage_taxonomies_required',10,2);
function cred_voyage_taxonomies_required($field_data, $form_data)
 {
    list($fields,$errors)=$field_data;

    $forms = [ 90, 110 ];

    // if a specific form
    if ( in_array( $form_data['id'], $forms ) )
    {
	if ( empty($fields['sailing-regions']['value'][0]) )
	{
            $errors['sailing-regions']='Please specify the sailing region/s this series takes place in.';
        }
	if ( empty($fields['embarkation-country']['value'][0]) )
	{
            $errors['embarkation-country']='Please specify the embarkation country.';
        }
	if ( empty($fields['disembarkation-country']['value'][0]) )
	{
            $errors['disembarkation-country']='Please specify the disembarkation country.';
        }
	if ( empty($fields['voyage-categories']['value'][0]) )
	{
            $errors['voyage-categories']='Please specify at least 1 voyage category.';
        }
	if (empty($fields['wpcf-voyage-vessel']['value'][0]))
        {
            $errors['voyage-vessel'] = 'Please select at least 1 vessel.';
        }
	if ( empty($fields['wpcf-secondary-title']['value']) )
	{
            $errors['wpcf-secondary-title']='Please specify the voyage name.';
        }
    }
  
    return array($fields,$errors);

}

Everything except voyage-vessel validation works using the above code.

I have also tried using the following variations to validate voyage-vessel but they do not work either...

	if ( $fields['wpcf-voyage-vessel']['value'] == '' )
        {
            $errors['voyage-vessel'] = 'Please select at least 1 vessel.';
        }

	if (empty($fields['wpcf-voyage-vessel']['value'][0]))
        {
            $errors['voyage-vessel'] = 'Please select at least 1 vessel.';
        }

	if (empty($fields['wpcf-voyage-vessel']['value']))
        {
            $errors['voyage-vessel'] = 'Please select at least 1 vessel.';
        }

Please can you advise on how to make this field required?

#630467

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - to validate the checkboxes field you should use the following code:

In your CRED form - you should make generic checkboxes field required:1.

For example:

[cred_generic_field field='voyage-vessel' type='checkboxes' class='' urlparam='']
            {
            "required":1,
            "validate_format":0,
            "default":[],
            "options":[ [wpv-view name="dynamically-generated-list-of-vessels"] ]
          }
          [/cred_generic_field]

And in your "cred_form_validate" hook - you should try to use following condition:

For example:

  if ($fields['voyage-vessel']['value'] == '')
        {
            $errors['voyage-vessel'] = 'Please select at least 1 vessel.';
        }
#630648

Hi Minesh,

Unfortunately the following code does not work...

<div class="form-group">
		<label>Which Vessel/s is this Voyage Available on?<span style="color: red;">*</span></label>
			<div class="checkbox-scroll-area">
          	  <p>Scroll for more...</p>
              [cred_generic_field field='voyage-vessel' type='checkboxes' class='' urlparam='']
				{
				"required":1,
				"validate_format":0,
				"default":[],
				"options":[ [wpv-view name="dynamically-generated-list-of-vessels"] ]
			  }
			  [/cred_generic_field]
    	    </div>
  	</div>
	if ($fields['voyage-vessel']['value'] == '')
        {
          $errors['voyage-vessel'] = 'Please select at least 1 vessel.';
        }

I have also tried with the wpcf- prefix and with [0] after ['value'] but these do not make it work either.

#630800

Minesh
Supporter

Languages: English (English )

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

could you please share problem URL where you added the CRED form and access details.

*** 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.