views:

45

answers:

2

How can I get the built-in date formatting working in my Cake app? Maybe I am making some simple mistake. I'm focused on the model code right now, I think this is what I am screwing up.

On the page it looks like it's working, showing three select widgets with months, days, years in that order. However, when I submit the form I'm getting the "Must be a valid date" message.

Here's the view code I have:

echo $this->Form->create('Subscription');
echo $this->Form->input('starts',array('type'=>'date','dateFormat'=>'MDY'));
echo $this->Form->end('Submit', true);

And in my model the validation looks like this:

'starts' => array(
    'date' => array(
    'rule' => array('date', array('MDY')),
    'message' => 'Must be a valid date',
    ),
    'notempty' => array(
        'rule' => array('notempty'),
        'message' => 'Start date is required',
    ),
),

The field I'm trying to update is declared as DATETIME in the mysql db, in case that makes a difference.

+1  A: 

I don't remember off the top of my head, but I think you have to use the generated date inputs to construct your own date.

!!- Scrub that -!!

‘Mdy’ e.g. December 27, 2006 or Dec 27, 2006 (comma is optional)

If no keys are supplied, the default key that will be used is ‘ymd’.

I'm with SpawnCxy on this.

Leo
ok i might give that a try. one thing i noticed is the date is being updated correctly if i don't validate it, which makes me think that you wouldn't need to build-up the date string yourself. plus that seems like more work than you'd have to do. cake is so nice like that.
the0ther
A: 

I don't find the parameter MDY in the parameter list.You can try Mdy.

SpawnCxy
"MDY" is used in the "dateFormat" parameter for the view, but the validation looks like it wants "Mdy".
the0ther
I mean the validation ,not the form creating part.
SpawnCxy