You are on page 1of 10

LearnDooPHP

Login Home About Contact Forum SUBSCRIBETORSS LearnDooPHP:HighperformancePHPframework 27Sep2009

byMilosKovacki 16Comments Tutorials captcha,difficult,DooForm,form,validation

DooFormlearntoworkwithforms
OkinthistutorialIwillshowyouhowdoyouworkwithforms,itsverysimple. LoadingDooFormhelperisveryeasy:
1 . D o o : : l o a d H e l p e r ( ' D o o F o r m ' )

Firstyoucreateformfromarrayandthenjustcall
1 . e c h o $ f o r m > r e n d e r ( )

Torendertheform,renderfunctionisreturninghtmloftheform.Sowewillbeginmakingonesimpleelement,letssay textfield:
0 1 . $ f o r m = n e w D o o F o r m ( a r r a y ( 0 2 . ' m e t h o d ' = > ' p o s t ' , 0 3 . ' a c t i o n ' = > $ a c t i o n , 0 4 . ' e l e m e n t s ' = > a r r a y ( 0 5 . ' u s e r n a m e ' = > a r r a y ( ' t e x t ' , a r r a y ( 0 6 . ' r e q u i r e d ' = > t r u e , 0 7 . ' l a b e l ' = > ' M y u s e r n a m e : ' , 0 8 . ' a t t r i b u t e s ' = > a r r a y ( " s t y l e " = > ' b o r d e r : 1 p x s o l i d # 0 0 0 ' , ' c l a s s ' = > ' u s e r n a m e f i e l d ' ) , 0 9 . ' f i e l d w r a p p e r ' = > ' d i v ' , 1 0 . ' v a l i d a t o r s ' = > a r r a y ( 1 1 . a r r a y ( ' u s e r n a m e ' , 4 , 7 ) , 1 2 . a r r a y ( ' m a x l e n g t h ' , 6 , ' T h i s i s t o o l o n g ' ) ,

1 3 . 1 4 . 1 5 . 1 6 . ) 1 7 . ) )

a r r a y ( ' m i n l e n g t h ' , 6 ) ) ) )

Asyoucanseeallformdatayoucanaddintoformarray,sowhenyouarecreatingformyouwillhavetodefine method,actionandelementsyouhaveinyourform,elemntsisanotherarray,thatarraywillcontainallformelements. Youbeginwithnamingyourelementandthencreatearraythatwilldescribethatelement(type,label,validators,field wrappers,etc) Soinelementyouwillhaveforexample:


0 1 . ' u s e r n a m e ' = > a r r a y ( ' t e x t ' , a r r a y ( 0 2 . ' r e q u i r e d ' = > t r u e , 0 3 . ' l a b e l ' = > ' M y u s e r n a m e : ' , 0 4 . ' a t t r i b u t e s ' = > a r r a y ( " s t y l e " = > ' b o r d e r : 1 p x s o l i d # 0 0 0 ' , ' c l a s s ' = > ' m i t a r ' ) , 0 5 . ' f i e l d w r a p p e r ' = > ' d i v ' , 0 6 . ' v a l i d a t o r s ' = > a r r a y ( 0 7 . a r r a y ( ' u s e r n a m e ' , 4 , 7 ) , 0 8 . a r r a y ( ' m a x l e n g t h ' , 6 , ' T h i s i s t o o l o n g ' ) , 0 9 . a r r a y ( ' m i n l e n g t h ' , 6 ) ) 1 0 . ) 1 1 . ) ,

