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.
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);
});
when I save the file it tells me error in this command:
jQuery(document).ready(function($){
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
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!
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);
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>
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!