Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Date Input Using Cakephp Form Helper
Posted on 09/09/2009 at 03:29 pm by Kevin Wentworth
Viewed 32,830 times | 3 comments
I always forget what exactly needs to happen in order for CakePHP's form helper to work with date input fields. I'm sick of tearing through old source code and looking at the official CakePHP docementation site. Here's how to get it to work...
Date Inputs Using the Form Helper:
- $max_year = $current_year + 2;
- echo $form->input('dateSelectBoxes', array('type'=>'date', 'selected'=>$unix_timestamp, 'empty'=>true, 'minYear'=>2009, 'maxYear'=>$max_year));
It's pretty simple, but just different enough from the other form helper inputs that I'm always forgetting the correct syntax. To recap:
- type - In order to only have the date show (and not the date AND time) specify a type of date.
- selected - To set the value of the select boxes, use the selected key (NOT default)
- The value for 'selected' MUST BE a unix timestamp (at least I couldn't get it to work with a mm/dd/YYYY format as of 1.2.3.8166)
- empty - Set empty in order to have empty options at the top of the lists
- minYear - Defines the year that you want as the minimum
- maxYear - Define the year that you want to be the maximum (I like to calculate it based on the current year, see above)
In case you are curious as to why the dates are listed in descending order, it's a bug/feature request. If you want to make the change now, to your CakePHP core, just follow the directions and add the following to your form helper file:
Cheers,
-Kevin Wentworth
Tags for Date Input Using Cakephp Form Helper
Cakephp | Web Programming | Usage | Forms
Comments for this Posting
Posted by M Danish
on 29/9/10
THANK YOU VERY MUCH FOR YOUR GREATEST HELP,i was really stuck in this small thing...thats gr8
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!
Posted by Farhan
on 8/5/10
Thanks for sharing.
'selected'=>$unix_timestamp,
was helpfull a lot, I googled for an hour, but could not find a solution for it. Cake documentation also did not state anywhere about this.
Thanks for the help