dongsheng8158 2013-04-30 17:24
浏览 26
已采纳

Yii在“查看”页面中提交表单

I just started working with yii and I stumbled upon a problem. I have a table called "users" and a table called "messages". I basically want to have a page where I can view a user's details but also send him a message that would be saved in the message table.

I have a view called "user/view.php" which consists of:

<h1>View User #<?php echo $model->id; ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        'username',
        'first_name',
        'last_name',
    ),
)); ?>

<?php $message=new Message;
echo $this->renderPartial('_messagesend', array('model'=>$message)); ?>

the _messagesend form (created using gii) looks like:

<div class="form">

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

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

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

    //for the sake of simplicity lets just insert the id's manually for now
    <div class="row">
        <?php echo $form->labelEx($model,'idFrom'); ?>
        <?php echo $form->textField($model,'idFrom'); ?>
        <?php echo $form->error($model,'idFrom'); ?>
    </div>

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

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


    <div class="row buttons">
        <?php echo CHtml::submitButton('Send'); ?>
    </div>

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

</div><!-- form -->

I have a simple view in my UserController to display the details info:

public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id)
        ));
    }

And now I want to figure out how i can add the controller to save the message. After gii creation I get a code which I tried to use and modify a little bit:

public function actionMessagesend()
    {
        $model=new Message;

        if(isset($_POST['Message']))
        {
            $model->attributes=$_POST['Message'];
            if($model->save()){
                $this->redirect(array('admin'));
                }
        }
        $this->render('_messagesend',array('model'=>$model));
    }

I tried to add this controller function in the UserController.php but it doesn't seem to work, I tried to add the same function to MessageController.php but it also doesn't seem to work. I tried to remove all the code and only add a redirect to show if the controllercode actually hits but it doesn't redirect (i tried it both in usercontroller and messagecontroller). So I have a feeling my code isn't reached. You guys have any idea what I'm missing here?

Maybe a little extra question: Is there a better suggestion to do what I want to do?

Thanks a lot!

Best regards,

WtFudgE

  • 写回答

2条回答 默认 最新

  • douduan1953 2013-04-30 19:42
    关注

    Your message sending action isn't reached because by default the CActiveForm submits to the current page, so you will have to add the message processing to the view action, or you will need to set the action property for your CActiveForm.

    Here is the link to the property documentation: http://www.yiiframework.com/doc/api/1.1/CActiveForm#action-detail

    So the form widget in the view should be something like:

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'message-_messagesend-form',
        'enableAjaxValidation'=>false,
        'action' => array( '/message/messageSend' ), // change depending on your project
    )); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办