This is run once upon startup and applies to each page in which there is one or more forms. It Creates the JavaScript that must be run to pre validate form content before submission to the server. Arguments: None Returns: None
| Return Type | Function name | Arguments |
|---|---|---|
| void | hdsPage::WriteValidationJS | (void) |
Declared in file: hzDissemino.h
Defined in file : hdsGenerate.cpp
Function Logic:
Function body:
void hdsPage::WriteValidationJS (void)
{
// This is run once upon startup and applies to each page in which there is one or more forms. It Creates the JavaScript that must be run to pre
// validate form content before submission to the server.
//
// Arguments: None
// Returns: None
_hzfunc("hdsPage::WriteValidationJS") ;
hzList<hdsFormref*>::Iter fi ; // Form iterator
hzChain Z ; // For building page validation script(s)
hdsFormref* pFormref ; // Form in page
hdsFormdef* pFormdef ; // Form in page
hdsField* pFld ; // Field in form
uint32_t n ; // Field iterator
// Check for pre-submission requirements. This is where one or more fields can only be validated by querying the server. For example in a form to
// register a new user, the email address must be new. The 'onchange' event is used to check on the server for the existance of the field class,
// member and value.
threadLog("DOING PAGE %s\n", *m_Title) ;
for (fi = m_xForms ; fi.Valid() ; fi++)
{
pFormref = fi.Element() ;
threadLog("DOING FORM REF %p\n", pFormref) ;
if (!pFormref)
{ threadLog("ERROR Page %s has a NULL form reference\n", *m_Title) ; continue ; }
// pFormdef = pFormref->m_pFormdef ;
pFormdef = m_pApp->m_FormDefs[pFormref->m_Formname] ;
if (!pFormdef)
{ threadLog("ERROR Page %s has a NULL form definition\n", *m_Title) ; continue ; }
for (n = 0; n < pFormdef->m_vecFlds.Count() ; n++)
{
pFld = pFormdef->m_vecFlds[n] ;
if (!pFld->m_Varname)
continue ;
if (!(pFld->m_flagVE & VE_UNIQUE))
continue ;
if (!pFld->m_pClass || !pFld->m_pMem)
continue ;
Z.Printf("function ckUnique_%s()\n{\n", *pFld->m_Varname) ;
Z.Printf("\tif (ckExists('%s','%s',document.%s.%s.value))\n", pFld->m_pClass->txtType(), pFld->m_pMem->txtName(), *pFormdef->m_Formname, *pFld->m_Varname) ;
Z << "\t{\n" ;
Z.Printf("\t\talert(\"%s already in use\");\n", *pFld->m_Varname) ;
Z.Printf("\t\tdocument.%s.%s.value=\"\";\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
Z.Printf("\t\tdocument.%s.%s.focus();\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
Z << "\t}\n}\n" ;
m_bScriptFlags |= INC_SCRIPT_EXISTS ;
}
// Now do the ordinary format checking validation scripting
Z.Printf("function ck%s()\n{\n", *pFormdef->m_Formname) ;
for (n = 0; n < pFormdef->m_vecFlds.Count() ; n++)
{
pFld = pFormdef->m_vecFlds[n] ;
if (!pFld->m_Varname)
continue ;
if (!(pFld->m_flagVE & VE_COMPULSORY))
continue ;
if (pFld->m_Fldspec.m_pType->Basetype() == BASETYPE_ENUM)
Z.Printf("\tif (document.%s.%s.value==\"0\")\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
else
Z.Printf("\tif (document.%s.%s.value==\"\")\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
Z.Printf("\t\t{ alert(\"Please provide your %s\"); document.%s.%s.focus(); return false; }\n", *pFld->m_Varname, *pFormdef->m_Formname, *pFld->m_Varname) ;
if (pFld->m_Fldspec.m_pType->Basetype() == BASETYPE_EMADDR)
{
Z.Printf("\tif (!ckEmaddr(document.%s.%s.value))\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
Z.Printf("\t\t{ document.%s.%s.focus(); return false }\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
m_bScriptFlags |= INC_SCRIPT_CKEMAIL ;
}
if (pFld->m_Fldspec.m_pType->Basetype() == BASETYPE_URL)
{
Z.Printf("\tif (!ckUrl(document.%s.%s.value))\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
Z.Printf("\t\t{ document.%s.%s.focus(); return false }\n", *pFormdef->m_Formname, *pFld->m_Varname) ;
m_bScriptFlags |= INC_SCRIPT_CKURL ;
}
}
if (pFormdef->m_bScriptFlags & INC_SCRIPT_RECAPTCHA)
{
Z.Printf("\tif (grecaptcha.getResponse()==\"\")\n") ;
Z.Printf("\t\t{ alert(\"Please prove you are human!\"); return false; }\n") ;
}
Z.Printf("\tdocument.%s.submit();\n\treturn true\n}\n", *pFormdef->m_Formname) ;
}
m_validateJS = Z ;
}