Soasyoucanseeyoudefinetypeoftheelementasfirstvalueofthearray,secondvalueisalsoarraythatdefinesother stuffsuchaslabel,attributes,validatorsetcLabelisfieldlabel,attributesarearraythatwillgoinsideelementsoyou canaddstyle,classorsomethingelse.fieldwrapperishtmlthatwrappsyourelement,defaultis<dt>. Nowwewilltalklittleaboutvalidators,allvalidatorsyouhavedefinedinDooValidatorclass,sovalidatorsisanarray, andinityouwillstoreallvalidatorsyouneedforthatelement.Someelementshavebuiltinvalidatorssuchas elementfileorcaptcha,wewilltalkaboutthemlateron.SoasyoucanseeweareusingvalidatorsfromDooValidators class,whenformisvalidatingwithisValid()methodDooFormischeckingvalidationswithDooValidatehelperclass. NowIwillshowyouhowshouldyouworkwiththisforms,forexampleletssaythatwehaveformfromabovethat hasonlyonefieldusername,nowwehavefunctionforexampleregisterandinitwewilldoformvalidation:


0 1 . i f ( $ t h i s > i s P o s t ( ) ) { 0 2 . i f ( $ f o r m > i s V a l i d ( $ _ P O S T ) ) { 0 3 . e c h o " F o r m i s v a l i d ! " 0 4 . } e l s e { 0 5 . e c h o ' F o r m i s n o t v a l i d p l e a s e c o r r e c t a l l e r r o r s ' 0 6 . $ t h i s > v i e w ( ) > f o r m = $ f o r m > r e n d e r ( ) 0 7 . } 0 8 . } e l s e { 0 9 . $ t h i s > v i e w ( ) > f o r m = $ f o r m > r e n d e r ( ) 1 0 . }

AsyoucanseeisPostmethoddoesntrealyexistinDooControllerclassitsjustmyfunctionforcheckingifmethodis post,Iwillnowshowyoufunctionincaseyouwanttoknowhowitlookslike:
1 . p u b l i c f u n c t i o n i s P o s t ( ) { 2 . i f ( $ _ S E R V E R [ ' R E Q U E S T _ M E T H O D ' ] = = " P O S T " ) r e t u r n t r u e 3 . e l s e r e t u r n f a l s e 4 . }

Soifmethodispostthenformissubmitedandweneedtoseeifitsvalid,wearedoingthatwithisValid()function thatfunctionwillseeifallformsareokanditwilladderrorstoformifthereareany,sowhenyoudorender()method afteryouwillhaveformwitherrorsshowingifsomefieldhaserorrs.Okifformisnotvalidweshoulddefinesome variableinviewthatwillhaveformhtml:


1 . $ t h i s > v i e w ( ) > f o r m = $ f o r m > r e n d e r ( )

Nowinyourviewscriptyoushouldjusthave:
1 . e c h o $ t h i s > f o r m

