CRM-13966 - select2 - set default & allowClear from markup
[civicrm-core.git] / js / Common.js
index d219d60c5560baa77919c29e5e39894f9dc22ec3..37f299e93df3fd27a033b5c017ac8a1a2eed01d3 100644 (file)
@@ -290,6 +290,13 @@ CRM.validate = CRM.validate || {
         $(this).remove();
       });
       var options = $(this).data('select-params') || {};
+      // Set placeholder from markup if not specified
+      if ($(this).is('select:not([multiple])')) {
+        options.allowClear = options.allowClear !== undefined ? options.allowClear : !($(this).hasClass('required'));
+        if (options.placeHolder === undefined && $('option:first', this).val() === '') {
+          options.placeholderOption = 'first';
+        }
+      }
       // Api-based searching
       if ($(this).data('api-params')) {
         $(this).addClass('crm-ajax-select')