【问题标题】:The dialog can't pop up when the date validation is invalid日期验证无效时无法弹出对话框
【发布时间】:2013-03-27 03:44:15
【问题描述】:

我是一个新的 Yii 用户,当我想建一个网站时遇到问题。 我的网站有一部分需要用户输入开始日期和结束日期,我想要一个功能,如果结束日期设置在开始日期之前,它会显示一个对话框自动警告用户,但我不能那样做。有什么想法吗?

这是我的两个日期文本字段的代码:

<td>Start
    <?php 

            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'name'=>'COURSE_START_DATE',
            'model'=>$Cmodel,
            'attribute'=>'COURSE_START_DATE',
            'language'=>Yii::app()->language=='en_us',

            'options'=>array(
                'showAnim'=>'fold', // 'show' (the default), 'slideDown', 'fadeIn', 'fold'
                'showOn'=>'button', // 'focus', 'button', 'both'
                'buttonText'=>Yii::t('ui','Calendar'),
                'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.png',
                'buttonImageOnly'=>true,
            ),
    )
        ?></td>

     <td>End 
    <?php 
        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'name'=>'COURSE_END_DATE',
            'model'=>$Cmodel,
            'attribute'=>'COURSE_END_DATE',
            'language'=>Yii::app()->language=='en_us',
            'options'=>array(
                'showAnim'=>'fold', // 'show' (the default), 'slideDown', 'fadeIn', 'fold'
                'showOn'=>'button', // 'focus', 'button', 'both'
                'buttonText'=>Yii::t('ui','Calendar'),
                'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.png',
                'buttonImageOnly'=>true,

            ),
        ));

        ?>

【问题讨论】:

    标签: php ajax validation yii


    【解决方案1】:

    如果你真的是 yii 新手,你必须学习很多东西:

    1.如何在 yii 中进行 ajax 和客户端验证:

    在您的表单中,您需要指出可能会执行 ajax 和客户端验证。这是一个例子:

    <?php $form=$this->beginWidget('CActiveForm', array(
      'id'=>'contacts-form',
      'enableAjaxValidation'=>true, // this turns on AJAX validation
      'enableClientValidation'=>true, // this turns on Client validation
    )); ?>
    

    2。在控制器中启用 ajax 验证

    例如在你的控制器动作调用中:

    $this->performAjaxValidation($model);
    

    与:

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='contacts-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
        }
    }
    

    3.在模型中创建验证规则

    然后您需要创建将比较您的两个值的验证规则。为此,您可以使用 CCompareValidator 中的构建,例如将默认运算符更改为“>”。 所以在你的模型中,你会有类似的东西:

    public function rules()
    {
        return array(
            //your other rules
            array('COURSE_END_DATE', 'compare', 'compareAttribute'=>'COURSE_START_DATE', 'operator' => '>'),
        );
    }
    

    【讨论】:

    • 如果我想使用 CJuiDialog 来做我想做的事情,我该怎么做?感谢您的友好回答,并为我的英语不好感到抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多