Stupid Easy Cakephp Datepicker (without a massive library)
I didn't like CakePHP's 3/6 textboxes for date and datetime. I needed a simple and easy datepicker widget (a la jQuery) without installing any number of plugins, components, or helpers. This is what I did.
1. Get jQuery and jQuery UI's datepicker widget.
2. Setup your view to use a text input rather than a select. Then start a jQuery datepicker on that field.
<?php echo $this->Form->input('Mymodel.date', array('type'=>'text')); ?>
<script type="text/javascript">$("#MymodelDate").datepicker();</script>
3. Change the model to modify the date format on save with a beforeSave method.
function beforeSave($options) {
if(isset($this->data['Application']['date']))
$this->data['Application']['date'] = date("Y-m-d", strtotime($this->data['Application']['date']));
return true;
}
4. If necessary, build a similar afterFind method so that the edit view shows the currently selected date.