Skip Navigation

[Resolved] CRED Fom validation of taxonomy fields

This support ticket is created 8 years, 3 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
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 22 replies, has 2 voices.

Last updated by Sanny 8 years, 2 months ago.

Assisted by: Waqas.

Author
Posts
#326791

I'm appending a placeholder option to all select fields of a new post CRED form, through this js in the footer:

<script>
var option = new Option('- Select -', '');
jQuery('.cred-form select').prepend(jQuery(option)).val('');
</script>

which adds <option value="">- Select -</option> as first option for all selects.

I need to make some of these selects, required.
They are all taxonomies (created through Types).

I tried:

add_filter('cred_form_validate','tax_validation',10,2);
function tax_validation($field_data, $form_data) {
    list($fields,$errors) = $field_data;
    if (empty($fields['taxonomy-slug']['value']))  {
	$errors['taxonomy-slug'] = 'This field is required';
    }
    return array($fields,$errors);
}

But it's not working.

I'm using the latest CRED beta.

#326891

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Although your code is fine and should work. Can you please try with latest stable release, rather than with the latest beta?

Also, can you please provide some debug information about your site? Please see https://toolset.com/faq/provide-debug-information-faster-support/ for more information.

I have enabled debug information area for your next reply.

#326923

I need to use CRED beta because I need User Forms.

I dont get any js error in console, simply nothing happens to the select field.

#327247

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes you are right, user forms are part of beta so far.

Can I ask for a temporary access to your site? So I can look for more details.

I have enabled your next reply as private, please input all details in that area. Please mention the links to the pages, views, forms, CPTs and configurations in question.

Please take a backup of your site, before proceeding.

#329984

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing the details. Please allow me some time to work on this. I will update you as soon as I find a solution.

#330210

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I was able to reproduce the same on my local host with CRED beta 1.4-b4 (doesn't matter however). There's a little problem with the logic, while code is perfectly fine, however needs a little change as well. Let me explain:

A) Since you are adding an empty "SELECT" option to the drop down. So whenever a form is submitted, it sends that option as part of the field's selected option. Which means the value ($fields['category']['value']) is never empty.

B) Keeping 'A' in mind, $fields['category'] actually produces following array:

[category] => Array
        (
            [value] => Array
                (
                    [0] => 
                )

            [name] => category
            [type] => taxonomyhierarchical
            [repetitive] => 
        )

So to checking empty($fields['category']['value']) will never be true, because you can see the [value] array has an element on index 0 with empty value. This is due to that JS added option.

C) However, to over come this issue, I used $fields['category']['value'][0] to check for emptiness. Provided that, the select field is a single select field. And used your code, but of course on a 'category' field and with that little amendment:

add_filter('cred_form_validate','tax_validation',10,2);
function tax_validation($field_data, $form_data) {
    list($fields,$errors) = $field_data;

	//print_r($fields);
	//print_r($errors);
	//die();
    if (empty($fields['category']['value'][0]))  {
    	$errors['category'] = 'This field is required';
    }
    return array($fields,$errors);
}

And it works perfectly fine. Your code is fine, you just need to add [0] to address that element of value array. I have left print_r() statements in above code (but commented), I used those statements to expose what's actually being filled in the $fields array.

I hope this explains the situation well and you can fix the issue very easily.

#330288

Thank you!
I had the feeling it had to do with that. However I still cant make it work.

It's not even printing the field array (with print_r), so Im a little confused.
I'm trying to use this on the /add-new-rate-plan/ page.

#330325

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I can look into your website for the issue, since you have already provided the details. But can you please mention the exact URLs to the pages, views, content templates or etc related to this (in front-end and admin area)? This will help me finding and fixing the issue fast.

I have set your next reply as private for this purpose, you can ignore other access details.

Please remember to take backup of your site, before proceeding, thanks.

#330560

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing the details. Please allow me some time to work on this and I will update you as soon as I fix this.

#330669

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I am trying to access your website but looks like it is down. I am seeing a "Error establishing a database connection" error on front-end and while trying the admin area as well.

Can you please take a look if everything is alright?

#330697

Sorry, hosting is messing up.
Please try again now.
If that happens again, just keep trying, it's just temporary.

#330893

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please see it working now at page /add-new-rate-plan/. It was all perfect, but there was a little confusion around it. SELECT and Checkboxes are populated differently when a form is submitted.

As you know, that for a SELECT you had to test for 0th element of 'value' array - when there was nothing selected. For Checkboxes, if there's no checkbox checked, then 'value' is empty altogether (no element is set).

Following is a dump of 2 different fields:

// SELECT with no value selected
[pg-rate-type] => Array
        (
            [value] => Array
                (
                    [0] => 
                )

            [name] => pg-rate-type
            [type] => taxonomyhierarchical
            [repetitive] => 
        )

// Checkboxes with nothing checked
    [pg-brand] => Array
        (
            [value] => 
            [name] => pg-brand
            [type] => taxonomyhierarchical
            [repetitive] => 
        )

// Checkboxes with 1 option checked
    [pg-hotel-type] => Array
        (
            [value] => Array
                (
                    [0] => 431
                )

            [name] => pg-hotel-type
            [type] => taxonomyhierarchical
            [repetitive] => 
        )

I have updated the code in your custom.php file as below:

add_filter('cred_form_validate','tax_validation',10,2);
function tax_validation($field_data, $form_data) {
    list($fields,$errors) = $field_data;
    //print_r($fields);
    
    //if ($form_data['id'] == 7583)     {
    	// SELECT Field Taxonomies
	    if (empty($fields['pg-rate-type']['value'][0]))  {
        	$errors['pg-rate-type'] = 'This field is required';
    	}
	    
	    if (empty($fields['pg-rate-audit']['value'][0]))  {
        	$errors['pg-rate-audit'] = 'This field is required';
    	}
    	
    	if (empty($fields['pg-mandatory-optional']['value'][0]))  {
        	$errors['pg-mandatory-optional'] = 'This field is required';
    	}
    	
    	// Checkbox Taxonomies - Notice there's no array associated with 'value' when no checkbox is selected.
    	// This is different than a SELECT
    	if (empty($fields['pg-brand']['value']))  {
        	$errors['pg-brand'] = 'This field is required';
    	}
    	
    	if (empty($fields['pg-hotel-type']['value']))  {
        	$errors['pg-hotel-type'] = 'This field is required';
    	}
    	
    	if (empty($fields['pg-spg-level']['value']))  {
        	$errors['pg-spg-level'] = 'This field is required';
    	}
 	//}
    //print_r($errors);
    //die();
    
    return array($fields,$errors);
}

Please notice the checks placed differently for both types of fields. You can add more in this fashion. Also notice a die() statement at almost end of the code. That's required when you want to print_r() for a form submission callback handler, because CRED uses a redirection (as WP does), that's why you were not seeing any output by print_r().

FYI: Database Backed Up Successfully On '04/09/2015 @ 11:30'.

#333711

Sorry for the delay, we had some critical server issues and had to revert to an older version of the theme.
I have replaced my code with yours, but I see no difference (still not validating).
Maybe you changed something else too? Could you please have a look?
Thank you!

#333907

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Alright, please allow me some time to look into this again. I will try my best to correct things as expected.

I will update you soon on this.

#334239

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Add New Rate Plan.png

Please see the attached screen shot, it seems working now. You can see validation errors on top of the form (red color area).

Please confirm if this is what you wanted to work or am I missing something?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.