Andyouhaveeverythingrunning NowIwillpresentyoulittlecomplexedformthathasalmostallneededelements:
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . p u b l i c f u n c t i o n t e s t ( ) { $ a c t i o n = $ t h i s > h o s t . ' t e s t ' $ f o r m = n e w D o o F o r m ( a r r a y ( ' m e t h o d ' = > ' p o s t ' , ' a c t i o n ' = > $ a c t i o n , ' e l e m e n t s ' = > a r r a y ( ' u s e r n a m e ' = > a r r a y ( ' t e x t ' , a r r a y ( ' r e q u i r e d ' = > t r u e , ' l a b e l ' = > ' M y u s e r n a m e : ' , ' a t t r i b u t e s ' = > a r r a y ( " s t y l e " = > ' b o r d e r : 1 p x s o l i d # 0 0 0 ' , ' c l a s s ' = > ' m i t a r ' ) , ' f i e l d w r a p p e r ' = > ' d i v ' , ' v a l i d a t o r s ' = > a r r a y ( a r r a y ( ' u s e r n a m e ' , 4 , 7 ) , a r r a y ( ' m a x l e n g t h ' , 6 , ' T h i s i s t o o l o n g ' ) , a r r a y ( ' m i n l e n g t h ' , 6 ) ) ) ) , ' p a s s w o r d ' = > a r r a y ( ' p a s s w o r d ' , a r r a y ( ' r e q u i r e d ' = > t r u e , ' l a b e l ' = > ' P a s s w o r d : ' , ' v a l i d a t o r s ' = > a r r a y ( a r r a y ( ' m i n l e n g t h ' , 6 ) ) ) ) , ' r e p e a t _ p a s s w o r d ' = > a r r a y ( ' p a s s w o r d ' , a r r a y ( ' r e q u i r e d ' = > t r u e , ' l a b e l ' = > ' R e p e a t p a s s w o r d : ' , ' v a l i d a t o r s ' = > a r r a y ( ' e q u a l a s ' , ' p o s t ' , ' p a s s w o r d ' , ' P a s s w o r d s m i s m a t c h ! ' ) ) ) , ' l o o k i n g _ t y p e ' = > a r r a y ( ' s e l e c t ' , a r r a y ( ' r e q u i r e d ' = > t r u e , ' m u l t i o p t i o n s ' = > a r r a y ( 1 = > ' g o o d l o o k i n g ' , 2 = > ' b e t t e r l o o k i n g ' , 3 = > ' u n f u c k a b l e ' ) , ' l a b e l ' = > ' M y l o o k : ' ) ) , ' l o o k i n g _ f o r ' = > a r r a y ( ' M u l t i C h e c k b o x ' , a r r a y ( ' r e q u i r e d ' = > f a l s e , ' m u l t i o p t i o n s ' = > a r r a y ( 0 = > ' l o v e ' , 1 = > ' h a t e ' , 3 = > ' o t h e r ' ) , ' l a b e l ' = > ' I a m l o o k i n g f o r : ' , ' v a l i d a t o r s ' = > a r r a y ( ' n o t n u l l ' ) ) ) , ' t e s t _ c h e c k ' = > a r r a y ( ' c h e c k b o x ' , a r r a y ( ' r e q u i r e d ' = > t r u e , ' l a b e l ' = > " I a g r e e w i t h c o n d i t i o n " . ' : ' , ' v a l i d a t o r s ' = > a r r a y ( a r r a y ( ' n o t n u l l ' , ' Y o u m u s t a g r e e w i t h t e r m s o f u s e ! ' ) ) ) ) , ' c a p t c h a ' = > a r r a y ( ' c a p t c h a ' , a r r a y ( ' r e q u i r e d ' = > f a l s e , ' d i r e c t o r y ' = > D o o : : c o n f ( ) > R O O T _ D I R . ' _ c a p t c h a ' , ' u r l ' = > $ t h i s > h o s t . ' _ c a p t c h a / ' , ' i m a g e ' = > D o o : : c o n f ( ) > R O O T _ D I R . ' g l o b a l / i m g / c a p t c h a _ b g . j p g ' , ' l a b e l ' = > ' E n t e r l e t t e r s f r o m p i c t u r e ' , ' m e s s a g e ' = > ' C a p t c h a i s n o t v a l i d ! ' ) ) , ' s u b m i t ' = > a r r a y ( ' s u b m i t ' , a r r a y ( ' l a b e l ' = > " R e g i s t e r " ) ) ) , ) ) $ d a t a [ ' c o n t e n t ' ] = ' t e s t ' i f ( $ t h i s > i s P o s t ( ) ) { i f ( $ f o r m > i s V a l i d ( $ _ P O S T ) ) { e c h o " F o r m a i s v a l i d ! " } e l s e {

6 4 . e c h o ' F o r m i s n o t v a l i d ! ' 6 5 . $ t h i s > v i e w ( ) > f o r m = $ f o r m > r e n d e r ( ) 6 6 . } 6 7 . } e l s e { 6 8 . $ t h i s > v i e w ( ) > f o r m = $ f o r m > r e n d e r ( ) 6 9 . } 7 0 . 7 1 . $ t h i s > r e n d e r c ( ' t e m p l a t e ' , $ d a t a , t r u e ) 7 2 . }

