CRM_Core_Form - Support html5 field types
authorColeman Watts <coleman@civicrm.org>
Wed, 23 Dec 2015 04:50:17 +0000 (23:50 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 26 Dec 2015 03:07:56 +0000 (22:07 -0500)
CRM/Core/Form.php
CRM/Core/Form/Renderer.php

index 82e4e0afeff0f006f62bec691ee3ec8b05f02929..c4a4d65b90f14d1bd111c86915b66d185aa18cd3 100644 (file)
@@ -201,6 +201,16 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   private $_chainSelectFields = array();
 
+  /**
+   * Extra input types we support via the "add" method
+   * @var array
+   */
+  public static $html5Types = array(
+    'number',
+    'url',
+    'email',
+  );
+
   /**
    * Constructor for the basic form page.
    *
@@ -327,7 +337,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     $attributes = '', $required = FALSE, $extra = NULL
   ) {
     // Fudge some extra types that quickform doesn't support
-    if ($type == 'wysiwyg' || $type == 'number') {
+    if ($type == 'wysiwyg' || in_array($type, self::$html5Types)) {
       $attributes = ($attributes ? $attributes : array()) + array('class' => '');
       $attributes['class'] = ltrim($attributes['class'] . " crm-form-$type");
       $type = $type == 'wysiwyg' ? 'textarea' : 'text';
@@ -1359,10 +1369,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     $isSelect = (in_array($widget, array(
           'Select',
           'Multi-Select',
-          'Select State/Province',
-          'Multi-Select State/Province',
-          'Select Country',
-          'Multi-Select Country',
           'AdvMulti-Select',
           'CheckBoxGroup',
           'RadioGroup',
@@ -1391,16 +1397,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
         $props['data-api-field'] = $props['name'];
       }
     }
-    //Use select2 library for following widgets.
-    $isSelect2 = (in_array($widget, array(
-          'Select',
-          'Multi-Select',
-          'Select State/Province',
-          'Multi-Select State/Province',
-          'Select Country',
-          'Multi-Select Country',
-    )));
-    if ($isSelect2) {
+    // Use select2 library for following widgets.
+    if (in_array($widget, array('Select', 'Multi-Select'))) {
       $props['class'] = (!empty($props['class']) ? $props['class'] . ' ' : '') . "crm-select2";
       if ($props['context'] == 'search' || strpos($widget, 'Multi') !== FALSE) {
         $props['class'] .= ' huge';
@@ -1417,10 +1415,12 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     // TODO: refactor switch statement, to separate methods.
     switch ($widget) {
       case 'Text':
-      case 'Link':
+      case 'Url':
+      case 'Number':
+      case 'Email':
         //TODO: Autodetect ranges
         $props['size'] = isset($props['size']) ? $props['size'] : 60;
-        return $this->add('text', $name, $label, $props, $required);
+        return $this->add(strtolower($widget), $name, $label, $props, $required);
 
       case 'hidden':
         return $this->add('hidden', $name, NULL, $props, $required);
index 86a39b8b47e0e3b728a75595f8e14ecea0c67b96..60e997eeeee39fee424e5077b96421c70375e083 100644 (file)
@@ -192,9 +192,16 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty {
     elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) {
       self::preprocessContactReference($element);
     }
-    // Hack to support number fields
-    elseif (strpos($class, 'crm-form-number') !== FALSE) {
-      $element->setAttribute('type', 'number');
+    // Hack to support html5 fields (number, url, etc)
+    else {
+      foreach (CRM_Core_Form::$html5Types as $type) {
+        if (strpos($class, "crm-form-$type") !== FALSE) {
+          $element->setAttribute('type', $type);
+          // Also add the "base" class for consistent styling
+          $class .= ' crm-form-text';
+          break;
+        }
+      }
     }
 
     if ($required) {