Form example using CXP
Posted: Thu Nov 13, 2014 5:30 pm
I always struggled with validating data that was sent back in a form, but with CXP, this is so easy and elegant.
This is all handled in one CXP page.
An array is used to define all the items on the form.
The class DC_NoIvarContainer is used to dynamically create the form variables.
The commands DCFORM, DCTABLE, DCINPUT and DCHTML are used to render the HTML code.
Give it a try:
http://donnay-software.com/ds/form.cxp
FORM.CXP
This is all handled in one CXP page.
An array is used to define all the items on the form.
The class DC_NoIvarContainer is used to dynamically create the form variables.
The commands DCFORM, DCTABLE, DCINPUT and DCHTML are used to render the HTML code.
Give it a try:
http://donnay-software.com/ds/form.cxp
FORM.CXP
Code: Select all
<%#Code locality="page-global"%>
<%
#include "dcdialog.ch"
#include "dccxp.ch"
FUNCTION RenderForm( aItems, lSubmitted )
LOCAL GetList := {}, oTable, i, oForm, cHtml, lValid := .t., cVarName, cValue
// Validate each form item
FOR i := 1 TO Len(aItems)
cVarName := aItems[i,4]
cValue := aItems[i,2]
IF cVarName $ {'Name','Street','City','State','Zip','Email','Ccard','ExpDate','CvvCode' } ;
.AND. Empty(cValue)
aItems[i,5] := '***'
ELSEIF cVarName = 'Email' .AND. !'@' $ cValue
aItems[i,5] := '*** Invalid Email Address'
ELSEIF cVarName = 'Ccard' .AND. Len(Trim(cValue)) < 15
aItems[i,5] := '*** Card Number must be 15 or 16 digits'
ELSEIF cVarName = 'ExpDate' .AND. Len(Trim(cValue)) < 5
aItems[i,5] := '*** Expiration Date must be mm/yy'
ELSEIF cVarName = 'CvvCode' .AND. Len(Trim(cValue)) < 3
aItems[i,5] := '*** CvvCode must be 3 or more digits'
ENDIF
IF !Empty(aItems[i,5])
lValid := .f.
ENDIF
NEXT
IF lValid
cHtml := '<h2>Thank you for visiting our website. Come again soon!</h2>'
cHtml += '<pre>'
FOR i := 1 TO Len(aItems)
cHtml += aItems[i,1] + ': ' + aItems[i,2] + Chr(13)+Chr(10)
NEXT
cHtml += '</pre>'
ELSE
DCFORM OBJECT oForm METHOD 'POST' ACTION './Form.Cxp?submit'
DCTABLE OBJECT oTable ROWS Len(aItems)+4 COLUMNS 2 PARENT oForm BORDER 0
FOR i := 1 TO Len(aItems)
@ i,1 DCHTML aItems[i,1] PARENT oTable TDOPTION "align=right", "width=200"
@ i,2 DCINPUT VALUE IIF(Empty(aItems[i,2]),nil,aItems[i,2]) ;
VARNAME aItems[i,4] ;
SIZE aItems[i,3] ;
MAXLENGTH aItems[i,3] ;
PARENT oTable
@ i,2 DCHTML aItems[i,5] STYLE "color:red" PARENT oTable
NEXT
@ i, 2 DCHTML IIF(lSubmitted,'<em><b>Try Again!! Make the RED go away!!</b></em><br>','') + ;
'Please fill in all fields that are marked with ***' ;
PARENT oTable COLSPAN 2
@ i + 2, 2 DCSUBMIT CAPTION 'Submit Form!' VARNAME 'Submit' PARENT oTable
DCREAD HTML TO cHtml
ENDIF
RETURN cHtml
%>
<%#Code locality="page-render"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<link rel="stylesheet" type="text/css" href="dccxp.css" />
</head>
<body>
<%
IF PCount() == 0
oForm := DC_NoIvarContainer():new()
ELSE
oForm := ::httpRequest:form
ENDIF
aItems := { ;
{ 'Name',oForm:name,20,'Name',''}, ;
{ 'Street',oForm:street,30,'Street','' }, ;
{ 'City',oForm:city,30,'City','' }, ;
{ 'State',oForm:state,2,'State','' }, ;
{ 'Zip',oForm:zip,10,'Zip','' }, ;
{ 'Phone',oForm:phone,20,'Phone','' }, ;
{ 'Email',oForm:email,60,'Email','' }, ;
{ 'Credit Card Number',oForm:ccard,16,'Ccard','' }, ;
{ 'Expiration Date (mm/yy)',oForm:expdate,5,'ExpDate','' }, ;
{ 'CVV Code',oForm:cvvcode,4,'CvvCode','' } ;
}
cHtml := RenderForm( aItems, PCount()>0 )
? cHtml
%>
</body>
</html>