Validation confusioin

I am confused as at what point do I need to validate my fields with CB 4

I am trying to populate a model from rc and then validate it like so:

prc.validationResults = validateModel( userLevelService.populate( target=userLevelService.new(), memento=data, nullEmptyInclude="*" ) );

I did try to populate first and then run the validation with the same result:

Type: BeanPopulator.PopulateBeanException
Messages: Error populating bean allint.modules.aiadmin.models.userlevel with argument USERLEVELRATE of type class java.lang.String. Simple values are booleans, numbers, strings, and date-time values.
The value cannot be converted to a numeric because it is not a simple value.

The reason I am getting this particular error is that USERLEVELRATE is numeric but I pass a string in my form.

It appears the error is thrown before I even get to validation - at the populate() function that invokes the userlevel.CFC that has this for USERLEVELRATE constraints section:

userLevelRate = { required=true, requiredMessage=“Rate is required, Enter 0 for free.”, type=“numeric”,typeMessage=“Must Be a number with optional cents”}

So, the question is: how do I validate the model, if the error is thrown at populate, and before validation?

Thank you!

Then validate the RC instead of the model since you need validated values before populating.

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

Thanks, Luis.

How do I validate rc/form scope with the built in validator? I only managed to find the validateModel() function so far. Bear with me, I am relearning even that little I knew in 2008 when I got out of the developer business.

Thank you!

No problem

Well, you use the same validateModel() function, but instead of sending a model object, you send a model structure:

validateModel( target=rc, constratints=rules )

Where rules are the constraints to apply to the RC struct.

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

Thank you, Luis,

I tried to do this: prc.validationResults = validateModel(target=rc, fields=“userLevelName, userLevelRate”, constraints=“userLevelForm”);
(tried allcaps just in case)
the config file has this in it in the shared section:

userLevelForm = {
userLevelName = {required=true, requiredMessage=“Name is required”, type=“string”},
userLevelRate = {required=true, requiredMessage=“Enter 0 if free”, type=“numeric”, typeMessage=“Numbers with decimals only”}
}

When I dump prc, I see this

struct
VALIDATIONRESULTS
component cbvalidation.models.result.ValidationResult
implements allint.modules.cbvalidation.models.result.IValidationResult
$MIXED true
PROPERTIES
errors
array [empty]

resultMetadata
struct [empty]

locale [empty string]
targetName userLevelForm
constraints
struct
USERLEVELNAME
struct
REQUIRED true
REQUIREDMESSAGE Name is required
TYPE string

USERLEVELRATE
struct
REQUIRED true
REQUIREDMESSAGE Enter 0 if free
TYPE numeric
TYPEMESSAGE Numbers with decimals only

resourceService
component cbi18n.models.ResourceService
$MIXED true
METHODS

Everything seems to be in place, the constraints are registered, but the errors array is empty, although I enter a string in the USERLEVELRATE field which is numeric. The same happens if I leave the USERLEVELRATE empty, while it is required on constraints.

Any ideas of what I am doing wrong?

Thank you

A followup - when the USERLEVELNAME field is submitted blank, the validator does work this one instance and populates the error array correctly. No validation is performed on the USERLEVELRATE still. Instead of dealing with the rc directly, I placed the two fields in a struct and tried to validate. Same result. Works only if the first field is empty and ignores the numeric one. Tried to change type from “numeric” to the ormtype “big_decimal” - same behaviour.

Any suggestions would be much appreciated!

I created a sample, just run with command box, server start in the root.

It validates accordingly, so I cannot replicate your issue.

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

vTest.zip (1.3 MB)

Thank you, Luis!

I ran a few tests and the culprit was:

prc.validationResults=validateModel(target=rc, fields=“USERLEVELNAME, USERLEVELRATE”, constraints=“userLevelForm”);

As soon as I removed the part in bold above, everything works now.

Thank you again!