3

I have webform module with multiselect field (Checkbox) User can select all the three.

PR|Print Required
DP|Digital Print Required
TP|Print has been taken

If user selects the last option then, only that option must be saved after submitting even if the other options are selected. If other two options are selected then as usual those options should be saved.

In General, If the user selects the first two options, it should accept the first two options. If user selects third option, then it should save the third option not other two options(If user selects). Example: If user selects all three options, then it should save only the last option.If user selects the last option i.e., Print has been taken, then it should save that irrespective of other selections.

5
  • 1
    Quick thought: why don't you just use JS to limit selection to only one item? Why are you using the multiselect field in the first place? Apr 23 '14 at 12:02
  • No, I don't want to limit it to one item. As you can see I have three options, If the user selects the first two options, it should accept the first two options. If user selects third option, then it should save the third option not other two options(If user selects). Example: If user selects all three options, then it should save only the last option.If user selects the last option i.e., Print has been taken, then it should save that irrespective of other selections. Apr 23 '14 at 12:57
  • Got it now; I suggest updating the question so it's more clear. Apr 23 '14 at 13:14
  • I updated my question. @Topsitemakers Do you have an answer? Apr 24 '14 at 4:26
  • 1
    I'm bad with time to write you an example, but check what BenMcCloskey did as that is an easier option. Alternative is to use hook_form_alter(), reorder the default callbacks, put yours first and then alter the submitted values. However, I think the JS solution is more user-friendly as the users will then know what's happening. Otherwise, they will select 2-3 options and only 1 will be saved, which is confusing. Hope this helps. Apr 24 '14 at 8:04
2

I would use jQuery to deactivate and uncheck the other two checkboxes when the third has been checked. If they uncheck the third, they will all become active.

Your Drupal form will look something like this:

<div class="form-checkboxes" id="edit-field-terms-und">
<div class="form-item form-type-checkbox form-item-field-terms-und-pr">
    <input type="checkbox" class="form-checkbox" value="pr" name="field_terms[und][pr]" id="edit-field-terms-und-pr">  <label for="edit-field-terms-und-pr" class="option">Print Required</label>
</div>
<div class="form-item form-type-checkbox form-item-field-terms-und-dp">
    <input type="checkbox" class="form-checkbox" value="dp" name="field_terms[und][dp]" id="edit-field-terms-und-dp">  <label for="edit-field-terms-und-dp" class="option">Digital Print Required</label>
</div>
<div class="form-item form-type-checkbox form-item-field-terms-und-tp">
    <input type="checkbox" class="form-checkbox" value="tp" name="field_terms[und][tp]" id="edit-field-terms-und-tp">  <label for="edit-field-terms-und-tp" class="option">Print has been taken</label>
</div>
</div>

jQuery code would be something like this:

(function($) {
$(document).ready(function() { 

    $("#edit-field-terms-und-tp").click(function() {
        if($(this).is(':checked')) { 
            $('#edit-field-terms-und-pr, #edit-field-terms-und-dp').attr('disabled', true).prop('checked', false );
        } else {
            $('#edit-field-terms-und-pr, #edit-field-terms-und-dp').removeAttr('disabled');
        }
    });

});
})(jQuery);

Haven't tested it, but it should work.

0

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.