/** * global validation rules for the form * * @param array $fields (referance) posted values of the form * * @return array if errors then list of errors to be posted back to the form, * true otherwise * @static * @access public */ static function formRule(&$fields, &$files, &$form) { // all option fields are of type "money" $errors = array(); /** Check the option values entered * Appropriate values are required for the selected datatype * Incomplete row checking is also required. */ if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) && $fields['html_type'] == 'Text' && $fields['price'] == NULL) { $errors['price'] = ts('Price is a required field'); } //avoid the same price field label in Within PriceSet $priceFieldLabel = new CRM_Price_DAO_Field(); $priceFieldLabel->label = $fields['label']; $priceFieldLabel->price_set_id = $form->_sid; $dupeLabel = false; if ($priceFieldLabel->find(true) && $form->_fid != $priceFieldLabel->id) { $dupeLabel = true; } if ($dupeLabel) { $errors['label'] = ts('Name already exists in Database.'); } if ($form->_action & CRM_Core_Action::ADD) { if ($fields['html_type'] != 'Text') { $countemptyrows = 0; for ($index = self::NUM_OPTION; $index > 0; $index--) { $noLabel = $noAmount = $noWeight = 1; if (!empty($fields['option_label'][$index])) { $noLabel = 0; $duplicateIndex = CRM_Utils_Array::key($fields['option_label'][$index], $fields['option_label']); if (!($duplicateIndex === false) && !($duplicateIndex == $index)) { $errors["option_label[{$index}]"] = ts('Duplicate label value'); } } // allow for 0 value. if (!empty($fields['option_name'][$index]) || strlen($fields['option_name'][$index]) > 0) { $noAmount = 0; } if (!empty($fields['option_weight'][$index])) { $noWeight = 0; $duplicateIndex = CRM_Utils_Array::key($fields['option_weight'][$index], $fields['option_weight']); if (!($duplicateIndex === false) && !($duplicateIndex == $index)) { $errors["option_weight[{$index}]"] = ts('Duplicate weight value'); } } if ($noLabel && !$noAmount) { $errors["option_label[{$index}]"] = ts('Label cannot be empty.'); } if (!$noLabel && $noAmount) { $errors["option_name[{$index}]"] = ts('Amount cannot be empty.'); } if ($noLabel && $noAmount) { $countemptyrows++; } } if ($countemptyrows == 11) { $errors["option_label[1]"] = $errors["option_name[1]"] = ts('Label and value cannot be empty.'); } } $_showHide =& new CRM_Core_ShowHideBlocks('', ''); // do not process if no option rows were submitted if (empty($fields['option_name']) && empty($fields['option_label'])) { return true; } if (empty($fields['option_name'])) { $fields['option_name'] = array(); } if (empty($fields['option_label'])) { $fields['option_label'] = array(); } $dupeLabels = array(); $count = 0; for ($idx = 1; $idx <= self::NUM_OPTION; $idx++) { $_flagOption = 0; $_rowError = 0; $showBlocks = 'optionField_' . $idx; // both value and label are empty if ($fields['option_name'][$idx] == '' && $fields['option_label'][$idx] == '') { $_showHide->addHide($showBlocks); $count++; if ($count == 11) { $showBlocks = 'optionField_' . '1'; $_showHide->addShow($showBlocks); } continue; } $_showHide->addShow($showBlocks); if ($fields['option_name'][$idx] != '') { // check for empty label if ($fields['option_label'][$idx] == '') { $errors['option_label][' . $idx . ']'] = ts('Option label cannot be empty'); } // all fields are money fields if (!CRM_Utils_Rule::moneySigned($fields['option_name'][$idx])) { $_flagOption = 1; $errors['option_name[' . $idx . ']'] = ts('Please enter a valid money value.'); } } if ($fields['option_label'][$idx] != '') { // check for empty value if ($fields['option_name'][$idx] == '') { $errors['option_name][' . $idx . ']'] = ts('Option value cannot be empty'); } // check for duplicate labels, if not already done if (isset($dupeLabels[$idx])) { continue; } $also_in = array_keys($fields['option_label'], $fields['option_label'][$idx]); // first match is always the current key unset($also_in[0]); if (!empty($also_in)) { $_flagOption = 1; $errors['option_label][' . $idx . ']'] = ts('Duplicate Option label'); foreach ($also_in as $also_in_key) { $errors['option_name][' . $also_in_key . ']'] = ts('Duplicate Option label'); $dupeValues[$also_in_key] = true; } } } if ($_flagOption) { $_showHide->addShow($showBlocks); $_rowError = 1; } // last row - hide "Additional Option" option if ($idx == self::NUM_OPTION) { $hideBlock = 'additionalOption'; $_showHide->addHide($hideBlock); } } $_showHide->addToTemplate(); } return empty($errors) ? true : $errors; }