Skip Navigation

[Resolved] codice per scelta di tassonomie obbligatoria non va

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

Our next available supporter will start replying to tickets in about 0.46 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 17 replies, has 2 voices.

Last updated by massimoS527 6 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#617349

ho inserito questo codice nel file functions.php del tema bambino.
per rendere la scelta delle Tassonomie obbligatorie nel Modulo CRED.

Funzionava tutto benissimo , ora mi dice che devo fare una scelta della Tassonomia anche se ho fatto
la scelta. Non capisco perchè ha smesso di funzionare !

add_filter('cred_form_validate','func_validate_taxonomy_select',10,2);
function func_validate_taxonomy_select($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==348)
    {
        //check my_field value
        if ($fields['creatura-sesso']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['creatura-sesso']='fai una scelta';
        }
		
		 //check my_field value
        if ($fields['creatura-specie']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['creatura-specie']='fai una scelta';
        }
		
		 //check my_field value
        if ($fields['creatura-vita-assieme']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['creatura-vita-assieme']='fai una scelta';
        }
		
          }
    //return result
    return array($fields,$errors);
}

#617664

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Massimo

We don't have anyone able to provide support in Italian, I'm afraid.

If you are able to continue in English I would be happy to help.

I understand that you have a problem with the validation code included above, but I'm not clear what the problem is.

Are you using this exact code? It appears to require all of the fields you are attempting to validate to be equal to "correct_value". Is that your intention?

Can you explain what the problem is?

#619645

I entered this code in the child theme's file functions

    //validate if specific form
    if ($form_data['id']==348)
    {
        //check my_field value
        if ($fields['creatura-sesso']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['creatura-sesso']='fai una scelta';
        }
		
		 //check my_field value
        if ($fields['creatura-specie']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['creatura-specie']='fai una scelta';
        }
		
		 //check my_field value
        if ($fields['creatura-vita-assieme']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['creatura-vita-assieme']='fai una scelta';
        }
		
          }
    //return result
    return array($fields,$errors);
}

serves to make the choice of mandatory taxonomies in the CRED form

It does not work anymore !
He tells me that I have not chosen any taxonomy even though I actually make the choice

#619664

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Can I get credentials to look at your site? It would help me understand exactly what the problem is.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to break anything.

Can you also confirm which form this is and where I can see it.

#619682

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Massimo

Your validation code for each of the taxonomies says if the value submitted in the form is not "correct_value" then add the error message "fai una scelta".

None of your taxonomy field values will ever equal the string "correct_value". What is "correct_value" supposed to be?

Looking at the markup for your form, the creatura-sesso field, for example, has several options, each of which has a numeric value 4, 5, 6, or 28.

The "--choose--" option which is presumably what you do not want the visitors to select has a value of 0 (zero). So the test for this field should be

if ($fields['creatura-sesso']['value'] == '0')
        {
            //set error message
            $errors['creatura-sesso']='fai una scelta';
        }

And your other fields would need changing similarly.

#619708

for the first choice, I opened a help ticket:
https://toolset.com/forums/topic/drop-down-list-of-taxonomies-first-choice/

here is the code:

jQuery(document).ready(function($){
 
jQuery("select[name='creatura-sesso[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-sesso[]']")[0].selectedIndex = 0;
 
 
jQuery("select[name='creatura-specie[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-specie[]']")[0].selectedIndex = 0;
 
jQuery("select[name='creatura-vita-assieme[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex = 0;
 
});

the code I wrote at the beginning was a help ticket from a user.
  I do not know what to do to solve

#619721

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That's okay, you have added those options with JavaScript.

So you need to modify the PHP validation code like I showed you in my previous reply. You will need to do the same for each of the fields.

#619724

if I understand correctly I have to enter the code for each taxonomy-child

example:

sex (taxonomy-father)
|
|
| -maschio (taxonomy-child)
| -female
| -Other

question: I do not know or slug: I-do-not-know

can you confirm ?

#619725

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The sample code you started with is to make taxonomy selections in the form required fields, so that the form cannot be submitted without choosing an option for each of the taxonomy fields.

Is that what you want to do?

#619727

Yes

#619739

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so I think you just need to modify your original PHP validation code like so:

//validate if specific form
    if ($form_data['id']==348)
    {
        //check my_field value
        if ($fields['creatura-sesso']['value'] == '0')
        {
            //set error message for my_field
            $errors['creatura-sesso']='fai una scelta';
        }
         
         //check my_field value
        if ($fields['creatura-specie']['value'] == '0')
        {
            //set error message for my_field
            $errors['creatura-specie']='fai una scelta';
        }
         
         //check my_field value
        if ($fields['creatura-vita-assieme']['value'] == '0')
        {
            //set error message for my_field
            $errors['creatura-vita-assieme']='fai una scelta';
        }
         
          }
    //return result
    return array($fields,$errors);
}

This assumes that your JavaScript from your other thread adds a "choose" option which has a value of zero.

#619744
syntax error, unexpected '}', expecting end of file
#619746

I validate the form even if I do not choose taxonomies, what's wrong?

add_filter('cred_form_validate','func_validate_taxonomy_select',10,2);
function func_validate_taxonomy_select($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
//validate if specific form
    if ($form_data['id']==348)
    {
        //check my_field value
        if ($fields['creatura-sesso']['value'] == '0')
        {
            //set error message for my_field
            $errors['creatura-sesso']='fai una scelta';
        }
          
         //check my_field value
        if ($fields['creatura-specie']['value'] == '0')
        {
            //set error message for my_field
            $errors['creatura-specie']='fai una scelta';
        }
          
         //check my_field value
        if ($fields['creatura-vita-assieme']['value'] == '0')
        {
            //set error message for my_field
            $errors['creatura-vita-assieme']='fai una scelta';
        }
          
          }
    //return result
    return array($fields,$errors);
}
#619749

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The code from your last update looks fine, I copied it into a file editor and inspected it and I don't see any errors.

From the previous message it sounds like you maybe have an extra '}' at the end of your file.

#619751

Nigel the code is ok, but the CRED module is sent even if I do not choose taxonomies