0

I'm going to be linking this D.O issue.

I found that when changing an attribute other radio buttons would not update properly. Specifically that when there was two possible options for these buttons but each were only available if another option in the form was set, changing the first attribute resulted in the second one not being rendered as checked. If you went to add that item to the cart the proper variation would be added so this seems to be just visual.

I was able to use xdebug to find that the default value of the buttons is set correctly.
I've been digging through the rendering trace of this element but am getting quite lost.

Does anyone know why after an ajax call a radio button would be rendered incorrectly and not be marked as checked?

  • could you please post your AJAX callback code? Also, is the default value for the Style input already set when the form is initially loaded? Or is this set entirely based on the Color selection? – Beau Mar 25 at 22:17
0

A common mistake with AJAX is presuming that an AJAX callback is "aware" of the form's state. In reality, the form definition that AJAX works with is the original form instance. You will have to set the form values accordingly in your AJAX callback prior to returning the re-rendered markup. The $form argument that you receive in your callback function is always a cached copy of the original form.

So, to track what is going on in your form across AJAX callbacks, need to stash data server-side (e.g. in \Drupal::state()) or use the values stored in the $form_state argument. A good explanation of Drupal's form handling can be found here (thanks Jaypan!): https://www.drupal.org/forum/support/module-development-and-code-questions/2012-02-24/update-form-state-in-ajax-callback#comment-5648474

  • I do think that makes sense, but is that my problem? ~~I am only changing things within the commerce entity~~ Wrong issue, I don't get any errors about the state being wrong – Derek C. Mar 23 at 17:35
  • Just to make sure I understand: you have radio elements that should be rendered with updated values? And the value isn't being applied to the returned markup? – Beau Mar 25 at 14:36
  • Yes. I suggest you take a look at the screenshots on the D.O issue linked, it shows the problem. – Derek C. Mar 25 at 16:20
  • Thanks. I took a look, and I think it still might be the situation I've described. I've asked for some clarification. – Beau Mar 25 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.