This is a customer information data entry form. First name and Last name are required. In addition phone number, e-mail and birth date are used to check the database for existing customers with the same information.
Either of these fields (phone, e-mail or birth date) can be empty or filled in. In order to validate against database I need to check if the user has filled each field in or not.
Is there a way to refactor the code and still set a different property for each scenario?
The code looks like this:
var sFirstName = Inputs.GetProperty("sFirstName");
var sLastName = Inputs.GetProperty("sLastName");
var sEmail = Inputs.GetProperty("sEmail");
var sPhone = Inputs.GetProperty("sPhone");
var sBirthDate = Inputs.GetProperty("sBirthDate");
var sCoAddress = Inputs.GetProperty("sCoAddress");
var sAddress = Inputs.GetProperty("sAddress");
var sZip = Inputs.GetProperty("sZip");
var sCity = Inputs.GetProperty("sCity");
if(!sFirstName || !sLastName){
TheApplication().RaiseErrorText("First name and last name are required");
}
if(!sEmail && (!sCoAddress || !sAddress || !sZip || !sCity)){
TheApplication().RaiseErrorText("Please enter either e-mail or postal address");
}
if(sEmail && sBirthDate && sPhone){
Outputs.SetProperty("QueryAll","Y");
}
else if(sEmail && sBirthDate && !sPhone){
Outputs.SetProperty("QueryEmailBday","Y");
}
else if(sEmail && !sBirthDate && sPhone){
Outputs.SetProperty("QueryEmailPhone","Y");
}
else if(!sEmail && sBirthDate && sPhone){
Outputs.SetProperty("QueryBdayPhone","Y");
}
else if(sEmail && !sBirthDate && !sPhone){
Outputs.SetProperty("QueryEmail","Y");
}
else if(!sEmail && sBirthDate && !sPhone){
Outputs.SetProperty("QueryBirthDate","Y");
}
else if(!sEmail && !sBirthDate && sPhone){
Outputs.SetProperty("QueryPhone","Y");
}
else if(!sEmail && !sBirthDate && !sPhone){
Outputs.SetProperty("QueryNameOnly","Y");
}
2 Answers 2
You could solve this by constructing your Queryname dynamically:
var queryName = "Query";
if ( sEmail ) queryName += "Email";
if ( sBDay ) queryName += "BDay";
if ( sPhone ) queryName += "Phone";
if ( queryName == "Query" ) queryName = "QueryNameOnly";
Outputs.SetProperty( queryName,"Y" );
But for this you would have to be a little more strict with your Parameter-Names not changing between BDay and BithDate and no Specialcase for ALL
If you are unable to (as @ Falco commented, and answered,) change your "Property"^ names - you could also use a look-up table concept (implemented as a dictionary object):
var params = {
"sEmail" : "Email",
"sPhone" : "Phone",
"sBirthDate" : "BDay"
};
^ Note: As @ Falco implied, these are more correctly 'parameters' or arguments, than properties.
This makes it far more maintainable than a page of if-else blocks.
Input
andGetProperty
. They don't look like native JS (they look like from a Windows platform given the capital-case syntax). Giving a bit of context about your system may allow for better solutions. \$\endgroup\$QueryAll
,QueryEmailBday
, etc.? \$\endgroup\$