Professional Documents
Culture Documents
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 ) ) ) )
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 . }
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 ! ' )
16Comments
PreviousPost NextPost
TheexamplegivenisusingnativePHPastemplate.
Random
Share
JoinChat
Chuck 13Oct,2009
MilosKovacki 14Oct,2009
in_captchadir,inlightofsecuritythereisnowaythatsomeonecanguessmd5ofsomerandomstring wichisfilenameofthatcaptchapicture.
smitshah 10Nov,2009
MilosKovacki 10Nov,2009 +2
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.
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
MilosKovacki 9Aug,2010 +2
Notrightnow,Iwillchangeitinfewdaystomakeformclasseditable,tnxfornotice.Itotalyforgotabout it.
ccc353 11Aug,2010
OurSponsors ChooseLanguage
English Russian Deutsch Espaol Arabic Portugese
PopularTags
FindTutorial
Easy Normal Difficult
Categories
RecentComments
kukatonIntrotoDooPHPslides(PHPMalaysiameetup2011) MilosKovackionUsingDooTranslatorfortranslation WebdevGreeceonUsingDooTranslatorfortranslation MilosKovackionHandlingsessionswithoutApachesessions TonCreateasimpleToDoListinDooPHPPart2
Blogroll
DevelopmentBlog Documentation Forum GoogleCode Home About Contact Forum AllcontentscopyrightLearnDooPHP.Allrightsreserved.ThemedesignbyWebKreation.