用于客户端验证的cactiveform错误管理

时间:2012-08-19 16:35:18

标签: php yii

我的表单中有一个申请人计数下拉列表。当我从下拉列表中选择申请人数量时,我需要输入那么多申请人的名字,姓氏等。当我点击提交按钮时,我需要显示客户端验证。如何实现这一点。

我创建了cactiveform小部件。

我的控制器看起来像

class PolicyFamilyDetailsController extends Controller
{
 public function actionIndex()
{
    $PolicyFamilyDetails=new PolicyFamilyDetails;
    $PolicyFamilyApplicant=new PolicyFamilyApplicant;
    $this->performAjaxValidation($PolicyFamilyDetails);
    if(isset($_POST['continue']))
    {
        print_r($_POST['PolicyFamilyDetails']);
        exit;

    }
    Yii::app()->session['policy_id']=2;
    $data['policy_id']=Yii::app()->session['policy_id'];
    $data['PolicyFamilyDetails']=$PolicyFamilyDetails;
    $data['PolicyFamilyApplicant']=$PolicyFamilyApplicant;
    $this->render('index',$data);
}
public function actionapplicantList()
{
    $data=array();
    $PolicyFamilyDetails=new PolicyFamilyDetails;
    $data['PolicyFamilyDetails']=$PolicyFamilyDetails;
    $data['applicant_list']=$_POST['applicant_list'];
    $data['applicant_count']=$_POST['applicant_count'];
    $this->renderPartial('applicant_list',$data,false,true);
    //$this->render('applicant_list',$data);
}

}

模型是:

class PolicyFamilyDetails extends CActiveRecord
{
public function tableName()
{
    return 'cis_policy_family_details';
}
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('policy_id, first_name, last_name, dob, gender, height, height_scale, weight, weight_scale, occupation, nationality, passport_no, main_applicant', 'required'),
        );
}
}

观点是  的index.php

<script type="text/javascript">
$(document).ready(function() {



});

function closeError() {
hid_remove_count=$('#hid_remove_count').val();
if ($('input:checked').length==hid_remove_count)
{
    $('input:checked').each(function(){
        $('#applicant_'+$(this).attr('id')).remove();
    });
    $('#errorBlank_applicant_list').hide();
    $('#errorBox_applicant_list').hide();
}
else
    alert('Please choose '+hid_remove_count+' applicant');
}

</script>


<?php 

$form=$this->beginWidget('CActiveForm', array(
'id'=>'policy-registration-form',
'stateful'=>true,
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
/*'focus'=>array($model,'startDate'),*/
'clientOptions'=>array(
    'validateOnSubmit'=>true,
),
)); 
Yii::app()->session['form'] = $form;
//print_r($form);
?>
<div id="innerContainer">
    <h2>Step 3 - Family Details</h2>
    <p>Details about you and your family applying for cover:</p>
     <div id="infoContainer">
        <div class="item">

        <label class="small" for="noapplicants">Number of applicants</label>
