-
Notifications
You must be signed in to change notification settings - Fork 7.6k
FormDate
Category:Library Category:Library::Date Category:Library::Form
FormDate is a class that creates select and option tags for date and time elements. It relies on the form helpers form_dropdown function. The [url=http://codeigniter.com/forums/viewthread/45356/]forum thread[/url] where it all started.
[h3]Installation[/h3]
• Code Igniter version 1.5 or newer required. • [url=http://lab.rotsen.be/php/ci/formdate016.zip]Download & unzip formdate[/url]. • Put the formdate.php in your apps libraries folder. Create a libraries folder if it doesn’t exist.
[h3]Example[/h3]
Load and instanciate in controller [code]$this->load->library('formdate'); $formdate = new FormDate();[/code]
Set options [code]$formdate->setLocale('nl_BE'); $formdate->year['start'] = 1950; $formdate->year['end'] = 2006; $formdate->month['values'] = 'numbers';[/code]
Make formdate available in the view [code]$data['formdate'] = $formdate; $this->load->view('index', $data);[/code]
Display in view [code] <form> Name <input type="text" />
Day of Birth
<?=$formdate->selectDay()?>
<?=$formdate->selectMonth()?>
<?=$formdate->selectYear()?>
<input type='button' /> </form> [/code]
[h3]Date Validation[/h3]
Date validations are easy to do with php's built in [url=http://be2.php.net/checkdate/]checkdate()[/url]. Put this function in a validation callback, feed it with the month, day and year from your POST array and it will return bool true if valid.
[code] function valid_date() { if (!checkdate($this->input->post('month'), $this->input->post('day'), $this->input->post('year'))) { $this->validation->set_message('valid_date', 'The %s field is invalid.'); return FALSE; } } [/code]
[h3]More than one FormDate field per page[/h3]
To add more than one date field per page you can use the configuration variable prefix to name them differently.
[code] $startdate= new FormDate(); $startdate->config['prefix']="start_"; ... $enddate= new FormDate(); $enddate->config['prefix']="end_"; .. [/code]
Would create date fields as: start_year, start_month, start_day and end_year, end_month, end_day
[h3]Release History[/h3]
Version 0.10 (14 June 2006)
• Initial test release
Version 0.11 (17 June 2006)
• Added config prefix
Version 0.12 (26 October 2006)
• Fixed hour, minute and second selection bugs (reported by Maciej Kus) • Converted to a library since scripts are deprecated since CI 1.4.
Version 0.13 (3 April 2007)
• Added day argument value of 1 to mktime() function in selectMonth() method. This fixes wrong double month option elements. • Added config option to display blank value for day, month and year select elements.
Version 0.15 (4 April 2007)
• Blank value is always first element in dropdown list. • Auto select blank element instead of current date when blank option setted. • Added blank value for hour, minute, second and meridian • phpDocumentor style comments • Deprecated init file.
Version 0.16 (4 May 2007)
• Removed default values for referenced arguments because they are not supported under PHP4.