0

我创建了两个模型:用户和用户配置文件,现在我想在用户视图 (_form) 的用户配置文件的某些字段中显示,在用户表中创建操作时我已经这样做了:

  public function actionCreate() {



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

    $user_form= new TblUser;
    $profile_form = new TblUserProfile;

            if(isset($_POST['user_form'], $_POST['profile_form'])){



     // populate input data to $a and $b
    $user_form->attributes=$_POST['user_form'];
    $profile_form->attributes=$_POST['profile_form'];


           // validate BOTH $a and $b
          $valid=$user_form->validate();
         $valid=$profile_form->validate() && $valid;



        if($valid)
    {
        // use false parameter to disable validation
        $user_form->save(false);


         $profile_form->save(false);
            // ...redirect to another page


        }
    }

    $this->render('create', array(
        'user_form'=>$user_form,
        'profile_form'=>$profile_form,
    ));

}

然后 ,

这是我的 _form 用户表类

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

    <?php echo Chtml::errorSummary($user_form,$profile_form); ?>

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

    <div class="row">
        <?php echo $form->labelEx($user_form,'password'); ?>
        <?php echo $form->passwordField($user_form,'password',array('size'=>40,'maxlength'=>40)); ?>
        <?php echo $form->error($user_form,'password'); ?>
    </div>

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

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


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

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


        <!-- form -->

现在,我已经像这样在同一个视图中渲染了两个模型,

<?php echo $this->renderPartial('_form', 
    array('user_form'=>$user_form,'profile_form'=>$profile_form)); ?>

做完这一切后我面临的问题是:

 Undefined variable: form

C:\wamp\www\topicoll\protected\views\tblUser\_form.php(9)

01 
02 
03 <?php echo CHtml::beginForm(); ?>
04 
05 
06 
07     <p class="note">Fields with <span class="required">*</span> are required.</p>
08 
09     <?php echo CHtml::errorSummary($user_form,$profile_form); ?>
10 
11     <div class="row">
12         <?php //echo $form->labelEx($model,'id'); ?>
13         <?php echo $form->hiddenField($user_form,'id'); ?> 
14         <?php //echo $form->error($model,'id'); ?>
15     </div>
16 
17     <div class="row">
18         <?php echo $form->labelEx($user_form,'password'); ?>
19         <?php echo $form->passwordField($user_form,'password',array('size'=>40,'maxlength'=>40)); ?>
20         <?php echo $form->error($user_form,'password'); ?>
21     </div>

请告诉我我错过了什么!

4

3 回答 3

2

哦..您正在使用 CHtml 开始一个表格CHtml::beginForm() 并结束CHtml::endForm()

如果你这样做,你必须使用 CHtml 来生成表单元素,即在你的 _formCHtml::activeTextfield($model, 'field', array())CHtml::activeLabelEx($model, 'field') 所有地方

另一种方式是……

添加此代码而不是 CHtml::beginForm()。

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>false,
    'clientOptions'=>array(
    ),
));

并添加此代码代替 CHtml::endForm()

$this->endWidget();

它会解决你的问题。

于 2012-10-03T07:53:04.830 回答
1

在你的 _form.php 替换<?php echo CHtml::beginForm(); ?>

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'profile-form',
    'type'=>'horizontal',
    'enableAjaxValidation'=>false,
)); ?>

<?php echo CHtml::endForm(); ?>

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

并在您的控制器中更改代码块

if(isset($_POST['user_form'], $_POST['profile_form'])){



     // populate input data to $a and $b
    $user_form->attributes=$_POST['user_form'];
    $profile_form->attributes=$_POST['profile_form'];

if(isset($_POST['TblUserProfile'], $_POST['TblUser'])){



     // populate input data to $a and $b
    $user_form->attributes=$_POST['TblUser'];
    $profile_form->attributes=$_POST['TblUserProfile'];
于 2012-10-03T07:54:36.693 回答
0
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'profile-form',
    'type'=>'horizontal',
    'enableAjaxValidation'=>false,
)); ?>
and <?php echo CHtml::endForm(); ?> with

<?php $this->endWidget(); ?>
And in your controller change the code block

if(isset($_POST['user_form'], $_POST['profile_form'])){



     // populate input data to $a and $b
    $user_form->attributes=$_POST['user_form'];`enter code here`
    $profile_form->attributes=$_POST['profile_form'];
with
于 2013-05-06T11:28:48.613 回答