1

我正在使用yii framework我的网站。我在模式框中有一个注册表单。如果我在未填写的情况下提交表单,则验证错误应在modal box不刷新的情况下显示。但现在它重定向到其他页面。如何validation errors within the modal box在同一页面中显示?

这是我用于注册视图的代码

<?php 
    $model=new SignupForm;

  $form=$this->beginWidget('CActiveForm', array(
    'id'=>'signup-form',
    'enableAjaxValidation'=>true,
    'action'=>'site/signup'
));
?>
<?php echo $form->errorSummary($model); ?> 
<?php echo $form->textField($model,'name',array('value'=>'Enter Your Name', 'onclick'=>'javascript:this.value=""', 'onblur'=> 'this.value = ( this.value == "" ) ? "Enter Your Name" : this.value;')); ?><br />
<?php echo $form->textField($model,'email',array('value'=>'Enter Your Email ID', 'onclick'=>'javascript:this.value=""', 'onblur'=> 'this.value = ( this.value == "" ) ? "Enter Your Email ID" : this.value;')); ?><br />
<?php echo $form->textField($model,'phone',array('value'=>'Telephone', 'onclick'=>'javascript:this.value=""', 'onblur'=> 'this.value = ( this.value == "" ) ? "Telephone" : this.value;')); ?><br />
    <!--<input type="text" value="username" onClick="this.value=''"/><br/>
    <input type="password" value="Password" onClick="this.value=''"/>    -->
  <div class="d-login"><?php echo CHtml::submitButton('Submit'); ?>
  <?php /*?><input type="image" alt="Login" title="Login" src="<?php echo Yii::app()->request->baseUrl; ?>/images/signup.png"/><?php */?>
  </div>
  <?php $this->endWidget(); ?>

控制器中的代码:

public function actionSignup()
    {
        $model=new SignupForm;

        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='signup-form')
        {
            $model->attributes=$_POST['SignupForm'];
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }


        // collect input data
        if(isset($_POST['SignupForm']))
        {
            $model->attributes=$_POST['SignupForm'];
            $name=$model->name;
            $email=$model->email;
            $phone=$model->phone;

            $newsletter = new Newsletter();


            if($model->validate())
            {

                //insert  data
                $newsletter->varName = $name;
                $newsletter->varEmail = $email;
                $newsletter->varPhone = $phone;
                if($newsletter->save()) {
                $url = Yii::app()->getBaseUrl();
                Yii::app()->getRequest()->redirect($url);
                    }

             } 
        }
        $this->render('signup',array('model'=>$model));

        }
4

2 回答 2

1

renderPartial如果要在对话框模式框中验证模型,则必须使用 ajax 。下面是一个未经测试的代码。(来自这个链接

在你的意见/signup.php

<div class="form">  
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'signup-form',
        'enableAjaxValidation'=>false,
    ));
    ?>
    <?php echo $form->errorSummary($model); ?> 
    <?php echo $form->textField($model,'name',array('value'=>'Enter Your Name', 'onclick'=>'javascript:this.value=""', 'onblur'=> 'this.value = ( this.value == "" ) ? "Enter Your Name" : this.value;')); ?><br />
    <?php echo $form->textField($model,'email',array('value'=>'Enter Your Email ID', 'onclick'=>'javascript:this.value=""', 'onblur'=> 'this.value = ( this.value == "" ) ? "Enter Your Email ID" : this.value;')); ?><br />
    <?php echo $form->textField($model,'phone',array('value'=>'Telephone', 'onclick'=>'javascript:this.value=""', 'onblur'=> 'this.value = ( this.value == "" ) ? "Telephone" : this.value;')); ?><br />
    <div class="d-login">
        <?php echo CHtml::submitButton('Submit'); ?>
    </div>
     <?php $this->endWidget(); ?>
</div>

在您的控制器中

public function actionSignup()
    {
        $model=new SignupForm;


        if(isset($_POST['SignupForm']))
        {
            $model->attributes=$_POST['SignupForm'];
            $name=$model->name;
            $email=$model->email;
            $phone=$model->phone;

            $newsletter = new Newsletter();
            if($model->save())
            {
                //insert  data
                $newsletter->varName = $name;
                $newsletter->varEmail = $email;
                $newsletter->varPhone = $phone;
                if($newsletter->save()) 
                {               

                    if (Yii::app()->request->isAjaxRequest)
                    {
                        echo CJSON::encode(array(
                            'status'=>'success',
                            ));
                        exit;               
                    }
                    else
                    {   
                        $url = Yii::app()->getBaseUrl();
                        Yii::app()->getRequest()->redirect($url);
                    }
            }
        }

        if (Yii::app()->request->isAjaxRequest)
        {
            echo CJSON::encode(array(
                'status'=>'failure', 
                'div'=>$this->renderPartial('signup', array('model'=>$model), true)));
            exit;               
        }
        else
            $this->render('signup',array('model'=>$model,));
    }

最后在您要显示注册模式框的视图文件中

<?php echo CHtml::link('Signup', "",  // the link for open the dialog modal
    array(
        'style'=>'cursor: pointer; text-decoration: underline;',
        'onclick'=>"{doSignup(); $('#dialogSignup').dialog('open');}"));?>

<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
    'id'=>'dialogSignup',
    'options'=>array(
        'title'=>'Signup',
        'autoOpen'=>false,
        'modal'=>true,
        'width'=>550,
        'height'=>470,
    ),
));?>
<div class="divForForm"></div>

<?php $this->endWidget();?>

<script type="text/javascript">
function doSignup()
{
    <?php echo CHtml::ajax(array(
            'url'=>array('site/signup'),
            'data'=> "js:$(this).serialize()",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
                if (data.status == 'failure')
                {
                    $('#dialogSignup div.divForForm').html(data.div);
                    $('#dialogSignup div.divForForm form').submit(doSignup);
                }
                else
                {
                    window.location.href =".Yii::app()->getBaseUrl().";
                }

            } ",
            ))?>;
    return false; 

}

</script>
于 2012-05-06T18:53:40.240 回答
0

您应该在注册模型中定义验证规则。或者在此处粘贴您的注册模型。

于 2012-05-08T06:39:44.670 回答