FirstthingthatisfunnyisinpasswordelementvalidatorequalAs,wellthatvalidatorshouldcheckifsomevalueis sameassomevaluefromgetorpostmethodso
1 . ' v a l i d a t o r s ' = > a r r a y ( ' e q u a l a s ' , ' p o s t ' , ' p a s s w o r d ' , ' P a s s w o r d s m i s m a t c h ! ' )

meansifthisvalueisequalaspasswordvaluefrompostmethoditsokifnotadderrorthatpasswordsmismatch! Nowletslookatthecaptchaelement,forthiselementyouneedtodolittlesetup,firstofallyouneeddirectorywhere tostorecaptchaimages,thenyouneedtodefineurlofcaptchadirectory,imageisbackgroundimageofcaptcha, everythingelseIthinkyouknowthatslabelandmessageyouwanttoshowwhencaptchaisnotgood. AnotherelementthatwillbeupdatedinDooFormclassisfileelement,IamsyncingitasItypethistutorial,sothis elementlookslikethis:


1 . ' p h o t o ' = > a r r a y ( ' f i l e ' , a r r a y ( 2 . ' r e q u i r e d ' = > t r u e , 3 . ' l a b e l ' = > ' S e l e c t p h o t o : ' , 4 . ' d e s t i n a t i o n ' = > $ t h i s > _ b a s e P a t h . ' s t a t i c / u s e r p h o t o / t m p ' , 5 . ' s i z e ' = > 2 0 4 8 0 0 0 , 6 . ' e x t e n s i o n ' = > ' j p g , p n g , g i f ' 7 . ) ) ,

Asyoucanseeyoucanaddsizeandextension.Sizeisthesizeoffileandextensionislistofextensionsyouwantto haveinyourfileform,allextensiontypesaredelimitedwith,withoutspace.Thatsaboutit,ifyouhaveany questionspleasepostcomments. NewinDooFormclass,wehavedisplaygroupsformakingdisplaygroupyousimplymakeformfirstthencall:


1 . $ f o r m > a d d D i s p l a y G r o u p ( " m y D i s p l a y G r o u p " , a r r a y ( " u s e r n a m e " , " p a s s w o r d " , " r e p e a t _ p a s s w o r d " ) )

Asyoucanseefirstargumentisnameofyourdisplaygroupandsecondargumentisarraywithnamesofyourfields, nowDooFormwillmakeinternaldisplaygroupthatcanbereturnedbycallingrenderDisplayGroupfunction,with renderfunctionyouwillbereturnedwholeform. SotogetHTMLforyourdisplaygroupjustcall:


1 . $ f o r m > r e n d e r D i s p l a y G r o u p ( " m y D i s p l a y G r o u p " )

AboutwrappersinnewDooFormyouhave: fieldwrapper(Wrapsbothelementandelementslabel) labelwrapper(Wrapsjustlabel) elementwrapper(Wrapsjustelement)

16Comments

PreviousPost NextPost

Leng 28Sep,2009 +2 FYI,


$ t h i s > v i e w ( ) > f o r m=$ f o r m > r e n d e r ( ) ; Search...

TheexamplegivenisusingnativePHPastemplate.

Random

Share

JoinChat

chucka 11Oct,2009 2 Checkhereformoreinformationonavailablevalidationoptions:http://doophp.com/documentation/api_svn/doo helper/DooValidator.html

Chuck 13Oct,2009

IseethistutorialusesthevariableDoo::conf()>ROOT_DIRforthecaptchaelement.Thatisnotavariablein common.conf.phpbydefault.Bythelooksofit,itpointstothebasedirectoryinwhichyourappsindex.phpis located.Shouldthe_captchafolderreallybelocatedthere?Woulditbebetterorganizedunderprotectedor global?Andifso,whichwouldbebest?

MilosKovacki 14Oct,2009

YesyouarerightvariableROOT_DIRissomevariablethatIassignedtodirectoryofmywebappand thereis_captchadirinitwhereIstorecaptchas. Youcanhaveitanywhereyouwant,Iwrotecaptchaelementlikethatsoyouwouldhavecaptchasstored

