yii 将从视图提交的值绑定到控制器

huwehgph  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(153)

我正在编写一个表单,以获取用户输入,然后将该信息传递给控制器,并在此基础上执行一个函数,此时我无法使用POST方法将数据传递给控制器,我得到了空变量。
因此,控制器函数可以正确显示视图表单,我可以在文本框中键入内容,按下提交按钮后,我会收到一条setFlash自定义消息,说明参数为空。我使用的模型类只有两个参数。
a)这是模型:

<?php
namespace app\models;
use Yii;
use yii\base\Model;

class SendmailForm extends Model
{
    public $template;
    public $emtransport;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['template', 'emtransport'], 'required'],
        ];
    }

}

B)这是一个视图:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;

$this->title = 'Send Mail';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-contact">
    <h1><?= Html::encode($this->title) ?></h1>

    <?php if (Yii::$app->session->hasFlash('sminfo')): ?>
        <div class="alert alert-success">
            <?= Yii::$app->session->getFlash('sminfo');?>
        </div>

    <?php else: ?>

        <p>
            SendMail Exercise. Please choose needed options bellow:
        </p>

        <div class="row">
            <div class="col-lg-5">

                <?php $form = ActiveForm::begin(['id' => 'sendmail-form']); ?>

                    <?= $form->field($model, 'template')->textInput(['autofocus' => true]) ?>

                    <?= $form->field($model, 'emtransport') ?>

                    <div class="form-group">
                        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'value'=>'one', 'name'=>'sendbtn']) ?>
                    </div>

                <?php ActiveForm::end(); ?>

            </div>
        </div>

    <?php endif; ?>
</div>

这就是控制器的功能:

public function actionSendmail(){
        $model = new SendmailForm();                        
        if ($model->load(Yii::$app->request->post())) {
            $template = Yii::$app->request->post('template');
            $emailTransport = Yii::$app->request->post("emtransport");
            if($emailTransport=="local"){
                for($i=0;$i<=2;$i++){
                    $xclient = 'client' . $i;
                    \app\models\User::findByUsername($xclient)->sendMail($template, 'Welcome To XYZ Services', ['accountInfo' => 'www.mysite.com']);
                }//end of for loop
                Yii::$app->session->setFlash("sminfo", "Emails sent successfully to the Clients");
                return $this->refresh();                
            }//end of second if loop
            else{
                Yii::$app->session->setFlash("sminfo", "Params could not be verified!. Contact Tech Support");
                return $this->refresh();                                
            }

        }//end of post if loop

        return $this->render('sendmail', [
            'model' => $model,
        ]);
    }

我们的想法是从视图中获取值,此时我获取的是空值-

sq1bmfud

sq1bmfud1#

下面分两部分:

$template = Yii::$app->request->post('template');
$emailTransport = Yii::$app->request->post("emtransport");

更改以下内容:

$template = $model->template;
$emailTransport = $model->emtransport;

编辑后

public function actionSendmail(){
        $model = new SendmailForm();                        
        if ($model->load(Yii::$app->request->post())) {
            $template = $model->template;
            $emailTransport = $model->emtransport;
            if($emailTransport=="local"){
                for($i=0;$i<=2;$i++){
                    $xclient = 'client' . $i;
                    \app\models\User::findByUsername($xclient)->sendMail($template, 'Welcome To XYZ Services', ['accountInfo' => 'www.mysite.com']);
                }//end of for loop
                Yii::$app->session->setFlash("sminfo", "Emails sent successfully to the Clients");
                return $this->refresh();                
            }//end of second if loop
            else{
                Yii::$app->session->setFlash("sminfo", "Params could not be verified!. Contact Tech Support");
                return $this->refresh();                                
            }

        }//end of post if loop

        return $this->render('sendmail', [
            'model' => $model,
        ]);
    }
yvt65v4c

yvt65v4c2#

我对控制器做了一些更改,现在它工作了,这些是:

//at the beginning of the Controller:
use app\models\SendmailForm;

//two lines changed in the function:
            $model->template = $_POST['SendmailForm']['template'];
            $model->emtransport = $_POST['SendmailForm']['emtransport'];

这就是我所需要的。最好的问候

相关问题