Skip to content

Commit

Permalink
Merge pull request #13097 from eileenmcnaughton/website_type
Browse files Browse the repository at this point in the history
CRM-21427 - Add form validation to make it clear we only allow a single website of each type
  • Loading branch information
eileenmcnaughton authored Nov 15, 2018
2 parents 4ee2fc3 + 778fd76 commit 4563b5e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion CRM/Contact/Form/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ public static function formRule($fields, &$errors, $contactId, $contactType) {
$blocks['Address'] = $otherEditOptions['Address'];
}

$website_types = array();
$openIds = array();
$primaryID = FALSE;
foreach ($blocks as $name => $label) {
Expand All @@ -629,8 +630,17 @@ public static function formRule($fields, &$errors, $contactId, $contactType) {
}

if ($dataExists) {
// skip remaining checks for website
if ($name == 'website') {
if (!empty($blockValues['website_type_id'])) {
if (empty($website_types[$blockValues['website_type_id']])) {
$website_types[$blockValues['website_type_id']] = $blockValues['website_type_id'];
}
else {
$errors["{$name}[1][website_type_id]"] = ts('Contacts may only have one website of each type at most.');
}
}

// skip remaining checks for website
continue;
}

Expand Down
36 changes: 36 additions & 0 deletions CRM/Contact/Form/Inline/Website.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function buildQuickForm() {
CRM_Contact_Form_Edit_Website::buildQuickForm($this, $blockId, TRUE);
}

$this->addFormRule(array('CRM_Contact_Form_Inline_Website', 'formRule'), $this);

}

/**
Expand Down Expand Up @@ -128,4 +130,38 @@ public function postProcess() {
$this->response();
}

/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
* @param array $errors
* List of errors to be posted back to the form.
* @param CRM_Contact_Form_Inline_Website $form
*
* @return array
*/
public static function formRule($fields, $errors, $form) {
$hasData = $errors = array();
if (!empty($fields['website']) && is_array($fields['website'])) {
$types = array();
foreach ($fields['website'] as $instance => $blockValues) {
$dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);

if ($dataExists) {
$hasData[] = $instance;
if (!empty($blockValues['website_type_id'])) {
if (empty($types[$blockValues['website_type_id']])) {
$types[$blockValues['website_type_id']] = $blockValues['website_type_id'];
}
else {
$errors["website[" . $instance . "][website_type_id]"] = ts('Contacts may only have one website of each type at most.');
}
}
}
}
}
return $errors;
}

}

0 comments on commit 4563b5e

Please sign in to comment.