in_captchadir,inlightofsecuritythereisnowaythatsomeonecanguessmd5ofsomerandomstring wichisfilenameofthatcaptchapicture.

smitshah 10Nov,2009

hiigottheerror Fatalerror:ClassDooFormnotfoundin C:\wamp\www\emp\app\protected\controller\EmployeeController.phponline8

MilosKovacki 10Nov,2009 +2

IguessyoudidntdownloadlatesttrunksfromSVN,DooFormisnotimplementedinDooPhp1.2itwillbe implementedin1.3whenitcomesout. DownloadlatestDooPhpfromSVNathttp://code.google.com/p/doophp

Chuck 18Nov,2009

Usingthis,howwouldoneavoidduplicaterowsfromapagerefreshorbrowsernavigationusage?

img 26Dec,2009

thisisgoodtobeuseinbackend,admincotrolpanel

seralf 7Jan,2010 +1 Hi ivefoundthatitsimportanttocall: Doo::loadHelper(DooValidator) Doo::loadHelper(DooForm) intherightorder. InvertingthetwoloadwouldcaseaDooValidatorclassnotfounderror EditedbyMilos: YoudonthavetogetnewDooFormfromSVNnowDooFormloadshelperwhenisValidiscalled soyoudontneedtoloadvalidatorinyourcontrolleranymore.

Murray 25Feb,2010 +1 Yourcaptchaexamplehas: captcha=>array(captcha,array( required=>false, Doesthismeanthecaptchaisnotrequired,soifthisfieldisleftblanktheformcanstillvalidate?

MilosKovacki 26Feb,2010

Yes,soifcaptchaisblankitwillnotvalidateifitisnotblankitwillvalidate,butformshouldbevalidif thereisnocaptchaentered,itwasjustglitch,itshouldberequired=true

Patrick 9Aug,2010

Itwouldbeusefultobeabletosetaclassandidontheform.Thiscurrentlyseemstobehardcodedas class=dooform>.

MilosKovacki 9Aug,2010

Itsnothardcoded,youcanfullycustomizeeverything. Forexample:
' p a s s w o r d '= >a r r a y ( ' p a s s w o r d ' ,a r r a y ( ' a t t r i b u t e s '= >a r r a y ( ' c l a s s '= >' s o m e _ c l a s s ' ) , ' l a b e l '= >' P a s s w o r d :' , ' v a l i d a t o r s '= >a r r a y ( a r r a y ( ' m i n l e n g t h ' , 6 ) ) ) ) ,

Actuallyyouhavethisexampleonthestartoftutorial.

Patrick 9Aug,2010

HiMilosthanksforyouranswer! Imeansetclass&idfortheformthough:i.e. Isthatpossible? MycodegotswallowedImeanfor formaction=/contactmethod=postclass=notadooformid=exampleid

MilosKovacki 9Aug,2010 +2

Notrightnow,Iwillchangeitinfewdaystomakeformclasseditable,tnxfornotice.Itotalyforgotabout it.

ccc353 11Aug,2010

inmostcasesitwouldbefaster: functionisPost() { return(strtoupper($_SERVER['REQUEST_METHOD'])==POST) }


Search...

OurSponsors ChooseLanguage
English Russian Deutsch Espaol Arabic Portugese

PopularTags

FindTutorial
Easy Normal Difficult

Categories

Demos&Snippets(4) News&Updates(2) Screencast(1) Tutorials(17)

RecentComments
kukatonIntrotoDooPHPslides(PHPMalaysiameetup2011) MilosKovackionUsingDooTranslatorfortranslation WebdevGreeceonUsingDooTranslatorfortranslation MilosKovackionHandlingsessionswithoutApachesessions TonCreateasimpleToDoListinDooPHPPart2

Blogroll
DevelopmentBlog Documentation Forum GoogleCode Home About Contact Forum AllcontentscopyrightLearnDooPHP.Allrightsreserved.ThemedesignbyWebKreation.

You might also like