0
votes

I'm using CakePHP 1.3.10 with a MySQL database.

The field event_date has the type "timestamp" in MySQL. The great automagic form element helper gives me a nice dropdown menu.

echo $form->input('event_date'); 

The problem is, I noted that users could still input invalid, non-existing dates, like the 31st of June.

Okay, I thought ... let's crack out the validation rules. The problem is, the input form puts data in an array([month] => '31', [day] => '06', [year] => '2011', '[hour]' ... et cetera.

The default validation rules only seem to work on strings ... how can I validate in this case? I'd like to stay within the CakePHP framework, I'm still kinda new. I'm just looking for an additional rule to add to my already set 'notempty' rule:

 'event_date' => array(
        'rule' => 'notEmpty',
        'message' => 'Please enter a valid date for this event'
    ),

I've looked around in the CakePHP documentation, but can't find anything useful. Seems a bit weird that I'd have to go to a lot of trouble to validate the input of an automagically generated form field.

1
You will probably want to look into custom validation rules: book.cakephp.org/#!/view/1179/Custom-Validation-RulesMike
CakePHP has a built-in date data validation type? I haven't used it so I'm not sure if it will apply in your case. You could also experiment with the rangeRoss
@Ross: it's with the date validation type I'm having problems. The syntax only seems to accept strings for correct validation, and I have an array. I don't know how I would go about and String-ify it within the context of those data rules, and it seems to be a bit of a stretch...Jeroen Baert
The date validation rule should work fine, Cake should be intelligent enough to accept its own arrays.deceze♦
Well, I've tried the masks 'mdy', 'MDY'. None of them are validating correctly ...Jeroen Baert

1 Answers

0
votes

check

http://book.cakephp.org/view/1159/date

for date validation rules. this should work.

of course this will not check if June has 31 days on the fly, but should provide an error message, when this date does not exist.