http://coldfusion.com logo
b

bhartsfield

07/26/2022, 12:44 PM
Before I start writing custom methods for it, are there any coldbox-validation aficionados out there who can help with some requiredIf/array constraint logic? Details in the thread.
Basically, I have two properties in a bean that are both arrays and at least one of them needs to be required. If either is provided then the other shouldn't be required (both can be provided of course). I thought that might look something like this...
Copy code
this.constraints.append( {
	"array1": { "requiredIf": { "array2": nullValue() } },
	"array2": { "requiredIf": { "array1": nullValue() } }
} );
But, if I populate it with the following and validate it...
{ "array1": [1,2,3] }
I get an exception form the validation module
Copy code
lucee.runtime.exp.ExpressionException: can't compare Complex Object Type Array with a String
	at lucee.runtime.type.util.ArraySupport.compareTo(ArraySupport.java:290)
	at lucee.runtime.op.Operator.compare(Operator.java:202)
	at lucee.runtime.op.Operator.compare(Operator.java:74)
	at models.validators.requiredifvalidator_cfc$cf$49.udfCall(/cbvalidation/models/validators/RequiredIfValidator.cfc:57)
	at ...
I've only used cb validation a couple times and it has always been with simpler values/constraints I guess
w

wil-shiftinsert

07/26/2022, 1:37 PM
I am using cbvalidation a lot but never used requiredUnless, but think you need requiredUnless e.g
Copy code
this.constraints.append( {
	"array1": { "requiredUnless": "array2" },
	"array2": { "requiredUnless": "array1" }
} );
If you look at the RequiredIf and RequiredUnless constraints, you will see they come in two formats, one where the other field has an exacct value, and one where you just have to enter the name of this field. In the latter case, it just checks if this second field is not empty, I guess that’s what you want.
if you need something more complex you probably need an UDF or a CustomValidator. Just let me know if you need them, it is actually quite easy to create one, so I can give you some pointers
b

bhartsfield

07/26/2022, 1:44 PM
well that was easy enough.... requiredUnless instead of requiredIf
5 Views