0

After I render/generate a part of my form (radios) with an ajax callback (from a another radios), I am unable to define the #default_value attribute of that generated radio.

Also when I validate the form and there are errors, the form doesn't keep the previously selected radios(from the ajax generated).

I guess both are related, this ajax seems to mess with it, it's working fine without it.

I've read there are bugs with the ajax callback + form but what is the workaround? I've seen couple of "solution" but they were not working in my case.

EDIT: I've been using this solution in the meantime, which works fine.

//I use this in my ajax callback making the generated radio to set the default value to 0, or change the array(0) by array($yourDefaultValue)
$commands[] = ajax_command_invoke('#id', 'myfunction', /* array(0) */);


//used to keep the values in the validation form (in the end if it)
drupal_add_js("jQuery(document).ready(function () {  jQuery.fn.myfunction( $myVariable); })","inline");


//custom js file
$.fn.vtoolsRefresh = function(myVariable) {
   //get the selected values from the validation form
   if(typeof myVariable !== "undefined" ){
     $('#radio-id input[value='+myVariable+']').click();
   }
   else{
     //default value set to 0
      $('#radio-id input[value=0]').click();
   }
}
3
  • Have you tried $form_state['rebuild'] =true?
    – Viraj
    Commented Feb 17, 2016 at 12:17
  • Yes, but still not working :(
    – GwenM
    Commented Feb 17, 2016 at 13:10
  • I found a workaround, see the edit.
    – GwenM
    Commented Feb 17, 2016 at 15:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.