[ColdBox 4.0] custom validator example

Stuck on writing a custom validator…

In my current ValidationService.cfc I have something like:

<cfset validationRules = { password1 = { required=true, requiredMessage="Please enter password", size="8..30", sizeMessage="The password must be between {min} and {max} characters"} } />

I’d like to be able to add my own rule validateUniquePassword or something like that and pass things off to a CFC or something.

I’ve read the docs and poked through the examples and am a bit lost as to where I define my custom validator CFC and how do I wire things together (I am using Wirebox).

Anyone have a simple example of how to do this?

Thanks much!
Jim

Everyone must be on vacation :frowning:

The docs also mention using a method as a type but I’ve been unsuccessful wiring that in either.

Jim

For your own custom validator just add “validator” = “CFC path or WireBox ID”

That’s it.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

So I place my CFC in models (?)

Then define a method there - isThisValid() - and I’m assuming this would return a boolean?

Then I should be able to call that via my validationRules?

`

`
<cfset validationRules = {
password1 = {
isThisValid = true

} />
`

`

Jim, you need to follow the interface. Docs are here:

http://wiki.coldbox.org/wiki/Validation.cfm#Custom_Validators

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

Let me try this again. I tried to work through the docs but one issue is they all assume the validation stuff is under coldbox but now everything is under my local modules so I’m trying to repath things accordingly:

function [validate(modules.cbvalidation.models.result.ivalidationresult validationresult, any target, string field, [any targetvalue, [string validationdata]])] of component [C:\inetpub\wwwroot\railo\models\customValidation.cfc] does not match the function declaration [validate(cbvalidation.models.result.ivalidationresult validationresult, any target, string field, [any targetvalue, [any validationdata]])] of the interface [C:\inetpub\wwwroot\railo\modules\cbvalidation\models\validators\IValidator.cfc]

Might be useful to add a custom example in the validation sample application.

Jim

Yes, we need to rewrite those as we are migrating things.

Take a look at this custom validator for inspiration:

https://github.com/ColdBox/cbox-cborm/blob/master/modules/cborm/models/validation/UniqueValidator.cfc

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

Jim, this would make a good blog entry/sample app. I’ll make a note to look into it when I have some free time. Alternatively, feel free to blog what you come up with and I’ll help promote it :slight_smile:

~Brad