【问题标题】:Yii RenderPartial not workingYii RenderPartial 不工作
【发布时间】:2014-02-06 10:32:52
【问题描述】:

我正在使用 renderParial() 函数从views->layouts->main.php 渲染views->subscriber->_form 我遇到的问题是我无法使用它插入数据。 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>

【问题讨论】:

  • 这违背了整个 MVC 模式。您可以使用此表单更好地创建一个小部件,并将其包含在您的主布局和您的模型中。
  • @Michiel _form 也是小部件。那怎么用呢?
  • 同意@Michiel,你不应该把这种东西放在你的布局中。
  • @adamS 你能推荐一个替代答案吗?
  • 你的 $model 来自哪里,你在什么控制器/动作中声明它?创建一个小部件将允许您指定您尝试使用哪个模型(在小部件类中)保存表单数据。 yiiframework.com/doc/guide/1.1/en/basics.view#widget

标签: php yii renderpartial


【解决方案1】:

正如 adamS 和 Michiel 所评论的,如果你想在 main.php 布局文件中放置一个表单或数据,你应该使用一个小部件。

要创建小部件,您需要执行以下操作:

1:在 /protected/components/ 目录中创建一个 php 文件,类似于 SubscriptionWidget.php

2:在您的组件目录中创建一个目录views

3:在 /protected/components/views/ 中创建视图 .php 文件,类似于 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>

另外,不要忘记将表单放入新创建的视图文件中。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    尝试再添加一个斜线

    $this->renderPartial("//subscription/_form",array('model'=>$model));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多