0

What's the standard way to get rid of the three <select> elements and allow users to just type dates in a regular <input type="text"> control?

Requirements include:

  • Date format must be D/M/Y
  • Existing dates must be printed correctly
  • Cannot break date validation

I cannot find any reasonable documentation on this, just hacks in forum threads written by users as clueless as me xD

Clarification: Please note the CakePHP tag. I already know how to handle dates in regular PHP. I need help about the precise CakePHP mechanism I can use to adjust the framework's default functionality (and I really mean adjust rather than override).

So far, I've added this to the model:

public $validate = array(
    'fecha' => array(
        array(
            'rule' => 'notEmpty',
            'required' => true,
        ),
        array(
            'rule' => array('date', 'dmy'),
        ),
    )
);

... and I've composed the field like this inside the view:

echo $this->Form->input(
    'Foo.fecha',
    array(
        'type' => 'text',
    )
);

... but all I can do with this is reading and validating user input: it won't print previous date properly and it won't store new date properly.

JadedCore
  • 1,993
  • 1
  • 13
  • 20
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • You can create the form by yourself and not using the cakephp 'auto form generator' – Ofir Baruch Aug 24 '12 at 11:58
  • That defeats the concept of using a framework, doesn't it? – Álvaro González Aug 24 '12 at 12:00
  • The framework is not "flexible" , i blame the framework :) – Ofir Baruch Aug 24 '12 at 12:02
  • @OfirBaruch - The framework is extremely flexible but you need to know how to use it. The reason he is probably getting the multiple select boxes is due to the table in the DB being set to DATETIME. To get a regular text box - `$this->Form->input('my_field_name', array('type'=>'text'));` Combined with @Cupidvogel suggestion of a jquery datepicker and [cakephp's builtin field validator](http://book.cakephp.org/1.3/en/view/1152/Core-Validation-Rules#date-1159) he should have his bases covered. J/K - See your comment is outdated. – styks Aug 25 '12 at 21:29

3 Answers3

2

Here's a summary of my findings. It seems that the appropriate mechanism is using Model Callback Methods to switch between two date formats:

  • Database format, e.g.: 2012-08-28
  • Display format, e.g.: 28/08/2012

Steps:

  1. Add two utility methods to AppModel to convert between my custom format (aka "display format") and MySQL's default format (aka "DB format").
  2. Add an afterFind() filter to my model that converts to display format when read from DB.
  3. Render the form control as 'type' => 'text'.
  4. Add a 'rule' => array('date', 'dmy') validation rule to the field inside the model.
  5. Add a beforeSave() filter to my model that converts to DB format right before saving.

These steps can be encapsulated with a behaviour that implements the afterFind() and beforeSave() callbacks and possibly some others like beforeFind(). The behaviour can be applied directly to AppModel and will take care of traversing data arrays to convert dates between both formats (if the model has an attached table). The code is not trivial but can be done and it makes it all transparent.

Drawbacks:

  • Makes code MySQL-only (but, isn't it MySQL-only already?)
  • Makes it difficult to localize.
  • If you need to do date math, you find yourself with human-readable strings.

It would be more rock-solid to be able to use three formats:

  • Database format, e.g.: 2012-08-28
  • Display format, e.g.: 28/08/2012
  • PHP internal format, e.g. Unix timestamps or DateTime objects

Sadly, the CakePHP core is not designed for that and it all starts getting too complicate if you attemp to implement it this way.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

The only possible way looks like letting the user typing whatever he wants do, then checking for its validity when he wants to submit the form or something. Or better, go for jQuery datepicker.

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • yeah, jQuery datepicker will be very handy in this case – nik Aug 24 '12 at 12:07
  • Care to explain *how* to do the first part? (jQuery UI Datepicker is just GUI candy, you still need a form control with the date in the appropriate format). – Álvaro González Aug 24 '12 at 12:07
  • It will be a tad complicated. First you can use a regex like `\d{2}\/\d{2}\/\d{4}` to look for syntax checking, if it passes, then a series of `if-elseif-else` checks. If the year is a leap year, then if the month is `02`, the day part must not be greater than 29, blah blah blah. – SexyBeast Aug 24 '12 at 12:10
  • datePicker have [dateformat](http://jqueryui.com/demos/datepicker/#date-formats) to deal with it – nik Aug 24 '12 at 12:17
  • Of course it has. He asked me about how to do it through the text input method, so I was explaining that. – SexyBeast Aug 24 '12 at 12:18
  • Oh, then validations can be helpful, :) – nik Aug 24 '12 at 12:20
  • Thank you for your time, but I'd dare say you aren't really a CakePHP user and you're approaching the question from a regular PHP point of view. – Álvaro González Aug 28 '12 at 11:15
0

In case you'll write the input fields using the helper, There's a type option for the input method of formhelper. Try this , for instance:

<?php
echo $this->Form->input('birth_dt', array(
    'type'  => 'text',
    'label' => 'Date of birth',
));

EDIT: (after reading your comment)

The solution if so , can be to validate the input value after the submit of the form or using ajax vaildation.

Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39