CakePHP Date Validation using date array format
Greetings,
In my view I have a date input, set like so:
echo $form->input('cc_expdate', array('label' => __('exp. date', true),
'type' => 'date', 'dateFormat' => 'MY', 'default' => date('Y'),
'minYear' => date('Y'), 'maxYear' => date('Y', strtotime("+10 years")),
'orderYear' => 'asc', 'separator' => ' ', 'monthNames' => false));
And in my model I have a validation rule, viz:
'cc_expdate' => array(
'rule' => array('date', 'my'),
'message' => 'Please select a valid expiration date'
)
But the validation always fails. When I look at $this->data
, I see that the date is being passed through the form as an array (e.g. 'cc_expdate' => array('month' => '10', 'year' => '2010')
) but it seems the d开发者_运维百科ate validation rule expects a string like 10-2010
. I hunted around Cake and checked the documentation but could find nothing covering this. It seems this should be a simple task to handle, am I missing something simple or does Cake not have a built in validation for 'date' form elements?
cheers,
-BriBest solution / hack is to add an extra hidden field representing the day. In this case, you'll send your form as Cake expects to be: array('day' => 1, 'month' => 12, 'year' => 1980) and now you have a normal date validation in your model and no problems of saving a corrupted date value in your database.
Example:
View:
<?php echo $this->Form->input('YourModel.cc_expdate.day', array('type' => 'hidden', 'value' => 1));
echo $this->Form->input('cc_expdate', array('label' => __('exp. date'),
'type' => 'date', 'dateFormat' => 'MY', 'empty' => true,
'minYear' => date('Y'), 'maxYear' => date('Y') + 10,
'orderYear' => 'asc', 'separator' => ' ', 'monthNames' => false));
?>
Model:
'cc_expdate' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Invalid date',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
I had the same problem in a controller method recently where I was trying to save a month/year value into a MySQL date field and validation kept failing. I was using my/MY in the model/view for the date settings and it was passing the year and month as expected to $this->data when I checked it in the controller.
What I did was to set the day key to "1" in the date array passed from the form before validation in the controller, and then validation worked. This resulted in all my month/year dates being stored as the 1st of the month, which was fine for my app.
I'm still new to CakePHP, so this might be considered a hack, I'm not sure. My date field in the database was a MySQL date field, so I was wondering if maybe it was failing validation for some reason due to the day being missing (maybe using varchar would save the month/year as a string?).
If you are using Cake's month and year form fields, in order for the validation to work correctly, you must use the following notation:
Instead of .... 'column_name' for your rules, use 'column_name.month' and column_name.year.
精彩评论