[Coldbox] validation error

I have a simple validation process set up but something isn’t right.

In coldbox.cfc I have -

validation = {
sharedConstraints = {
sharedUser = {
lname = {required=true},
addr1 = {required=true},
city = {required=true},
country = {required=true},
email = {required=true, type=“email”},
ofage = {required=true, type=“boolean”},
eligible = {required=true, type=“boolean”},
formeremp = {required=true, type=“boolean”}
loginForm = {
username = {required=true}, password = {required=true}
changePasswordForm = {
password = {required=true,min=6}, password2 = {required=true, sameAs=“password”, min=6}

I then have a form coming in for a new user. One of the radio button fields is “eligible”, which has two options - yes/no. Above it’s defined as required = true and type = boolean.

The handler does the following -

<cfset prc.validationResults = validateModel( target=rc, constraints=“sharedUser” ) />

If the field “eligible” is not passed in (neither radio button checked) I would expect it would get reported back as one of my validation errors. Instead I get the following -

Application Execution ExceptionError Type: GenericObject.InvalidKey : [N/A]

Error Messages: The key requested ‘ELIGIBLE’ does not exist in the collection

I read thru the docs again and I see that "required’ means “Whether the property must have a non-null value”. So, for radio buttons where the property doesn’t exist at all if nothing checked, what is the recommended way to treat these?

Thank you!


Also, I realize I can just cfparam the value at the top of the function. Was just attempting to take validation completely off the handler and, in that spirit, wondering if I was missing something. Thx.

For radio buttons coming from forms, you have no choice to param them. Or you could create them before you dsiplay the form, so that the next request could already know about them. But like any client side work, radio buttons and checkboxes must be param or pre initialized.

Yes thanks. It’s just the “required” flag in the config threw me. I get that it simply checks for len. Not sure why it doesn’t check keyexists prior also. Very likely it makes sense why it doesn’t if I were to look at it more closely. This is just my first real Coldbox app so I’m picking along as I go. I’ve also got some “if this field then that field” type stuff so I’ll likely just make something of my own. Wanted to get a handle on what’s built-in first though. Thanks again. Irv

Andrew, I still don’t agree. If the key for the value does not exist it should be treated as a value not entered, not throw an error. Now we have to param everything everytime before we can call validateModel.

Jim Ward

That’s correct, it does exactly what is passed from the browser and the browser doesn’t pass it, so how do you expect ColdBox to predict what you want?

What Andrew said. If you want a default value for an ORM entity, you should specify that when you map the properties - or param it as noted below. The browser doesn’t send keys for unchecked radios or checkboxes and never has.

Also, if you want friendly validation messages instead of throwing errors, you should be using Coldbox’s built-in validation for ActiveEntities.


} else {
    var validationResults = myEntity.getValidationResults();
    //do stuff here…