首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >操作创建不工作的Yii2前端

操作创建不工作的Yii2前端
EN

Stack Overflow用户
提问于 2017-06-06 07:15:00
回答 2查看 712关注 0票数 1

我想在我看来执行动作创建,actionCreate在现场控制器中的代码是正确的,但是它用这个url“http://localhost/advanced/frontend/web/index.php?Subsidize%5Bname%5D.”将我重定向到index.php知道在创建表“补贴”中的新项目后,我想重定向到view.php

在siteController.php中创建动作

代码语言:javascript
复制
public function actionView($id) {
    $model = Subsidize::findOne($id);
    if ($model === null) {
        throw new NotFoundHttpException;
    }

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

public function actionCreate() {
    $model = new Subsidize();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {

         return $this->redirect(['view', 'id' => $model->subsidize_id]);


    } else {

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

查看“submitButton”中的create.php代码

代码语言:javascript
复制
        <?= Html::submitButton($model->isNewRecord ? 'إرسال ' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

另外,siteController中的函数行为

代码语言:javascript
复制
 public function behaviors() {
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['create'],
            'rules' => [
                [
                    'actions' => ['login', 'error'],
                    'allow' => true,
                ],
                [
                    'actions' => ['create', 'view'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    //see captcha and error added here, this fixes the issue
                    'actions' => ['support', 'test', 'delete', 'update', 'create', 'view'],
                    'allow' => true,
                    'roles' => ['?', '@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index' => ['get'],
                'view' => ['get'],
                'create' => ['get', 'post'],
                'update' => ['get', 'put', 'post'],
                'delete' => ['post', 'delete'],
            ],
        ],
    ];
}

我的视图代码: create.php

代码语言:javascript
复制
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; $model = new app\models\Subsidize; ?> <section class="support">
<div class="container">       
    <form class="dialog-form row">
        <?php $form = ActiveForm::begin() ?>
        <div class="col-md-12">
            <div class="form-group">             


                <?php echo $form->field($model, 'name', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('name', ['placeholder' => "الإسم الكريم"])->label(false); ?>

            </div><!--End Form-group-->
        </div><!-- col -->
        <div class="col-md-12">
            <div class="form-group">
                 <?php echo $form->field($model, 'montant', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('montant', ['placeholder' => "المبلغ "])->label(false); ?>
            </div><!--End Form-group-->
        </div><!-- col -->

         <div class="col-md-12">
            <div class="form-group">
                 <?php echo $form->field($model, 'date', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('date', ['placeholder' => "تاريخ التذكير "])->label(false); ?>
            </div><!--End Form-group-->
        </div><!-- col -->

        <div class="col-md-12">
            <div class="form-group">
                 <?php echo $form->field($model, 'phone', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput()->input('phone', ['placeholder' => "رقم الجوال"])->label(false); ?>
            </div><!--End Form-group-->
        </div><!-- col -->



        <div class="col-md-12">
            <div class="form-group">

                    <?php echo $form->field($model, 'remarks', [
       'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control transparent']
 ])->textInput(['rows' =>6])->input('remarks', ['placeholder' => "ملاحظات"])->label(false); ?>
            </div><!-- form-group -->
        </div><!-- col -->
        <div class="col-md-12">
            <div class="form-group" style="text-align:center "> 


    <?= Html::submitButton($model->isNewRecord ? 'إرسال ' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>



            </div><!-- form-group -->
        </div><!-- col -->
         <?php ActiveForm::end(); ?>
    </form><!--End-->
</div><!-- container -->

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-06 07:18:30

忘记指定HTML的method属性了吗?

代码语言:javascript
复制
<form method="POST" ...>

看起来表单只是作为GET请求提交的。

票数 1
EN

Stack Overflow用户

发布于 2017-06-06 07:32:36

以前我的英语很抱歉。你能编辑你的文章并添加完整的ActiveForm::begin()代码吗?对于替代修复,您可以尝试删除对谓词行为的create操作,如下所示:

代码语言:javascript
复制
'verbs' => [
        'class' => VerbFilter::className(),
        'actions' => [
            'index' => ['get'],
            'view' => ['get'], 
            'update' => ['get', 'put', 'post'],
            'delete' => ['post', 'delete'],
        ],
    ],
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44383589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档