以单一形式验证来自不同模型的字段

时间:2014-03-21 11:44:44

标签: php mysql yii

我有表格 TblRegistration / _form.php这个

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'tbl-registration-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>true,
)); ?>

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


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

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

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

    <div class="row">
        <?php echo $form->labelEx($model,'language'); ?>
        <?php echo $form->textField($model,'language',array('size'=>50,'maxlength'=>50)); ?>
        <?php echo $form->error($model,'language'); ?>
    </div>
<div class="row">
        <?php echo $form->labelEx($model1,'email'); ?>
        <?php echo $form->textField($model1,'email'); ?>
        <?php echo $form->error($model1,'email'); ?>
    </div>
<div class="row">
        <?php echo $form->labelEx($model1,'password'); ?>
        <?php echo $form->textField($model1,'password'); ?>
        <?php echo $form->error($model1,'password'); ?>
    </div>




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

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

我的actioncreate create contains

public function actionCreate()
    {
        $model=new TblRegistration;
        $model1=new TblLogin();

        // Uncomment the following line if AJAX validation is needed
        //$this->performAjaxValidation($model1);

        if(isset($_POST['TblRegistration']) && isset($_POST['TblLogin']))
        {
            $model->attributes=$_POST['TblRegistration'];
                        $model1->attributes=$_POST['TblLogin'];
            $model->save();
            if($model->save())
            {

                    $model1->reg_id=$model->reg_id;

                    $model1->save();
            }
            if(($model->save() )&& ($model1->save()))
                $this->redirect(array('view','id'=>$model->reg_id));
        }

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

我的字段电子邮件和密码来自模型TblLogin。我希望在保存数据之前验证这些字段。我可以实现这个吗?

1 个答案:

答案 0 :(得分:0)

后:

$model->attributes=$_POST['TblRegistration'];
$model1->attributes=$_POST['TblLogin'];

添加:

if($model->validate() && $model1->validate()){
$model->save();
...