简体   繁体   中英

how to keep form values after validations

I have this form:

<?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data')); ?>
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-form',
    'enableAjaxValidation'=>true,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'userName'); ?>
        <?php echo $form->textField($model,'userName',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
        <?php echo $form->error($model,'userName'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password',array('size'=>45,'maxlength'=>30, 'id'=>'in')); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'firstName', array('id'=>'in0')); ?>
        <?php echo $form->textField($model,'firstName',array('size'=>45,'maxlength'=>50, 'id'=>'in1')); ?>
        <?php echo $form->error($model,'firstName'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'lastName', array('id'=>'in2')); ?>
        <?php echo $form->textField($model,'lastName',array('size'=>45,'maxlength'=>50, 'id'=>'in3')); ?>
        <?php echo $form->error($model,'lastName'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'birthDate', array('id'=>'birth0')); ?>
        <?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
                        'name'=>'publishDate',
                        'attribute'=>'birthDate',
                        'model'=>$model,
                        'language'=>'en',
                        // additional javascript options for the date picker plugin
                        'options'=>array(
                            'showAnim'=>'fold',
                            'altFormat'=>'dd-mm-yy',
                            'dateFormat'=>'yy-mm-dd',
                        ),
                        'htmlOptions'=>array('id'=>'birth'),)); ?>
        <?php echo $form->error($model,'birthDate'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($model,'gender', array('id'=>'in4')); ?>
                <?php echo $form->radioButton($model,'gender',array('value'=>'0', 'id'=>'in5')) . '<b id=in6 style="height:20px; color: #000666; font-size: 12pt;
                                                                                                font-weight: bold;"> Male</b><br>'; ?>
                <?php echo $form->radioButton($model,'gender',array('value'=>'1', 'id'=>'in7')) . '<b id=in8 style="height:20px; color: #000666; font-size: 12pt;
                                                                                                font-weight: bold;"> Female</b>'; ?>
                <?php echo $form->error($model,'gender'); ?>
        </div>

    <div class="row">
        <?php echo $form->labelEx($model,'idCountry'); ?>
                <?php $country=Countries::model()->findAll();
                      $countryList=CHtml::listData($country, 'idCountry' , 'cntName');
                      $url = CController::createUrl('users/selectedcity');
                      echo CHtml::activeDropDownList($model,'idCountry', $countryList,
                      array('empty'=>'choose country','id' => 'idCountry','onchange' =>
                      "selectedcountry('{$url}', 'idCountry', '#name');",'style'=>'width:300px')); ?>
        <?php echo $form->error($model,'idCountry'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'idCity'); ?>
                <div id="name"></div>
                <?php echo $form->error($model,'idCity'); ?>
        </div>

    <div class="row">
        <?php echo $form->labelEx($model,'securityQuestion'); ?>
        <?php $q=  Securityquestions::model()->findAll();
                      $qs=CHtml::listData($q, 'idQuestion' , 'question');
                      echo CHtml::activeDropDownList($model,'securityQuestion', $qs,array('empty'=>'Select question', 'id'=>'sec')); ?>
        <?php echo $form->error($model,'securityQuestion'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'answer'); ?>
        <?php echo $form->textField($model,'answer',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
        <?php echo $form->error($model,'answer'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'email'); ?>
        <?php echo $form->textField($model,'email',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'facebookAcc'); ?>
        <?php echo $form->textField($model,'facebookAcc',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
        <?php echo $form->error($model,'facebookAcc'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'twitterAcc'); ?>
        <?php echo $form->textField($model,'twitterAcc',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
        <?php echo $form->error($model,'twitterAcc'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'profilePhoto'); ?>
                <?php echo CHtml::radioButton('profile',false,array('value'=>'0')) . 
                                            '<b style="height:20px; color: #000666;
                                            font-size: 12pt; font-weight: bold;">
                                            Defalut</b><br>'; ?>
                <?php echo CHtml::radioButton('profile',false,array('value'=>'1')) . 
                                            '<b style="height:20px; color: #000666;
                                            font-size: 12pt; font-weight: bold;">
                                            Upload from here :</b>'; ?>
        <?php echo CHtml::activeFileField($model,'profilePhoto',array('width'=>45)); ?>
        <?php echo $form->error($model,'profilePhoto'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'website'); ?>
        <?php echo $form->textField($model,'website',array('size'=>45,'maxlength'=>500, 'id'=>'in')); ?>
        <?php echo $form->error($model,'website'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'telephone'); ?>
        <?php echo $form->textField($model,'telephone',array('size'=>45,'maxlength'=>200, 'id'=>'in')); ?>
        <?php echo $form->error($model,'telephone'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('id'=>'but')); ?>
                <?php echo CHtml::resetButton('Reset', array('id'=>'but')); ?>
    </div>

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

</div><!-- form -->
<?php echo CHtml::endForm(); ?>

and this is the action that the form belongs to:

$model=new Users;
                $countries=Countries::model()->findAll();
        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        if(isset($_POST['Users']))
        {
                    $model->attributes=$_POST['Users'];
                    $model->userType='p';
                    $model->communityType='null';
                    $model->active=1;
                    $model->admin=0;
                    if($model->gender==0)
                        $model->gender='m';
                    else
                        $model->gender='f';
                    if(isset($_POST['profile'])){
                    if($_POST['profile']==0)
                        $model->profilePhoto='default.jpg';
                    elseif($_POST['profile']==1)
                        $model->profilePhoto=CUploadedFile::getInstance($model, 'profilePhoto');
                    }
                    if($model->save()){
                        $this->actionfiledir();
                        if(($_POST['profile'])==1)
                            $model->profilePhoto->saveAs($this->actionphotodir()."/".$model->profilePhoto);
                        Yii::app()->session['userId']=$model->idUser;
                        $this->redirect(array('view','id'=>$model->idUser));
                    }
                }

        $this->render('createp',array(
            'model'=>$model,'countries'=>$countries
        ));
            }
            else
                $this->redirect(array('site/index'));
    }

        public function actionCreatec()
    {
            if($this->actionlogged()==0||$this->actionisadmin()==1)
            {
        $model=new Users;
                $model2=new Communities; 
                $countries=Countries::model()->findAll();

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        if(isset($_POST['Users']))
        {

                    $model->attributes=$_POST['Users'];
                    $model->userType='c';
                    $model->gender='n';
                    $model->firstName='null';
                    $model->lastName='null';
                    $model->birthDate='null';
                    $model->active=1;
                    $model->admin=0;

                    if(isset($_POST['profile'])==0)
                        $model->profilePhoto='default.jpg';
                    elseif(isset($_POST['profile'])==1)
                        $model->profilePhoto=CUploadedFile::getInstance($model, 'profilePhoto');

                    if($model->save()){
                        $model2->idUser=$model->idUser;
                        $model2->admin=1;
                        $model2->save(false);
                        $this->actionfiledir();
                        if(isset($_POST['profile'])==1)
                            $model->profilePhoto->saveAs($this->actionphotodir()."/".$model->profilePhoto);
                        Yii::app()->session['userId']=$model->idUser;
                        $this->redirect(array('view','id'=>$model->idUser));
                    }
                }

        $this->render('createc',array(
            'model'=>$model,'countries'=>$countries
        ));

after submitting the form, if the the form have validation errors it will be returned to the user to re-fill and correct the errors, but the form didn't returned with its all values, so the selected radio button will returns not selected, and also the dropDownList of cities, so how to retain all values of the form if it not submitted? I hope that my question is clear.

Enable the validation on submit, if form has some error it will not submit the form.

  'clientOptions'=>array(
     'validateOnSubmit'=>true, // Required to perform AJAX validation on form submit
   ),

Add This value in

 $form=$this->beginWidget('CActiveForm', array(
      'id'=>'users-form',
     'enableAjaxValidation'=>true,
     'clientOptions'=>array(
         'validateOnSubmit'=>true, 
     ),
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM