1

I'm having a strange problem. I am constructing a form with checkboxes to toggle email subscription preferences on and off. For the new user sign-up form, this is straightforward:

$form['marketing-container']['restaurant_newsletter_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Subscribe to Restaurant Newsletter'),
    '#return_value' => "True",
    '#default_value' => "False",
);

However, when it comes to the update user form, I'm running into a problem. If I set #default_value to the existing preference, it will check the boxes for those publications you're subscribed to correctly, but if you UNcheck those boxes, the form API just returns the default value....which would still be TRUE.

To work around this, I retrieved the subscriber object at the start of the form function, and I've added hidden fields whose default values are the existing subscription preference values, just like I attempted to do with the real fields in the first place. Then, I used #states to populate the real fields based on these hidden fields, like so:

$form['marketing-container']['existing_restaurant_newsletter_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Restaurant Newsletter'),
    '#value' => $attributes[15]->Value,
);

$form['marketing-container']['restaurant_newsletter_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Subscribe to Restaurant Newsletter'),
    '#return_value' => "True",
    '#default_value' => "False",
    '#states' => array(
        'checked' => array(
            ':input[name="existing_restaurant_newsletter_subscription"]' => array('checked' => TRUE)
        )
    )
);

Now this works! The problem is that the first field is visible. Now if I alter this to hide the field:

$form['marketing-container']['existing_restaurant_newsletter_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Restaurant Newsletter'),
    '#value' => $attributes[15]->Value,
    '#access' => FALSE,
);

The field is hidden perfectly....but its value isn't copied over to the real field that's still visible.

I've searched all over and pored over the Form API documentation, and I'm stumped.

Is there some way to preserve this default value inside the hidden form so it copies over to another field correctly?

If there isn't, or if it's more efficient, is there a DIFFERENT method I could use to pre-check this checkbox but still keep a default value of FALSE so that if a user unchecks it, something actually happens? Is there another class of field I'm missing that is a sort of on-off switch that returns specified values for each state?

0

Figured it out on my own. The solution was:

$form['restaurant_newsletter_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Subscribe to Restaurant Newsletter'),
    '#return_value' => "True",
    '#default_value' => $attributes[15]->Value,
    '#prefix' => '<div class="subscription-toggle">',
    '#suffix' => '</div>',
    '#ajax' => array(
        'callback' => 'ajax_replace_default_callback',
        'wrapper' => 'restaurant_newsletter_subscription_div',
    ),
);

And for the callback:

function ajax_replace_default($form, $form_state) {
    $field_name = $form_state['triggering_element']['#name'];
    $form[$field_name]['#default_value'] = "False";
    return $form[$field_name];
}

And I also kept the hidden fields in the form:

$form['existing_restaurant_newsletter_subscription'] = array(
    '#type' => 'checkbox',
    '#title' => t('Restaurant Newsletter'),
    '#value' => $attributes[15]->Value,
    '#access' => FALSE,
);

This was simpler to store the existing preference variables for comparison in my logic later on than to try to retrieve the subscriber yet again inside of that form submit handler (It already retrieved the subscriber when it loaded the form, to populate the fields....trying to minimize the number of API calls I have to make).

Implemented like this, if a box is checked and becomes unchecked, that Ajax callback function alters the default value to false, rather than returning just the pre-populated value. If you check the box again it still changes the default to false, but it doesn't matter since if the box is checked that's irrelevant.

Not the most elegant fix, you get a little loading circle thing for a second when the Ajax runs, but it works.

Note: I left the Ajax callback function generic because even though I keep using the same field on this page, there are lots of similar fields doing the same thing. Using the triggering_element allows me to just recycle this code for all of them.

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.