From 0874bee083d120248774b424ebde76d71dc90805 Mon Sep 17 00:00:00 2001 From: Mookesh Nanda Date: Sun, 16 Apr 2023 17:35:44 +0800 Subject: [PATCH 1/9] [twgit] Init feature 'feature-VALIDATION-4_required_if_match_system_config'. From c8c57a03949f36c5fd975fde37dbea60b42ae9d1 Mon Sep 17 00:00:00 2001 From: Mookesh Nanda Date: Sun, 16 Apr 2023 17:47:35 +0800 Subject: [PATCH 2/9] VALIDATION 4 add requiredIfOtherFieldMatchSystemLookup rule --- README.md | 14 ++++++++++ .../validation/ValidationExtrasValidators.cfc | 28 +++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d084a5..8dbd3be 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,20 @@ Makes a field required if the value of another field matches the specified value ``` +### requiredIfOtherFieldMatchSystemLookup +Makes a field required if the value of another field matches systemSetting. In this example, if the ethnicity value matches the value on systemSetting ( category="lookup", setting="ethnicity_other" ), then the ethnicity_other field will be required +```xml + + + + + + + + +``` + + ### simpleUrl Checks for a simple web URL. Requires either `http://` or `https://` at the start. diff --git a/services/validation/ValidationExtrasValidators.cfc b/services/validation/ValidationExtrasValidators.cfc index e0db5e5..cdb9835 100644 --- a/services/validation/ValidationExtrasValidators.cfc +++ b/services/validation/ValidationExtrasValidators.cfc @@ -4,11 +4,18 @@ component { - public any function init() { + /** + * @systemConfigurationService.inject systemConfigurationService + * + */ + public any function init( + required any systemConfigurationService + ) { variables.SIMPLE_URL_REGEX = "^https?:\/\/([-_A-Z0-9]+\.)+[-_A-Z0-9]+(\/.*)?$"; variables.UK_POSTCODE_REGEX = "^([A-Z][A-HJ-Y]?\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$"; variables.UK_DRIVING_LICENCE_REGEX = "^[A-Z9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z9]{2}\d[A-Z]{2}$"; variables.UK_PHONE_REGEX = "^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\##(\d{4}|\d{3}))?$"; + _setSystemConfigurationService( arguments.systemConfigurationService ); return this; } @@ -67,6 +74,17 @@ return "function( value, el, params ){ $otherField = $( '[name=' + params[0] + ']' ); if ( !$otherField.length ) { return true}; var otherValue = $otherField.prop('type')=='radio'?$( '[name=' + params[0] + ']:checked').val() : $otherField.val(); if ( otherValue != params[1] ) { return true; } return ( value.length > 0 ); }"; } + public boolean function requiredIfOtherFieldMatchSystemLookup( required string fieldName, any value="", struct data={}, required string otherField, required string category, required setting ) validatorMessage="cms:validation.conditional.required.default" { + var lookupValue = _getSystemConfigurationService().getSetting( category = arguments.category, setting = arguments.setting, default = "" ); + var otherFieldValue = arguments.data[ arguments.otherField ] ?: ""; + + if ( !len( trim ( lookupValue ) ) || !len( trim( otherFieldValue ) ) ) { + return true; + } + + return !( listFindNoCase( lookupValue, otherFieldValue ) && !len( trim( value ) ) ); + } + public boolean function simpleUrl( required string fieldName, any value="" ) validatorMessage="validationExtras:validation.simpleUrl.default" { return IsEmpty( arguments.value ) || ReFindNoCase( variables.SIMPLE_URL_REGEX, arguments.value ); @@ -111,5 +129,11 @@ return "function( value, el, params ){ return !value.length || value.match( /#variables.UK_DRIVING_LICENCE_REGEX#/i ) !== null }"; } - + // GETTERS AND SETTERS + private any function _getSystemConfigurationService() { + return _systemConfigurationService; + } + private void function _setSystemConfigurationService( required any systemConfigurationService ) { + _systemConfigurationService = arguments.systemConfigurationService; + } } From f648ddbdfa4f0f2a7c6a791f8b4178e4b9d18c24 Mon Sep 17 00:00:00 2001 From: Russel Cole Date: Fri, 21 Apr 2023 10:48:16 +0100 Subject: [PATCH 3/9] [twgit] Init feature 'feature-VALIDATION-5_requiredIfOtherFieldValues-to-allow-required-for-multiple-values'. From 50b3615af91cdd8bb262f576e2cc9f73633afc81 Mon Sep 17 00:00:00 2001 From: Russel Cole Date: Fri, 21 Apr 2023 10:52:13 +0100 Subject: [PATCH 4/9] VALIDATION-5 requiredIfOtherFieldInValues --- services/validation/ValidationExtrasValidators.cfc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/validation/ValidationExtrasValidators.cfc b/services/validation/ValidationExtrasValidators.cfc index e0db5e5..ade997b 100644 --- a/services/validation/ValidationExtrasValidators.cfc +++ b/services/validation/ValidationExtrasValidators.cfc @@ -67,6 +67,18 @@ return "function( value, el, params ){ $otherField = $( '[name=' + params[0] + ']' ); if ( !$otherField.length ) { return true}; var otherValue = $otherField.prop('type')=='radio'?$( '[name=' + params[0] + ']:checked').val() : $otherField.val(); if ( otherValue != params[1] ) { return true; } return ( value.length > 0 ); }"; } + public boolean function requiredIfOtherFieldInValues( required string fieldName, any value="", required struct data, required string otherField, required string otherFieldValues ) validatorMessage="cms:validation.conditional.required.default" { + + var otherValues = ListToArray( arguments.otherFieldValues ); + if ( ( !ArrayContains( otherValues, arguments.data[ arguments.otherField ] ?: "" ) ) ) { + return true; + } + return arguments.data.keyExists( fieldName ) && !IsEmpty( value ); + } + + public string function requiredIfOtherFieldInValues_js() validatorMessage="cms:validation.required.default" { + return "function( value, el, params ){ $otherField = $( '[name=' + params[0] + ']' ); if ( !$otherField.length ) { return true}; var otherValue = $otherField.prop('type')=='radio'?$( '[name=' + params[0] + ']:checked').val() : $otherField.val(); var otherValues = params[1].split(',');if ( !otherValues.includes( otherValue ) ) { return true; } return ( value.length > 0 ); }"; + } public boolean function simpleUrl( required string fieldName, any value="" ) validatorMessage="validationExtras:validation.simpleUrl.default" { return IsEmpty( arguments.value ) || ReFindNoCase( variables.SIMPLE_URL_REGEX, arguments.value ); From 60813815b40cfbb328e37f4e332594fa7042d987 Mon Sep 17 00:00:00 2001 From: Russel Cole Date: Fri, 21 Apr 2023 11:06:52 +0100 Subject: [PATCH 5/9] VALIDATION-5 requiredIfOtherFieldInValues --- services/validation/ValidationExtrasValidators.cfc | 1 + 1 file changed, 1 insertion(+) diff --git a/services/validation/ValidationExtrasValidators.cfc b/services/validation/ValidationExtrasValidators.cfc index ade997b..9a8b164 100644 --- a/services/validation/ValidationExtrasValidators.cfc +++ b/services/validation/ValidationExtrasValidators.cfc @@ -67,6 +67,7 @@ return "function( value, el, params ){ $otherField = $( '[name=' + params[0] + ']' ); if ( !$otherField.length ) { return true}; var otherValue = $otherField.prop('type')=='radio'?$( '[name=' + params[0] + ']:checked').val() : $otherField.val(); if ( otherValue != params[1] ) { return true; } return ( value.length > 0 ); }"; } + public boolean function requiredIfOtherFieldInValues( required string fieldName, any value="", required struct data, required string otherField, required string otherFieldValues ) validatorMessage="cms:validation.conditional.required.default" { var otherValues = ListToArray( arguments.otherFieldValues ); From 5b5c233d5562054d89fedbeb7edc5cfaf9c0d6ba Mon Sep 17 00:00:00 2001 From: Russel Cole Date: Fri, 21 Apr 2023 11:07:48 +0100 Subject: [PATCH 6/9] [twgit] Init release 'release-1.2.0'. From c76d3c5b7f7c69b0bc0600d8f414d0f74bdd9b58 Mon Sep 17 00:00:00 2001 From: Mookesh Nanda Date: Mon, 24 Apr 2023 12:36:28 +0800 Subject: [PATCH 7/9] VALIDATION-4 use @singleTon and @presideService on the service --- .../validation/ValidationExtrasValidators.cfc | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/services/validation/ValidationExtrasValidators.cfc b/services/validation/ValidationExtrasValidators.cfc index cdb9835..ee6d772 100644 --- a/services/validation/ValidationExtrasValidators.cfc +++ b/services/validation/ValidationExtrasValidators.cfc @@ -1,21 +1,15 @@ /** + * @singleton + * @presideService * @validationProvider */ +component { - component { - - /** - * @systemConfigurationService.inject systemConfigurationService - * - */ - public any function init( - required any systemConfigurationService - ) { + public any function init() { variables.SIMPLE_URL_REGEX = "^https?:\/\/([-_A-Z0-9]+\.)+[-_A-Z0-9]+(\/.*)?$"; variables.UK_POSTCODE_REGEX = "^([A-Z][A-HJ-Y]?\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$"; variables.UK_DRIVING_LICENCE_REGEX = "^[A-Z9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z9]{2}\d[A-Z]{2}$"; variables.UK_PHONE_REGEX = "^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\##(\d{4}|\d{3}))?$"; - _setSystemConfigurationService( arguments.systemConfigurationService ); return this; } @@ -75,7 +69,7 @@ } public boolean function requiredIfOtherFieldMatchSystemLookup( required string fieldName, any value="", struct data={}, required string otherField, required string category, required setting ) validatorMessage="cms:validation.conditional.required.default" { - var lookupValue = _getSystemConfigurationService().getSetting( category = arguments.category, setting = arguments.setting, default = "" ); + var lookupValue = $getPresideSetting( category = arguments.category, setting = arguments.setting, default = "" ); var otherFieldValue = arguments.data[ arguments.otherField ] ?: ""; if ( !len( trim ( lookupValue ) ) || !len( trim( otherFieldValue ) ) ) { @@ -128,12 +122,4 @@ public string function ukDrivingLicence_js() { return "function( value, el, params ){ return !value.length || value.match( /#variables.UK_DRIVING_LICENCE_REGEX#/i ) !== null }"; } - - // GETTERS AND SETTERS - private any function _getSystemConfigurationService() { - return _systemConfigurationService; - } - private void function _setSystemConfigurationService( required any systemConfigurationService ) { - _systemConfigurationService = arguments.systemConfigurationService; - } -} +} \ No newline at end of file From 140269c4a1441ac3da2081f0abde758427d8b631 Mon Sep 17 00:00:00 2001 From: Dominic Watson Date: Mon, 24 Apr 2023 10:00:04 +0100 Subject: [PATCH 8/9] [twgit] Init feature 'feature-VALIDATION-4_helpers-to-validate-using-system-settings'. From ee7f9a73bcf5237a76232d7b36306ad283921baf Mon Sep 17 00:00:00 2001 From: Dominic Watson Date: Tue, 2 May 2023 12:29:04 +0100 Subject: [PATCH 9/9] changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e7b625..c901f04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v1.2.0 + +* [VALIDATION-5](https://projects.pixl8.london/browse/VALIDATION-5) - requiredIfOtherFieldInValues to allow required for multiple values +* [VALIDATION-4](https://projects.pixl8.london/browse/VALIDATION-4) - Add rule for a field to be required if other field match system config + ## v1.1.0 * VALIDATION-3 - requiredIfOtherFieldValue should work with radio lists