0

I have created a checkbox in user edit using form alter and based checkbox state I want to print the message. For this I used below code

function module_form_user_profile_form_alter(&$form, &$form_state) {

  $form['account']['checkbox'] = array(
    '#type' => 'checkbox',
    '#title' => 'Test check',
   );
   $form['#submit'][] = 'module_simplenews_unsubscribe';
}

//~ //Submit handler for Subscribe/unsubscribe newsletter.
function module_simplenews_unsubscribe($form, &$form_state) {

  if ($form['account']['checkbox']['#value'] == 0) {
    drupal_set_message(t("You are checked"));
  }
 if ($form['account']['checkbox']['#value'] == 1) {

    drupal_set_message(t("You are unchecked"));
  }
}

It works fine. When a checkbox is checked/unchecked and user saves the profile it prints respective message. But when user doesn't modify this checkbox field and modifies some other field and saves, again it prints message based on checkbox state.

I want to display the message only when checkbox field is modified. How can I achieve this?

I have tried this https://drupal.stackexchange.com/a/180079/50991 but not working.

2
  • 2
    In your submit check form default_value / value for checkbox and form_state values for checkbox. If both are same then don't display message else display respective message. – Ajay Reddy Nov 13 '15 at 11:32
  • It's the same question you asked a week ago. – apaderno Nov 13 '15 at 13:10