Skip Navigation

[Resolved] CRED edit post form taxonomy field default option

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two CRED forms, one to create posts and another to edit posts. In each form I have 3 taxonomy fields for custom taxonomies. In the past, I added some custom code that inserts a default option in each field that says "-choose-". I also added custom code that returns an error using cred_form_validate if the default option is selected. It doesn't always work as expected. Sometimes the wrong selected option appears on the front-end.

Solution:
CRED API form validation code:

/*  TASSONOMIE OBBLIGATORIE IN AGGIUNGI CREATURE  */
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;
  
    //validate if specific form
    if ($form_data['id']==348 or  $form_data['id']==349)
    {
         
        //check my_field value
        if ($fields['creatura-sesso']['value'][0] == '0')
        {
            //set error message for my_field
            $errors['creatura-sesso']='fai una scelta';
        }
            
         //check my_field value
        if ($fields['creatura-specie']['value'][0] == '0')
        {
            //set error message for my_field
            $errors['creatura-specie']='fai una scelta';
        }
            
         //check my_field value
        if ($fields['creatura-vita-assieme']['value'][0] == '0')
        {
            //set error message for my_field
            $errors['creatura-vita-assieme']='fai una scelta';
        }
            
          }
    //return result
    return array($fields,$errors);
}

CRED New Post form JS:

var $selected = jQuery("select[name='creatura-sesso[]']").find("option[selected='selected']");
var si = $selected.length ? $selected.index() : -1;
jQuery("select[name='creatura-sesso[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-sesso[]']")[0].selectedIndex = (si==-1 ? 0 : si + 1);
 
$selected = jQuery("select[name='creatura-specie[]']").find("option[selected='selected']");
si = $selected.length ? $selected.index() : -1;
jQuery("select[name='creatura-specie[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-specie[]']")[0].selectedIndex = (si==-1 ? 0 : si + 1);
 
