From dd2835184da3dd17204d7035fb21dd071038ec4d Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 24 Jul 2020 15:40:19 +1000 Subject: [PATCH] [REF] Fix jquery validation for radios Fix layout of required radio message --- CRM/Core/Form.php | 9 ++++++++- js/Common.js | 2 +- templates/CRM/common/l10n.js.tpl | 10 +++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 7d48b6569e..b1f638cccc 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -366,6 +366,9 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $type, $name, $label = '', $attributes = '', $required = FALSE, $extra = NULL ) { + if ($type === 'radio') { + CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_Form::addRadio'); + } // Fudge some extra types that quickform doesn't support $inputType = $type; if ($type == 'wysiwyg' || in_array($type, self::$html5Types)) { @@ -1202,7 +1205,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } } } - $options[] = $this->createElement('radio', NULL, NULL, $var, $key, $optAttributes); + $element = $this->createElement('radio', NULL, NULL, $var, $key, $optAttributes); + if ($required) { + $element->setAttribute('required', TRUE); + } + $options[] = $element; } $group = $this->addGroup($options, $name, $title, $separator); diff --git a/js/Common.js b/js/Common.js index 6f41eab124..88acc9ef4d 100644 --- a/js/Common.js +++ b/js/Common.js @@ -883,7 +883,7 @@ if (!CRM.vars) CRM.vars = {}; var that = this; validator.settings = $.extend({}, validator.settings, CRM.validate._defaults, CRM.validate.params); // Call our custom validation handler. - $(validator.currentForm).on("invalid-form.validate", validator.settings.invalidHandler ); + $(validator.currentForm).on("invalid-form.validate", validator.settings.invalidHandler); // Call any post-initialization callbacks if (CRM.validate.functions && CRM.validate.functions.length) { $.each(CRM.validate.functions, function(i, func) { diff --git a/templates/CRM/common/l10n.js.tpl b/templates/CRM/common/l10n.js.tpl index bba775f40a..f6eec45ea6 100644 --- a/templates/CRM/common/l10n.js.tpl +++ b/templates/CRM/common/l10n.js.tpl @@ -96,7 +96,15 @@ errorClass: 'crm-inline-error alert-danger', messages: {}, ignore: '.select2-offscreen, [readonly], :hidden:not(.crm-select2), .crm-no-validate', - ignoreTitle: true + ignoreTitle: true, + errorPlacement: function(error, element) { + if (element.prop('type') === 'radio') { + error.appendTo(element.parent('div.content')); + } + else { + error.insertAfter(element); + } + } }; // use civicrm notifications when there are errors -- 2.25.1