Commit 0d0919c5 authored by Kurund Jalmi's avatar Kurund Jalmi

Merge pull request #3041 from freeform/CRM-14470

CRM-14470 - Prevent users from adding two 'is_primary' fields of the same field type
parents cedc53b9 fdcc5d07
......@@ -713,6 +713,45 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
}
}
/**
* Validation rule to prevent multiple fields of primary location type within the same communication type.
*
* @param Array $fields Submitted fields
* @param String $profileFieldName Group Id.
* @param Array $groupFields List of fields already in the group
* @param Array $errors Collect errors
*
* @static
* @access public
*/
static function formRulePrimaryCheck($fields, $profileFieldName, $groupFields, &$errors) {
//FIXME: This may need to also apply to website fields if they are refactored to allow more than one per profile
$checkPrimary = array('phone' => 'civicrm_phone.phone', 'phone_and_ext' => 'civicrm_phone.phone');
$whereCheck = NULL;
$primaryOfSameTypeFound = NULL;
$fieldID = empty($fields['field_id']) ? 0 : $fields['field_id'];
// Is this a primary location type field of interest
if (array_key_exists($profileFieldName, $checkPrimary)) {
$whereCheck = $checkPrimary[$profileFieldName];
}
$potentialLocationType = CRM_Utils_Array::value(2, $fields['field_name']);
if ($whereCheck && $potentialLocationType == 0) {
$primaryOfSameTypeFound = '';
foreach ($groupFields as $groupField) {
// if it is a phone
if ($groupField['where'] == $whereCheck && is_null($groupField['location_type_id']) && $groupField['field_id'] != $fieldID) {
$primaryOfSameTypeFound = $groupField['title'];
break;
}
}
if ($primaryOfSameTypeFound) {
$errors['field_name'] = ts('You have already added a primary location field of this type: %1', $primaryOfSameTypeFound);
}
}
}
/**
* global validation rules for the form
*
......@@ -737,14 +776,14 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
$errors['is_view'] = ts('A View Only field cannot be required');
}
$fieldName = $fields['field_name'][0];
if (!$fieldName) {
$entityName = $fields['field_name'][0];
if (!$entityName) {
$errors['field_name'] = ts('Please select a field name');
}
if ($in_selector && in_array($fieldName, array(
if ($in_selector && in_array($entityName, array(
'Contribution', 'Participant', 'Membership', 'Activity'))) {
$errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $fieldName));
$errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $entityName));
}
$isCustomField = FALSE;
......@@ -773,6 +812,11 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
}
}
// Get list of fields already in the group
$groupFields = CRM_Core_BAO_UFGroup::getFields($fields['group_id'], FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE);
// Check if we already added a primary field of the same communication type
self::formRulePrimaryCheck($fields, $profileFieldName, $groupFields, $errors);
//check profile is configured for double option process
//adding group field, email field should be present in the group
//fixed for issue CRM-2861 & CRM-4153
......
{htxt id='field_name_0-title'}
{ts}Field Name{/ts}
{/htxt}
{htxt id='field_name_0'}
{capture assign="ltURL"}{crmURL p="civicrm/admin/locationType" q="reset=1"}{/capture}
<p>
{ts}Choose the field to add to the profile.{/ts}<br/>
<strong>{ts}Restrictions:{/ts}</strong><br/>
{ts}Only one field of each communication type (EG. phone) can use the primary location type.{/ts}
</p>
<p>
{ts 1=$ltURL}When you choose the primary location type the collected data will use the default location type from the <a href="%1">Location types</a> setup.{/ts}
</p>
{/htxt}
{htxt id='label-title'}
{ts}Field Label{/ts}
{/htxt}
......
......@@ -34,7 +34,7 @@
<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
<table class="form-layout-compressed">
<tr class="crm-uf-field-form-block-field_name">
<td class="label">{$form.field_name.label}</td>
<td class="label">{$form.field_name.label} {help id='field_name_0'}</td>
<td>{$form.field_name.html}<br />
<span class="description">&nbsp;{ts}Select the type of CiviCRM record and the field you want to include in this Profile.{/ts}</span></td>
</tr>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment