Jonas Kvist Jensen
Jonas Kvist Jensen

Reputation: 139

Cakephp 3.x - datetime input as text for datetimepicker

This has been bugging me for a while now. I would like to use a Javascript datetimepicker, instead of the default set of select boxes that the CakePHP Formhelper generates. The easy solution would seem to be:

echo $this->Form->input('start_time', ['type' => 'text', 'class'=>'datetime']);

But this introduces some other problems as Cake will then convert the values into the locale format. I would also prefer a solution that worked from a general perspective, so I looked into changing the template for the datetime input:

//AppView.php
class AppView extends View
{
public function initialize()
{
    $this->Form->templates(['dateWidget' => '<input type="text" class="datetime" name="{{name}}"{{attrs}}/>']); 
}

}

This, however is as close as I've gotten. What happens is that the Formhelper outputs a nice text input, with an empty name attribute, which defeats the purpose.

I assume this is because the datetimeinput is basically wired to output the select fields, and is thus confused by my approach here.

But what would be the right way to do it?

Upvotes: 2

Views: 4951

Answers (1)

rayhan
rayhan

Reputation: 656

I have temporarily solved this problem by this line:

echo $this->Form->input('date', ['class' => 'datepicker-input', 'type' => 'text', 'format' => 'Y-m-d', 'default' => date('Y-m-d'), 'value' => !empty($item->date) ? $item->date->format('Y-m-d') : date('Y-m-d')]);

This will display a text field, add my datepicker-input class to the field. Show default date value in y-m-d format so that date picker can instantly read the current date.

This way it also save this data in database without any further issue.

Upvotes: 1

Related Questions