$selected = jQuery("select[name='creatura-vita-assieme[]']").find("option[selected='selected']");
si = $selected.length ? $selected.index() : -1;
jQuery("select[name='creatura-vita-assieme[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex = (si==-1 ? 0 : si + 1);

CRED Edit Post form JS:

// tassonomia prima selezione con patch avviso: diversa da aggiungi creatura
 
jQuery(document).ready(function($){
   
var si = jQuery("select[name='creatura-sesso[]']")[0].selectedIndex;
jQuery("select[name='creatura-sesso[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-sesso[]']")[0].selectedIndex = (si==-1 ? 0 : si+1);
   
si = jQuery("select[name='creatura-specie[]']")[0].selectedIndex;
jQuery("select[name='creatura-specie[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-specie[]']")[0].selectedIndex = (si==-1 ? 0 : si+1);
   
si = jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex;
jQuery("select[name='creatura-vita-assieme[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex = (si==-1 ? 0 : si+1);
});

Relevant Documentation:
http://api.jquery.com/find/
http://api.jquery.com/index/
http://api.jquery.com/prepend/
https://www.w3schools.com/jsref/prop_select_selectedindex.asp

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by massimoS527 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#620195

unfortunately it is unnerving what happens to me: I just solved a problem:https://toolset.com/forums/topic/codice-per-scelta-di-tassonomie-obbligatoria-non-va/

now I have a new one, you can help me make this module OK.
it's hard for me to translate every time into English !!!!!

PROBLEM: front-end: when I change a post I sent, I always see the taxonomies with the word "choose".

But if I go to see the card that generates the post, I note with amazement that my selection is seen.

#620266

Check now, I have modified the JS code on the edit form to respect the original selectedIndex:

jQuery(document).ready(function($){
 
var si = jQuery("select[name='creatura-sesso[]']")[0].selectedIndex;
jQuery("select[name='creatura-sesso[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-sesso[]']")[0].selectedIndex = (si==-1 ? 0 : si+1);
 
si = jQuery("select[name='creatura-specie[]']")[0].selectedIndex;
jQuery("select[name='creatura-specie[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-specie[]']")[0].selectedIndex = (si==-1 ? 0 : si+1);
 
si = jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex;
jQuery("select[name='creatura-vita-assieme[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex = (si==-1 ? 0 : si+1);
});
#620427
error.jpg

when I save the file it tells me error in this command:

jQuery(document).ready(function($){
#620661

You added JavaScript code directly to the functions.php file? That won't work. Use the JS panel on the CRED form editor screen instead, or enqueue this code using standard WordPress script enqueueing techniques.
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

#620867

Hi Christian,
your code works, but there was a problem always related to the choice of taxonomies.
It's a month that happens and it always makes me lose a lot of time.
I see you explain what happens:

I have this code in the child theme functions file and it is used to make the mandatory taxonomies in the modules "create content" and "edit content"

file code functions

/*  TASSONOMIE OBBLIGATORIE IN AGGIUNGI CREATURE  */
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;
 
    //validate if specific form
    if ($form_data['id']==348 or  $form_data['id']==349)
	{
		
        //check my_field value
        if ($fields['creatura-sesso']['value'][0] == '0')
        {
            //set error message for my_field
            $errors['creatura-sesso']='fai una scelta';
        }
           
         //check my_field value
        if ($fields['creatura-specie']['value'][0] == '0')
        {
            //set error message for my_field
            $errors['creatura-specie']='fai una scelta';
        }
           
         //check my_field value
        if ($fields['creatura-vita-assieme']['value'][0] == '0')
        {
            //set error message for my_field
            $errors['creatura-vita-assieme']='fai una scelta';
        }
           
          }
    //return result
    return array($fields,$errors);
}
      

now there is some code in the JS editor of the CRED modules to see in the choice of taxonomies as the first entry "-choose-". everything worked fine, but now it does not work any better. The word "-choose-" does not appear.

form code add content

// tassonomia prima selezione

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

form code edit content

 
// tassonomia prima selezione con patch avviso: diversa da aggiungi creatura

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

Unfortunately I resolve on one side and not on the other.
I hope I explained myself well, I use Google Translate.

My question is that I want a definitive thing and that it always works, can you help me?
I would be happy for this!

#621069

Check now, I made an adjustment to the JS code in the Add Creatures form. Now "--choose--" is selected when the page first loads. If the User submits the form and an error is returned, the previously selected values will show up correctly and "--choose--" will be added to the form.

var $selected = jQuery("select[name='creatura-sesso[]']").find("option[selected='selected']");
var si = $selected.length ? $selected.index() : -1;
jQuery("select[name='creatura-sesso[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-sesso[]']")[0].selectedIndex = (si==-1 ? 0 : si + 1);

$selected = jQuery("select[name='creatura-specie[]']").find("option[selected='selected']");
si = $selected.length ? $selected.index() : -1;
jQuery("select[name='creatura-specie[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-specie[]']")[0].selectedIndex = (si==-1 ? 0 : si + 1);

$selected = jQuery("select[name='creatura-vita-assieme[]']").find("option[selected='selected']");
si = $selected.length ? $selected.index() : -1;
jQuery("select[name='creatura-vita-assieme[]']").prepend('<option value="0">-choose-</option>');
jQuery("select[name='creatura-vita-assieme[]']")[0].selectedIndex = (si==-1 ? 0 : si + 1);
#621295

Hi Christian,
I did several tests before writing to you, let's talk about the "add content" module.

the choice of Taxonomies is ok, but something strange happens.
if I do not choose the Taxonomy (I tried to generate an error to see the operation) it happens that the image of the post increases in size, goes from miniature (150x150px) to medium size (300x300px).

I tried to select taxonomies and generate a different error by not inserting the post title and the image does NOT change in size.

It works out in my opinion, establishing the size of the image in the shortcode of the Cred module.

here is the shortcode part of the form:

<div class="form-group">
		<label>[wpml-string context='cred-form-aggiungi creatura originale-348' name='foto']foto[/wpml-string]</label>
		[cred_field field='_featured_image' value='' urlparam='' output='bootstrap']
	</div>

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/split-cred-featured-image-resizes-after-backend-validation-with-cred_form_validate/

#621822

Hi, I split that issue into another ticket because it seems to be unrelated to the custom taxonomy validation we added, and related to backend validation in general. For example, the same behavior can be reproduced with this code:

So the problem is the size of the featured image shown in the CRED form after backend-validation scripts have run? If that's the issue, please open a separate ticket. This appears to be unrelated to the taxonomy validation issue here, because you can replicate the same behavior with any backend validation. For example, replace your code with this other validation and the same image resize behavior can be seen:
[php]
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;
  
    //validate if specific form
    if ($form_data['id']==348 or  $form_data['id']==349)
    {
         
        $errors['_featured_image'] = 'Some test error';
            
          }
    //return result
    return array($fields,$errors);
}

I'm not sure if this is a bug or the intended behavior, so I split that into another ticket where we can do more investigation. If the taxonomy fields' selections are saved correctly, and shown correctly during editing posts, let's close out this ticket. Thanks!

#623314

ok, thanks