diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 7314693bca2..9d92c7dd880 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -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) { @@ -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; } diff --git a/CRM/Contact/Form/Inline/Website.php b/CRM/Contact/Form/Inline/Website.php index c4664a66519..14f64d71587 100644 --- a/CRM/Contact/Form/Inline/Website.php +++ b/CRM/Contact/Form/Inline/Website.php @@ -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); + } /** @@ -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; + } + }