I have a problem with setting the default value of my 'radios' button, dynamically. In my code, I have an addresses radios group stated like this:
$form['addresses'] = array(
'#type' => 'radios',
'#title' => '',
'#options' => $addresses,
isset($user->data['NewAddress']) ? $user->data['NewAddress'] : 0,
);
This works, however, in my form, I have an ajax callback that lets the user add a new address and this address also gets added to the form once they save the address, via ajax. I want to now set the default value to this new address. The default_value, if I do a dd() of it, shows that it is set to the new address, however, it just doesn't get selected.
I have read somewhere, that the default_value only works the first time that a form is loaded. My situation is, the form loads once, then all the changes are being made via Ajax. I do a $form_state['rebuild'] = true
; after each new address is saved, however. So, basically, my question is, is there any way to get around this issue. How can we set the default_value each time a new address is entered via Ajax?
Any help would be greatly appreciated. Thanks.
Comments
<?php
Thanks for the example, but
Thanks for the example, but unfortunately, it is still not making any effect. A
dd($default_value)
anddd($form['addresses']['#default_value']
shows that the default value is the new address value, but it just isn't getting selected. Wonder, why there is a problem with the select button moving from the old address to the newly-entered address. Seems simple enough...Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba
I don't know what to say.
I don't know what to say. I've used the above many times, both in #ahah in D6, and #ajax in D7, and it works fine. I'm guessing it's something else in your code, but cannot tell from the bit you've given.
After further debugging of
After further debugging of the radios button, I have found out that, once a new address is entered via Ajax (a div with the old address radios is replaced with a div with the new address included), the #default_value is set to the new address, however, the #value remains the same, pointed to the initial default address during form load. This is how the addresses radios array looks for a particular address:
As you notice, the #default_value is set to the new address, but the #value is the same. Do you think this has anything to do with the default value not getting checked? I tried setting the #value to the new address, as well, but that didn't work. Also tried using a #value_callback function to set it to the new address, but still no luck. Any ideas, as to why it would keep the #value the same despite rebuilding the form after every new address has been inserted?
UPDATE!! I wanted to test if the #value was the one that was causing the new address to be not selected, so what I did (as much as I don't like doing this) is, I manually, changed the #value for each option in the addresses radio box to the default value to see if that made any difference. Well, presto, it worked! The new address is being selected as the default address now. This is the code that I did:
So now the question is, why would the #value remain the same? Even though, after each rebuild, we are populating the radio button from scratch. Also, note, I tried to unset the radio button after each new address was submitted, but that didn't work either.
Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba
#value cannot be overridden,
#value cannot be overridden, no matter what the user enters. If the user submits 3, and #value is set to 2, then the submitted value will be 2 regardless. If you set #default_value and #value, #default_value has no meaning since #value is the value.
Ajax Submit Button
I think that the #value is setting every time that a new address is saved. Basically, my Ajax button for saving a new address is of type "submit", I am thinking this submits the form. My button definition is like this:
Is it because it is type "submit" that the #value, which is the initial #default_value, is being set each time I save a new address. I guess, we can change the button to #type "button", but if I understand it correctly, a button, which is of #type "button" will not call the #submit handler, and thus, my "NewAddress_submit" function won't get called. If so, then what would be the way around this?
How can we basically, NOT get the radios button value to submit each time we save an address?
Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba
Can you show your form
Can you show your form definition?
I have copied and pasted all
I have copied and pasted all the relevant form definitions for this radios problem:
Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba
With this line of
With this line of code:
If the $user object has the $user->data['NewAddress'] field set on it, the radios will always be set to that value no matter what. Try looking at the code in my first post again. You want to set priority as follows:
Submitted value -> pre-existing value -> default value.
So if a value has been submitted, that should be used, as the user may have overridden the default. Else if no value has been submitted, then all we have is the pre-existing value, so we should use that. And if no pre-existing value exists, then the default value should be used.
I actually tried that
I actually tried that recommendation after your post and it still didn't check the new address. I currently have the #default_value commented, as it isn't working either way.
Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba
In that case, you haven't set
In that case, you haven't set #value or #default_value, so the first option will always be selected no matter what happens.
As I mentioned before, I have
As I mentioned before, I have it working now with that nasty little for-loop that I created:
So, basically, the #default_value in my radios definition is really not needed. Would appreciate it, if you could let me know, if the reason for the initial default value getting set is the ajax button being a #type "submit". If that is the case, then what can I do to get around it, as I need my #submit handlers to be called when the ajax save button is clicked.
Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba
I had the same problem
I had the same problem with my code today.
I had to update a textarea element based on selectbox's value and #default_value didn't work, and #value was continuously breaking the user subbmition until I moved the code that sets #value to my AJAX callback.
Now my textarea element is updating properly and user input is properly delivered to my submit handler.
About AJAX callbacks and #default_value in form elements
I found this thread after running into a similar problem when trying to use #default_value in form elements when using AJAX callbacks. Here is some example code for doing this:
Form modifications in wrong place
I realize this is an old comment but I'm updating it for posterity.
According to https://www.drupal.org/node/752056
In this case that's
my_example_form()
Also since
$form['price']
is fixed value, the default value is not really being used contrary to the description.http://japodomingo.com
AJAX Radio Button Value Change Solution that works for me
I'm also changing the default values of the form using ajax callback. (Trigger is user making a selection in a dropdown --- triggers different default values in radios etc of the form that they can then edit.) For maintainability reasons, I wanted to do all this server-side using the Drupal Form API rather than use custom jQuery/javascript tricks to change the radio button values after building the page.
Rebuilding the form from ajax automatically works fine for dropdown lists, custom markup, and text inputs. However, and frustratingly, the radio button and checkbox controls of the Form API behave differently; and that difference is really not well documented. I guess this is an edge case that not many developers run into.
Here is what worked for me...
After implementing that, the behavior looks good for my needs.
To change the radio/checkboxes state in AJAX callback
Finally got this worked out
I have a select field in a form. When the select field is changed, a list of checkboxes is loaded. So, depending on selected value, I need to see checked elements. My solution is to change the state of checkboxes in AJAX callback. I added checkboxes (not checkbox) field in a form without default value. And in AJAX callback I changed checkboxes' '#checked' to TRUE, depending on selected value of a select list.
Mission Successfully
Thanks, it works!!!!!!!
In Drupal 8 before to do this trick you have to call Checkboxes::processCheckboxes on ajax callback. More info: https://www.drupal.org/node/2758631
This one seems to be the correct solution
upd: The key is to check FormBuild::processForm() - there you will see that the form is build twice. At first always an empty $form_state array is used and user input mapped to the form elements. Then it calls other #process callbacks for the children elements. When done, the second cycle is performed with already ready $form_state.
So here we use #process callback to create a list of options, the list itself is keyed with the selected option key, so if the select was changed no value from user input will get mapped to the options list and the #default value will be used.