Is anyone using the `UniqueValidator` with the `cb...
# box-products
l
Is anyone using the
UniqueValidator
with the
cbValidation
module? It bombs out on line 61:
var IdValue  = invoke( target, "get#validationData.keyProperty#" );
with this error:
component [models.myModel] has no function with name [getid]
Indeed, my model does not have an
id
property nor a getter for it. Is this a requirement?
r
The unique validator uses the Id property to check if the current entity is refering to itself.
The unqiueValidator needs to know the primary column (and property) to craft the sql
By default it
id
but you can specify the primary key by adding
keyProperty
and
keyColumn
l
@Ryan Albrecht thank you for your response. So, this sql is all I would need:
Copy code
var sql     = "Select 1 from #validationData.table# where #validationData.column# = :name";
the table's primary key is unrelated to the check?
r
No that would not work, because if you are UPDATING an entity that sql would return a record which would therefore fail the uniqueness check and you would not be able to update your entity. You are only thinking in terms of inserting into the database
To fix just simply add your primary key as a property to your entity.
which i assume is
id
Are you using an ORM library?
l
It is not
id
, but ok... so as to my original question: it appears that an "id" property is a requirement? and/or a diff primary key needs to be passed in. That works now, thank you.
no, not using ORM at all
p
If your primary key is NOT
id
then just define it as Ryan is saying with those properties.
That way the uniqueValidator validator will understand how to do its checks.
l
yep, I get that now... it's just not mentioned in any documentation that I found. So, it was a dive into the source to see why it's bombing.
r
Ok to answer your question. The
id
property is not specifically required but a property that defines your primary key is. By default the unique validator will assume it is called
id
. If it is different you can specify this in the validation declaration eg.
Copy code
this.constraints = {
		team_name: { 
			unique = { 
				table : "team", 
				column : "name",
                //the unique validator will call getTeamId() on your object
				keyProperty: 'teamId', 
                //the unique validator will add the sql - 'and teamId <> :id'
				keyColumn: 'teamId'
			 }
		},
	};
And you are right. I too had to delve into the source code to remember whats going on !
p
Yea some docs suck or bare minimal given.
l
@Ryan Albrecht yes, thank you. I get it now... and pretty spot on that my field name is actually
team_name
and the PK is
teamID
... 👀
r
OOooohh snap you owe me a coke!
👍 1
l
@Ryan Albrecht I don't suppose you've ever used a custom message in combination with the
unique
validator? I've tried adding a
uniqueMessage
in both the
team_name
parent struct and the
unique
struct, but it's not picked up in either? Other custom messages work fine.
r
I had this issue as well. Admittedly I did not spend a whole lot of time trying to solve it. I eventually just made a note to return to the issue one day and just moved on.
Let me take another look
l
OK, thanks for the sanity check. It's not critical, just aesthetics.
r
looks like there is a bug in uniqueValidator class
@lmajano on this line
I dont think this line should be executed??
@wil-shiftinsert
p
Random but what occurs if you wrap your field name in
"team_name":
. I usually am just using cborm for this stuff not sure about the method for just cbvalidation tool.
I sorta recall having this issue back in the day but couldnt find my cold from before but that is one thing I noticed on some of my uniqueValidator setups
w
Hi, 11 pm here 😀 . Not sure what your problem is now. But I know custom messages and metadata have their problems
r
Oops. Yeah if I comment that line out it works great. Also it seems this.name should be 'Unique' instead of 'uniqueValidator'
@Patrick I can try that.
w
I think there is some serialization problem with meta data for some objects. And i18n messages also have their problems. I discovered even more problems but for fixing them cbvalidation needed some fundamental changes
I didn’t get a lot of feedback at the time so I stopped working on improvements. That was 3 or 4 years ago I just created my own validators to work around the limitations
Maybe I should have a look again, I have more time now.