2012-05-06 3 views
1

내 웹 사이트에 yii framework을 사용하고 있습니다. 모달 상자에 가입 양식이 있습니다. 작성하지 않고 양식을 제출하면 유효성 검사 오류가 새로 고치지 않고 modal box 내에 표시됩니다. 하지만 이제 다른 페이지로 리디렉션됩니다. 동일한 페이지에 validation errors within the modal box을 표시하려면 어떻게해야합니까?yii 모달 상자 양식 유효성 확인

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)); 

     } 

답변

1
당신은 아약스를 사용해야

renderPartial 당신이 내부 모델을 확인하려면 :

내가 가입보기

<?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(); ?> 

컨트롤러의 코드에 사용한 코드입니다 대화 상자 모달 상자. 아래의 테스트되지 않은 코드입니다. 귀하의 의견에

(이 link에서)

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,)); 
    } 

그리고 최종적으로보기 파일에 표시 할 컨트롤러에서/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> 

가입 모달 상자

<?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> 
0

가입 모델에서 유효성 검사 규칙을 정의해야합니다. 또는 여기에 가입 모델을 붙여 넣으세요.