我使用renderParial()函数呈现视图->订户->_form->layouts->main.php,问题是我无法使用它插入数据。Gii生成的CRUD运行良好,但当我呈现_form并希望在数据库中插入数据时,它不起作用。这是我的密码。main.php
<div class="col-lg-6" style="text-align:right;">
<span>Subscribe Newsletter</span>
<div style="float:right;margin-left:10px;">
<?php
$model=new Subscription();
$this->renderPartial("/subscription/_form",array('model'=>$model));?>
</div>和_form.php
<?php
/* @var $this SubscriptionController */
/* @var $model Subscription */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'subscription-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'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->textField($model,'email',array('size'=>30,'maxlength'=>30,'placeholder'=>'Email Address')); ?>
<?php echo $form->error($model,'email'); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Subscribe' : 'Save',array('class'=>'btn btn-xs btn-success subscribeBtn')); ?>
</div>
<?php $this->endWidget(); ?>
</div>发布于 2014-02-06 11:28:02
正如adamS和Michiel所评论的那样,如果您想要将表单或数据放入main.php布局文件中,则应该使用小部件。
要创建一个小部件,您需要执行以下操作:
1:在/protected/components/ dir中创建一个php文件,类似于SubscriptionWidget.php
2:在组件dir中创建dir views。
3:在您的.php中创建视图/protected/components/views/文件,类似于subscriptionWidget.php
4:将以下代码放入您的SubscriptionWidget.php文件:
<?php
class SubscriptionWidget extends CWidget
{
public function init()
{
}
public function run()
{
$model = new SubscriptionForm;
if(isset($_POST['SubscriptionForm']))
{
// proces the data
}
$this->render('subscriptionWidget', array('model'=>$model));
}
}
?>您的小部件完成了。现在只需在main.php布局文件中调用它,如下所示:
<!doctype html>
<html lang="en">
...
<?php $this->widget('SubscriptionWidget'); ?>
...
</html>另外,不要忘记将表单放在新创建的视图文件中。
希望这能有所帮助。
发布于 2014-02-06 10:50:40
再加一个斜杠
$this->renderPartial("//subscription/_form",array('model'=>$model));https://stackoverflow.com/questions/21600323
复制相似问题