【问题标题】:binding values submitted from a View to a Controller绑定从视图提交到控制器的值
【发布时间】:2019-07-09 21:48:00
【问题描述】:

我正在编写一个表单来获取用户输入,然后将该信息传递给控制器​​并基于此执行一个函数,此时我无法使用 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,
        ]);
    }

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

【问题讨论】:

    标签: php yii yii2 yii2-basic-app


    【解决方案1】:

    以下两部分:

    $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,
            ]);
        }
    

    【讨论】:

      【解决方案2】:

      我对控制器进行了一些更改,现在它可以工作了,就是这样:

      //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'];            
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-23
        相关资源
        最近更新 更多