From ac79e2f51211e4381f3a413fdaecf5842ee4c05e Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Mon, 21 Oct 2013 18:25:49 +0530 Subject: [PATCH] CRM-13490 : the concept used for fixing this is - detach the 'required' form rule for address custom data field, check if the address is filled i.e. intended to be submitted if 'yes' then check for 'required' custom field form rule ---------------------------------------- * CRM-13490: Addresses with required custom fields can't be deleted from the Edit screen http://issues.civicrm.org/jira/browse/CRM-13490 --- CRM/Contact/Form/Contact.php | 2 +- CRM/Contact/Form/Domain.php | 2 +- CRM/Contact/Form/Edit/Address.php | 28 +++++++++++++++++++++- CRM/Core/BAO/Address.php | 31 ++++++++++++++++++++++++- CRM/Event/Form/ManageEvent/Location.php | 2 +- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 6cea50882c..000931f50e 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -591,7 +591,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { } if (array_key_exists('Address', $this->_editOptions)) { - $this->addFormRule(array('CRM_Contact_Form_Edit_Address', 'formRule')); + $this->addFormRule(array('CRM_Contact_Form_Edit_Address', 'formRule'), $this); } if (array_key_exists('CommunicationPreferences', $this->_editOptions)) { diff --git a/CRM/Contact/Form/Domain.php b/CRM/Contact/Form/Domain.php index 74d0df2e23..1e1671122b 100644 --- a/CRM/Contact/Form/Domain.php +++ b/CRM/Contact/Form/Domain.php @@ -207,7 +207,7 @@ class CRM_Contact_Form_Domain extends CRM_Core_Form { static function formRule($fields) { $errors = array(); // check for state/country mapping - CRM_Contact_Form_Edit_Address::formRule($fields, $errors); + $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject); //fix for CRM-3552, //as we use "fromName" format for domain email. diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index 42c8369761..ffae0dcc3d 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -274,6 +274,14 @@ class CRM_Contact_Form_Edit_Address { // And we can't set it to 'address_' because we want to set it in a slightly different format. CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); + // detach required rule for custom data. + // during contact editing : if no address is filled + // required custom data must not produce 'required' form rule error + // more handling done in formRule func + if (!$inlineEdit) { + CRM_Core_BAO_Address::detachRequiredRule(&$form, $groupTree); + } + $template = CRM_Core_Smarty::singleton(); $tplGroupTree = $template->get_template_vars('address_groupTree'); $tplGroupTree = empty($tplGroupTree) ? array( @@ -312,8 +320,14 @@ class CRM_Contact_Form_Edit_Address { * @access public * @static */ - static function formRule($fields) { + static function formRule($fields, $files, $self) { $errors = array(); + + $customDataRequiredFields = array(); + if (property_exists($self, '_addressRequireOmission')) { + $customDataRequiredFields = explode(',', $self->_addressRequireOmission); + } + // check for state/county match if not report error to user. if (CRM_Utils_Array::value('address', $fields) && is_array($fields['address'])) { foreach ($fields['address'] as $instance => $addressValues) { @@ -321,6 +335,18 @@ class CRM_Contact_Form_Edit_Address { continue; } + // attach 'required' form rule error to + // custom data only if address data exists i.e. user intended to fill address + // but forgot to fill in required fields + if (!empty($customDataRequiredFields) && CRM_Core_BAO_Address::dataExists($addressValues)) { + foreach($customDataRequiredFields as $elementName) { + if (!CRM_Utils_Array::value($elementName, $addressValues)) { + $label = $self->getElement("address[$instance][$elementName]")->getLabel(); + $errors["address[$instance][$elementName]"] = ts('%1 is required field', array(1 => $label)); + } + } + } + $countryId = CRM_Utils_Array::value('country_id', $addressValues); $stateProvinceId = CRM_Utils_Array::value('state_province_id', $addressValues); diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 21be1e027d..113c522053 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -1198,4 +1198,33 @@ SELECT is_primary, } return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); } -} + + /** + * CRM-13490 : helper function to detach required rule for address custom data + */ + static function detachRequiredRule(&$form, $groupTree) { + if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') { + $requireOmission = NULL; + foreach ($groupTree as $csId => $csVal) { + // only process Address entity fields + if ($csVal['extends'] != 'Address') { + continue; + } + + foreach ($csVal['fields'] as $cdId => $cdVal) { + if ($cdVal['is_required']) { + // unset-ing of required rule done here, also store the element name whose required rule is been removed + $elementName = $cdVal['element_name']; + if (in_array($elementName, $form->_required)) { + $form->_required = array_diff($form->_required, array($elementName)); + // store the omitted rule for a element, to be used later on + $requireOmission = $cdVal['element_custom_name'] . ','; + } + } + } + } + + $form->_addressRequireOmission = rtrim($requireOmission, ','); + } + } +} \ No newline at end of file diff --git a/CRM/Event/Form/ManageEvent/Location.php b/CRM/Event/Form/ManageEvent/Location.php index 738e06e892..05cc054ab6 100644 --- a/CRM/Event/Form/ManageEvent/Location.php +++ b/CRM/Event/Form/ManageEvent/Location.php @@ -165,7 +165,7 @@ class CRM_Event_Form_ManageEvent_Location extends CRM_Event_Form_ManageEvent { */ static function formRule($fields) { // check for state/country mapping - $errors = CRM_Contact_Form_Edit_Address::formRule($fields); + $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject); return empty($errors) ? TRUE : $errors; } -- 2.25.1