Skip to content

Commit

Permalink
Merge branch 'release-1.2.0' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicWatson committed May 2, 2023
2 parents 0f523b8 + ee7f9a7 commit 411c359
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ Makes a field required if the value of another field matches the specified value
</field>
```

### 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
<field name="ethnicity" control="objectPicker" object="ethnicity" />
<field name="ethnicity_other" control="textInput">
<rule validator="requiredIfOtherFieldMatchSystemLookup">
<param name="otherField" value="ethnicity" />
<param name="category" value="lookup" />
<param name="setting" value="ethnicity_other" />
</rule>
</field>
```


### simpleUrl

Checks for a simple web URL. Requires either `http://` or `https://` at the start.
Expand Down
33 changes: 28 additions & 5 deletions services/validation/ValidationExtrasValidators.cfc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* @singleton
* @presideService
* @validationProvider
*/

component {
component {

public any function init() {
variables.SIMPLE_URL_REGEX = "^https?:\/\/([-_A-Z0-9]+\.)+[-_A-Z0-9]+(\/.*)?$";
Expand Down Expand Up @@ -67,6 +68,30 @@
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 = $getPresideSetting( 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 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 );
Expand Down Expand Up @@ -110,6 +135,4 @@
public string function ukDrivingLicence_js() {
return "function( value, el, params ){ return !value.length || value.match( /#variables.UK_DRIVING_LICENCE_REGEX#/i ) !== null }";
}


}
}

0 comments on commit 411c359

Please sign in to comment.