From f3631c02fa291cca60c5d7f5018a0b6202148be8 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 10 Feb 2015 22:04:59 -0500 Subject: [PATCH] CRM-15949 - Fix check for duplicate options and set defalt values ---------------------------------------- * CRM-15949: Don't allow the same value for different custom field options https://issues.civicrm.org/jira/browse/CRM-15949 --- CRM/Custom/Form/Field.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index dd569f6de9..7630298f2e 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -243,6 +243,7 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { for ($i = 1; $i <= self::NUM_OPTION; $i++) { $defaults['option_status[' . $i . ']'] = 1; $defaults['option_weight[' . $i . ']'] = $i; + $defaults['option_value[' . $i . ']'] = $i; } if ($this->_action & CRM_Core_Action::ADD) { @@ -577,6 +578,8 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { $errors = array(); + self::clearEmptyOptions($fields); + //validate field label as well as name. $title = $fields['label']; $name = CRM_Utils_String::munge($title, '_', 64); @@ -719,7 +722,7 @@ SELECT count(*) $nextIndex = $start + 1; while ($nextIndex <= self::NUM_OPTION) { if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] && - !empty($fields['option_value'][$nextIndex]) + strlen($fields['option_value'][$nextIndex]) ) { $errors['option_value[' . $start . ']'] = ts('Duplicate Option values'); $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values'); @@ -900,6 +903,7 @@ AND option_group_id = %2"; public function postProcess() { // store the submitted values in an array $params = $this->controller->exportValues($this->_name); + self::clearEmptyOptions($params); if ($this->_action == CRM_Core_Action::UPDATE) { $dataTypeKey = $this->_defaultDataType[0]; $params['data_type'] = self::$_dataTypeKeys[$this->_defaultDataType[0]]; @@ -1020,4 +1024,19 @@ SELECT id $this->ajaxResponse['customField'] = $customField->toArray(); } + /** + * Removes value from fields with no label. + * + * This allows default values to be set in the form, but ignored in post-processing. + * + * @param array $fields + */ + public static function clearEmptyOptions(&$fields) { + foreach ($fields['option_label'] as $i => $label) { + if (!strlen(trim($label))) { + $fields['option_value'][$i] = ''; + } + } + } + } -- 2.25.1