我在Yii框架中遇到了表单验证问题。
这是我的 VIEW 代码:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'search-form',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'focus' => array($model, 'ccc'),
'clientOptions' => array(
'validateOnSubmit' => true,
),
));
?>
<?php
echo $form->errorSummary($model);
?>
<div class="row">
<?php echo $form->labelEx($model, 'input'); ?>
<?php echo $form->textField($model, 'input', array('class' => 'input-medium', 'maxlength' => 11,)); ?>
<?php echo $form->error($model, 'input'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
?>
<?php echo $form->error($model, 'date'); ?>
</div>
<?php $this->endWidget(); ?>
没什么特别。但是对于textField,验证消息仅(Ajax请求仅使用onChange textField发送)。
如何启用CJuiDatePicker验证消息?
答案 0 :(得分:3)
您只需要为 CJuidatepicker 对象提供正确的ID,使用CHtml::getIdByName
创建id值,尝试使用html元素的名称,它必须是类似的
'id' => CHtml::getIdByName(get_class($model) . '[' . $attribute . ']')
它会变成这样:
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id' => CHtml::getIdByName(get_class($model) . '[date]'),
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
答案 1 :(得分:1)
wonde的答案“你应该包含一个值”对我来说不起作用......
这是有用的:
Yii CActiveForm date validation
views / site / login.php原来有:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
当改变为:
时,事情有效$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
),
请参阅: http://sky-walker.net/temp/test/yii/testdate/index.php?r=site/login
答案 2 :(得分:0)
这就是我所做的并且它有效,我认为你应该包括 value
<?php echo $form->labelEx($model,'reportDate'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array( 'model'=>$model,
'attribute'=>'reportDate',
**'value'=>$model->reportDate,**
'options'=>array(
'showButtonPanel'=>true,
'changeYear'=>true,
'changeMonth'=>true,
'autoSize'=>true,
'dateFormat'=>'yy-mm-dd',
'defaultDate'=>$model->reportDate,
),
));
?>
<?php echo $form->error($model,'reportDate'); ?>
答案 3 :(得分:0)
只需在 CJuiDatePicker 之后添加$form->error($model,'end_date')
即可解决此问题。
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id' => CHtml::getIdByName(get_class($model) . '[date]'),
'attribute' => 'date',
'name' => 'date',
'model' => $model,
'language' => 'ru',
'options' => array(
'dateFormat' => 'dd/mm/y',
'showAnim' => 'slideDown',
'changeMonth' => true,
'changeYear' => true,
'showOn' => 'button',
'constrainInput' => 'true',
),
'htmlOptions' => array(
'style' => 'height:15px; width:6em'
),
));
echo $form->error($model,'dateTo');// added to enaple clint validation