Skip to content

validation constraints issue#264

Merged
grantcopley merged 7 commits intocoldbox-modules:developmentfrom
mrigsby:cbvalidation-bug
Mar 23, 2026
Merged

validation constraints issue#264
grantcopley merged 7 commits intocoldbox-modules:developmentfrom
mrigsby:cbvalidation-bug

Conversation

@mrigsby
Copy link
Contributor

@mrigsby mrigsby commented Mar 23, 2026

When using the Component.cfc validate() method from a wire to validate another model and the wire has constraints defined, it uses the wires constraints to validate and not the constraints defined in the provided model.

For example if your wire defines this constraint:

constraints = {
    "username" : {
        required : true,
        size : "6..30"
    }
};

and you have a saveUser action like this:

function saveUser(){
    var oUser = getInstance( "User" );
    
    	oUser.setUsername( data.username )
        	.setEmail( "NotAValidEmail.com" );
        
    var validationResults = validate( oUser );
    
    // ...
}

The validate()method uses the wires constraints and not the constraints defined in the model

This pull request also addresses an issue where if the constraints are defined on the wire using the way that cbValidation says to in the documentation, e.g. this.constraints={} the _getConstraints() method will not return the constraints.

https://coldbox-validation.ortusbooks.com/overview/declaring-constraints/domain-object

The _getConstraints() method has been updated to check both the variables and this scopes to look for constraints. Note: preference is given to the variables scope as the cbwire documentation suggests they are defined.

https://cbwire.ortusbooks.com/features/form-validation

  • Modified logic used in Component.cfc validate() method to only use the wires constraints when it is the wire itself being validated 
  • Added tests to validate the proper constraints are being used to validate
  • Modified Component.cfc _getConstraints() method to also check the this scope for constraints
  • Added tests to validate Component.cfc _getConstraints() will return constraints from either the variables or this scopes 
  • Passed all tests for all supported engines in local dev (now lets see if they pass on github)
  • and... apparently my vscode removed whitespace in a bunch of places 🤷‍♂️

@grantcopley grantcopley added the bug Something isn't working label Mar 23, 2026
@grantcopley grantcopley requested a review from Copilot March 23, 2026 15:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes CBWIRE validation behavior so Component.validate( target=someModel ) uses the target model’s constraints (instead of the wire’s), and ensures wire constraints can be discovered whether declared in variables.constraints or this.constraints.

Changes:

  • Adjusts ValidationService.validate() defaulting logic so wire constraints are only applied when validating the wire’s own data.
  • Updates Component._getConstraints() to check both variables and this scopes (preferring variables).
  • Adds new TestBox coverage and supporting test-harness fixtures for the above behaviors.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
models/services/ValidationService.cfc Changes defaulting of target/constraints to avoid overriding non-wire targets with wire constraints.
models/Component.cfc Enhances _getConstraints() to return constraints from either variables or this scope.
test-harness/tests/specs/unit/services/ValidationServiceSpec.cfc Adds unit tests covering validation of non-wire targets and custom constraints precedence.
test-harness/tests/specs/CBWIRESpec.cfc Adds integration-style assertions for _getConstraints() scope resolution.
test-harness/models/validationTest.cfc Introduces a domain object fixture with constraints for non-wire validation tests.
test-harness/wires/test/validation/validateConstraints1.cfc / .cfm Adds wire fixture declaring constraints via this.constraints.
test-harness/wires/test/validation/validateConstraints2.cfc / .cfm Adds wire fixture declaring constraints via variables.constraints (constraints = ...).

grantcopley and others added 5 commits March 23, 2026 10:52
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@grantcopley grantcopley merged commit 07fadb9 into coldbox-modules:development Mar 23, 2026
9 of 11 checks passed
@grantcopley
Copy link
Collaborator

Merged and released as 5.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants