Using field events to let users know before saving a record that a record already exists.
My scenario is I do not want a duplicate Job Title added to the table.
In order to prevent a duplicate field value added, we typically use the "Check if specific record exists" before record added event. Something like:
string strSQLExists = "select JobTitle from dbo.Jobs where JobTitle='"+values["JobTitle"].ToString()+"'";
XVar rsExists = CommonFunctions.db_query(strSQLExists, null);
XVar data = CommonFunctions.db_fetch_array(rsExists);
if(data)
{
message = "This Job Title already exisits.";
return false;
}
else
{
return true;
}
This works perfectly, but wouldn't it be nice if we could let the user know before they even click save?
Here is how we can do that:
On the fields section, click properties of the field you do not want a duplicate. In the upper right, click Field Events.
Your event should be set to editing (dropdown on top). Now click New Handler.
Client Before:
params["val"] = this.getValue();
Server:
string strSQLExists = "select * from Careers.dbo.Jobs where JobTitle='"+parameters["val"].ToString()+"'";
XVar rsExists = CommonFunctions.db_query(strSQLExists, null);
XVar data = CommonFunctions.db_fetch_array(rsExists);
if(data)
{
result["newJobTitle"] = parameters["val"].ToString() + " " + "exists. Please enter another title.";
}
else
{
result["newJobTitle"] = "";
}
Client After:
$("#JobTitleTip").remove();
$("input[id=value_JobTitle_1]").after("<div id='JobTitleTip' style='margin-top: 10px; color: red; font-size: 120%;'>"+result["newJobTitle"]+"</div>");
There you go.
Now having said all that. This scenario can also be accomplished with a simple click of a box. I just wanted to demonstrate the Field Events in action.