I am using the email column validator on a 3.5 form. Works great.
My problem is to do with a situation where I am programmatically moving data from a source db table into a table with an email column that is being validated.
If an email is invalid, the column validator will fire during foundset load and the load attempt fails. I can turn off the column validator (at design), but this isn’t where I want to go …
My question is this: how do I call, programmatically, the email validator so that I can validate the inbound string before loading? Code snippet follows. The import table, which is in forms[readfile].email1, contains a bad email address … I want to test for bad emails in the external table during the load and then only bring in good emails into my email field.
if (forms[readfile].email1 != null)
if (forms[readfile].email1.length != 0)
{
{
forms.communicate.controller.newRecord(false);
forms.communicate.name_id = forms[newfile].name_id;
forms.communicate.comm_order = commcnt;
commcnt += 1;
forms.communicate.comm_type = 'Email-1' ;
forms.communicate.email = forms[readfile].email1;
}
}
Suggestions? Thanks Michael
You could always use your own method for validating email addresses. The standard is to use regular expressions. Below is some sample code using a very basic RegExp to validation an email. You can find many more RegExp at http://www.regexlib.com . Just search for email.
var myEmail = "sbutler@servoy.com"
//remember that a single \ must be put in with 2 \ like below... \=\\
var myRegEx = new RegExp("^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}$")
var myMatch = myEmail.match(myRegEx)
if(myMatch)
plugins.dialogs.showInfoDialog("","Good Email","ok")
else
plugins.dialogs.showInfoDialog("","Bad Email","ok")
Thanks, Scott - tip appreciated!
However, imho, with the notions of database integrity in mind, inbound information into a db backend will typically arise from multiple sources. Two major ones being “keypunch” and “import routines”.
I am thinking it would be good to be able to programmatically ensure that inbound data (via a third party plugin (like IT2BE’s) or via a roll-your-own-whatever) would be able to follow the same regex/validation rules as what is going on through keypunch in a Servoy form.
So I am thinking that if one or more validation rules (e.g. - email etc) exist within the data providers, we should be able to call these rules against an inbound column and make sure good stuff is going into the db. Also, if the regex rule I choose differs from say what is going on in the validation class, I can still get an error (ie - my validation assumptions differ from what is in the native Servoy validators) and then need to deal with the exception being thrown (ok this way I suppose … haven’t learned Servoy exception handlers yet
).
Do you happen to know if this facility is in 3.5 or should it go in the feature request hopper?
Michael
From what I understand what you would like to do is a feature request. If I get this right you want to be able to call the validator before it gets fired by Servoy’s own mechanisms.
I could imagine a call like
databaseManager.validateValue(server_name, table_name, column_name, value)
This points to the exact validation used and could run validation. If no validation is defined for a column, such a call would always return true. If that’s what you want, make a request.
Thanks … I will submit as a feature request,
Michael
but with such a call you always need exactly know what to call.
If you for example changed it or added a validation to another column then you also need to change the import code everytime?
the problem is that it would be much better if we could catch it
{
try
{
forms.communicate.controller.newRecord(false);
forms.communicate.name_id = forms[newfile].name_id;
forms.communicate.comm_order = commcnt;
commcnt += 1;
forms.communicate.comm_type = 'Email-1' ;
forms.communicate.email = forms[readfile].email1;
}
catch(e)
{
// record failed.
}
}
the problem is that currently javascript isn’t being able to catch java exceptions. maybe we should look into this a bit futher in a next release.
goldcougar:
var myRegEx = new RegExp("^\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,3}$")
BTW, This regexp is not correct, it doesn’t allow dots in the left part of the address (like john.doe@company.com).
I use the following code:
var oRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
Hope this helps.
Thanks Robert, I’m using this code (courtesy of Enrico Arata):
if(emailToCheck.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
{
// Good email address
return true;
}
Hi Johan,
Sure … I agree that there is the concern for development not catching a change to the column validation or adding validation to another column. However, I think this is more of a business process concern (technical project mgmt, unit testing, integration testing, QA etc). What would happen if say I was to adopt the regex that Nicola has recommended (looks good!) and then Servoy was to change their email column validator? Same fundamental problem (a bug!) … something will fall off the rails somewhere.
In a future release perhaps as you suggest: to catch the exception and deal with it there. I am thinking that this is potentially an area in the context of Eclipse cutover and revisiting the areas/overall approach for encapsulation and error control/catching at that time. This also has additional context in the whole philosophy of column and data provider validations. This would include, for example, recent threads on mandatory columns and how to ensure data is noted as mandatory at the form level without using a global method for catching exceptions.
I do appreciate and understand certain current architectural constraints you may have with respect to Javascript. As such, I do not want to be unduly burdensome while you are working through Eclipse cutover and related architectural matters. As always, I appreciate and value the helpful feedback from you and others!
Best, Michael