我创建了两个模型:User和UserProfile,现在我希望在用户视图(_form)中的UserProfile的某些字段中显示,在用户表中创建操作我已经完成了这个:
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>
请告诉我我错过了什么!
答案 0 :(得分:2)
哦..您正在使用CHtml创建表单CHtml::beginForm()
并结束CHtml::endForm()
如果你这样做,你必须使用CHtml来生成表单元素,即_form中所有位置的CHtml::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();
它会解决你的问题。
答案 1 :(得分: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'];
答案 2 :(得分: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