From 778fd7634950eecf2f049dd0728274bf6633a063 Mon Sep 17 00:00:00 2001 From: Guillaume Boudrias Date: Fri, 27 Jul 2018 11:53:46 -0400 Subject: [PATCH] CRM-21427: Allow multiple websites without type and only allow a single website of specified type --- CRM/Contact/Form/Contact.php | 12 +++++++++- CRM/Contact/Form/Inline/Website.php | 36 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 7314693bca..9d92c7dd88 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -615,6 +615,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { $blocks['Address'] = $otherEditOptions['Address']; } + $website_types = array(); $openIds = array(); $primaryID = FALSE; foreach ($blocks as $name => $label) { @@ -629,8 +630,17 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { } 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; } diff --git a/CRM/Contact/Form/Inline/Website.php b/CRM/Contact/Form/Inline/Website.php index c4664a6651..14f64d7158 100644 --- a/CRM/Contact/Form/Inline/Website.php +++ b/CRM/Contact/Form/Inline/Website.php @@ -87,6 +87,8 @@ class CRM_Contact_Form_Inline_Website extends CRM_Contact_Form_Inline { CRM_Contact_Form_Edit_Website::buildQuickForm($this, $blockId, TRUE); } + $this->addFormRule(array('CRM_Contact_Form_Inline_Website', 'formRule'), $this); + } /** @@ -128,4 +130,38 @@ class CRM_Contact_Form_Inline_Website extends CRM_Contact_Form_Inline { $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; + } + } -- 2.25.1