Reputation: 494
I have a problem with validating rules for dates, when I enter invalid date like 'xxxx', I dont get the validator running, but it runs for other fields with required validator.
the form
?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'post-form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
)); ?>
....
<div class="row">
<?php echo $form->labelEx($model,'fromDate'); ?>
<?php echo $form->textField($model,'fromDate'); ?>
<?php echo $form->error($model,'fromDate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'toDate'); ?>
<?php echo $form->textField($model,'toDate'); ?>
<?php echo $form->error($model,'toDate'); ?>
</div>
the rules in the post model
public function rules()
{
return array(
array('fromDate, toDate', 'date', 'format'=>'dd-mm-yyyy H:m:s', 'allowEmpty'=>false),
array('subject', 'required'),
);
...
}
Thank you in advance for your help
Upvotes: 5
Views: 6427
Reputation: 1053
The Validation rules are explained inside CDateTimeParse.php
* Pattern | Description
* ----------------------------------------------------
* d | Day of month 1 to 31, no padding
* dd | Day of month 01 to 31, zero leading
* M | Month digit 1 to 12, no padding
* MM | Month digit 01 to 12, zero leading
* MMM | Abbreviation representation of month (available since 1.1.11; locale aware since 1.1.13)
* MMMM | Full name representation (available since 1.1.13; locale aware)
* y | 4 year digit, e.g., 2005 (available since 1.1.16)
* yy | 2 year digit, e.g., 96, 05
* yyyy | 4 year digit, e.g., 2005
* h | Hour in 0 to 12, no padding
* hh | Hour in 00 to 12, zero leading
* H | Hour in 0 to 23, no padding
* HH | Hour in 00 to 23, zero leading
* m | Minutes in 0 to 59, no padding
* mm | Minutes in 00 to 59, zero leading
* s | Seconds in 0 to 59, no padding
* ss | Seconds in 00 to 59, zero leading
* a | AM or PM, case-insensitive (since version 1.1.5)
* ? | matches any character (wildcard) (since version 1.1.11)
* For example, to parse a date string '21/10/2008', use the following:
* $timestamp=CDateTimeParser::parse('21/10/2008','dd/MM/yyyy');
Upvotes: 1
Reputation: 1697
I don't find Yii's CDateValidator particularly flexible, and I usually go down the route of creating a custom validation rule:
Add this to your Model:
public function isValidDate($attribute, $params)
{
if(!strtotime($this->$attribute))
{
$this->addError($attribute, $attribute . ' was not a valid date');
}
}
Then assign the custom validator to your attribute in the rules() array:
array('fromDate, toDate', 'isValidDate'),
You could expand on that to make sure the dates are within a reasonable time frame, and that the toDate
is after fromDate
etc.
Upvotes: 4
Reputation: 187
You can use Yii widget
<div class="row">
<?php
echo $form->labelEx($model, 'fromDate');
$this->widget('CJuiDateTimePicker', array(
'model' => $model, //Model object
'attribute' => 'fromDate', //attribute name
'mode' => 'date', //use "time","date" or "datetime" (default)
'options' => array(
'dateFormat' => "dd/mm/yy",
'defaultDate' => "new Date()",
), // jquery plugin options
'htmlOptions' => array(
'id' => 'fromDate',
),
));
?>
</div>
and in action :
if ($model->fromDate != '') {
$temp = strtotime(str_replace('/', '-', $model->fromDate));
$temp = date('Y-m-d', $temp);
$model->fromDate = $temp;
}
Upvotes: 1