<?php
 $static =   array('1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,'10'=>10,);
 $ajax_code = <<<LABEL
 if ($(this).val()=='')
{
  $('.applicant_list').html('');
  return false;
}
var applicant_count=parseInt($(this).val());
var applicant_list=parseInt($('.applicants').length);

if (applicant_count<=applicant_list)
{
//alert(applicant_list);
applicant_check_list='<p>Please choose any '+(applicant_list-applicant_count)+' applicants that you want to remove.</p>';
applicant_check_list+='<input type="hidden" id="hid_remove_count" value="'+(applicant_list-applicant_count)+'"/>';
for (var i=2; i<=applicant_list; i++)
    applicant_check_list+='<input type="checkbox" id="'+i+'" class="check_list"/>Appilcant '+i+'<br/>';
$(".applicant_check_list").html(applicant_check_list);
$("#errorBlank_applicant_list").show();
$("#errorBox_applicant_list").show();
return false;
}
LABEL;

echo Yii::app()->session['form']->dropDownList(
              $PolicyFamilyApplicant,
              'applicants_count',
              $static ,
              array('empty'=>Yii::t('fim','Please Select'),
              'onchange'=>$ajax_code.
                        CHtml::ajax(array(
                          'type'=>'POST',
                            'url'=>$this->createUrl("policyFamilyDetails/applicantList"),
                          'data'=>array('applicant_count'=>'js:this.value',
                                        'applicant_list'=>'js:$(".applicants").length',
                                           ),  
                          'success'=>'function(data) { $(".applicant_list").append(data) }'
                          )),

 ));
 ?>


       <?php echo $form->error($PolicyFamilyApplicant,'applicants_count');?>
        </div>
        <?php $this->renderPartial('applicant_list',array(
                        'applicant_list'=>0,
                        'applicant_count'=>1,
                        'PolicyFamilyDetails'=>$PolicyFamilyDetails,
                        'form'=>$form,
                        'enableAjaxValidation'=>false
        ));?>
        <div class="applicant_list">
        <?php //$this->renderPartial('applicant_list');?>
        </div>



    </div>


    <div id="footer" class="clearfix">

        <div id="fr" class="form-btn-orange">
        <span> 
        <?php echo CHtml::submitButton('Continue',array('class'=>'btnOrange','name'=>'continue')); ?></span>
        </div>
    </div>
</div>
 <?php $this->endWidget(); ?>
 <div id="errorBlank_applicant_list"></div>
 <div id="errorBox_applicant_list">
<img src="<?php echo Yii::app()->request->baseUrl;?>/images/infobig.jpg" width="51" height="51" alt="Information" style="float:left;margin: 0 20px 20px 0" />
<h2>For your information</h2>
<div class="applicant_check_list">
</div>
<div id="errorOK">
    <input type="image" value="ok" src="<?php echo Yii::app()->request->baseUrl;?>/images/error-ok.gif" onclick="closeError()" /></div> 
</div>

applicant_list.php

<?php //$loop_val=5;
for ($i=$applicant_list;$i<$applicant_count;$i++) {?>

 <div class="clearfix">
 <div id="applicant_<?php echo $i+1;?>" class="applicants">
 <h3><?php echo ($i==0)?'Main Applicant':'Applicant '.($i+1);?></h3>


 <?php
 $value=($i==0)?'Y':'N';
 $cActiveForm=new CActiveForm();
 $form=Yii::app()->session['form'];

 $newCActiveFormObj = new CActiveForm(array(
                                'enableClientValidation' =>true,
                                'clientOptions'=>array(
                                                        'validateOnSubmit'=>true,
                                                    ),
                                ));

   echo Yii::app()->session['form']->hiddenField($PolicyFamilyDetails,'['.($i+1).']main_applicant',array('value'=>$value));
  echo $form->hiddenField($PolicyFamilyDetails,'['.($i+1).']policy_id',array('value'=>Yii::app()->session['policy_id']));
  echo $form->hiddenField($PolicyFamilyDetails,'applicant_list[]',array('value'=>$i+1));

 ?>
 <div class="halfCol float">
              <div class="item">
                <label for="firstname" class="xsmall">First Name</label>
                <?php echo $form->textField($PolicyFamilyDetails,'['.($i+1).']first_name',array('class'=>'medium'/*,'id'=>'first_name_'.($i+1)*/)); ?>
                <?php echo $form->error($PolicyFamilyDetails, 'first_name',array('inputID'=>'PolicyFamilyDetails_'.($i+1).'_first_name'));?>
                </div>



            </div>

           <div class="halfCol floatRight">
                <div class="item">

                <label class="xsmall" for="lastname">Last Name</label>
                <?php echo $form->textField($PolicyFamilyDetails,'['.($i+1).']last_name',array('class'=>'medium')); ?>
                           <?php echo    $form->error($PolicyFamilyDetails,'last_name',array('inputID'=>'PolicyFamilyDetails_'.($i+1).'_last_name')); ?></div>
       </div>


        </div>





        </div>
 <?php } ?>   

0 个答案:

没有答案