Hi all,
I'm currently working on creating a text field component that checks its own text-value against a database. I have the database return type as a list, but I cannot find a way or place to put the code to have it constantly checking against the users input.
the pseduo code would look something like this:
bds = system.db.runNamedQuery("Admin_user/get_userInfo/get_allUserNames", {})
myList = bds.getColumnAsList(bds.getColumnIndex('username'))
if self.props.text in myList:
#Display some type of overlay to the user that they cannot use that user name
else:
#do nothing as the username should be acceptable
Does anyone know how I would be able to achieve this and where I would need to run this script to do so?
Thank you,
Cam
Is there any reason you aren't using built-in system.user
functions or system.perspective.isAuthorized
function?
Hey @Cameron_Nickle
The pseduo code seems to be okay in theory. However, it will be better that we iterate the list in a loop and before comparison, go for lower case and trim the string (strip function). That way same name, but different case will also be flagged and if there are any leading/tailing spaces, they will also get removed.
The best place to call this script will be onFocus and onBlur events of the text field. If more granularity is required, then key press event can also be captured. However, the text field updated value is only reflected once user commits the value, either by pressing enter or loosing / gaining focus.
Hi Usman,
I've updated the code to look like this:
def runAction(self, event):
#Get all the usernames
bds = system.db.runNamedQuery("Admin_user/get_userInfo/get_allUserNames", {})
#convedrt the dataset to a list
myList = bds.getColumnAsList(bds.getColumnIndex('username'))
#Pre-process the data for accurate comparison
for indx, val in enumerate(myList):
myList[indx] = val.strip().lower()
selfVal = self.props.text
selfVal = selfVal.strip().lower()
if selfVal in myList:
self.getSibling("Test1").props.text = 'error'
else:
self.getSibling("Test1").props.text = 'good'
Which is all good an well, however, I'd like to be able to place some sort of 'error' overlay on-top of the text field to indicate to the user that they cannot have that name as opposed to simply printing 'error' beside the text field. Do you know of a way to do this?
Thank you,
Cam
Depending on what you're trying to do, there might be better ways to approach this. I think we have a case of the xy problem.
But nonetheless, I'll suggest another, more general way of doing this.
So you have a text field where a user types things, and a label that displays the result of the comparison of the user input against a data structure.
now:
- Add a custom property, and bind it to your query.
- Set the input field
deferUpdate
property to false
- Add a structure binding on the label. Bind it on both the input field and the custom property
- Add an expression transform to that binding, and use the
lookup
expression function to find the input into the dataset.
If your data structure is not a dataset (it should be, since it's the result of a query), use a script binding instead of an expression binding.