1
votes

I m new in yii2 framework, when I submit my Activeform data to save in database in display error

Not Found (#404).

This Activeform mention in different model view. My form is on product page and I want just give review of customer regarding this product and save in DB.

view file: detail.php:

    <?php
    use yii\helpers\Html;
    use yii\widgets\DetailView;
    use yii\widgets\ActiveForm;

    /* @var $this yii\web\View */
    /* @var $model app\models\Product */

    $name = $result->name;

         $price = $result->price;
         $offerrate = 8;
         $offer = ($price / 100) * $offerrate;
         $offerprice = number_format($price - $offer, 2);

         $text = $result->Description;
         $list = explode(",", $text);
    ?>

    <div id="page-content" class="home-page">
    <div class="container">
        <div class="row">
            <div class="col-lg-5">
                <center>
                <?= Html::img(Yii::$app->urlManagerBackend->BaseUrl.'/'.$result->image, ['alt'=>'myImage','width'=>'300','height'=>'300', 'class' => 'img-responsive']) ?>
                <br/>
            <?= Html::button('ADD TO CART', ['class' => 'btn btn-success', 'id'=>'addcart']) ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <?= Html::button('BUY NOW', ['class' => 'btn btn-danger', 'id'=>'buynow']) ?>
            </center>
            </div>
            <div class="col-lg-7">
            <?php echo "<h5 style='color:#009933'>".$name."</h5>"?><br/>
            <?php
                echo "<ul>";
                foreach ($list as $lists)
                {
                    echo "<li class='liststyle'>".$lists."</li>";
                }
                echo "</ul>";
            ?><br/>
            <?php echo "<h6 style='color:#009933'>MobileShop Offer Price Rs: ".$offerprice."</h6>" ?>
            <?= Html::button('Rate and Review product', ['class' => 'btn btn-default', 'id'=>'review', 'data-toggle' => 'collapse', 'data-target' => '#demo']) ?>
            <div id="demo" class="collapse">

     <?php $form = ActiveForm::begin(['id' => 'reviewForm', 'action' => Yii::$app->urlManager->createUrl(['TblFeedback/create'])]); ?>
                        <?= $form->field($feedback, 'cust_name')->textInput() ?>
                        <?= $form->field($feedback, 'feedback')->textArea(['rows' => '2', 'cols' => '10'])?>
                        <?= $form->field($feedback, 'rating')->dropDownlist(['1' => '*', '2' => '* *', '3' => '* * *', '4' => '* * * *', '5' => '* * * * *']) ?>    
                            <div class="form-group">
                                <?= Html::submitButton('Save', ['id' => 'savebtn', 'class' => 'btn btn-default']) ?>
                           </div>
            <?php ActiveForm::end(); ?>
    </div>

    </div>
    </div>
    </div>
    </div>

Controller file: TblFeedbackController.php

    <?php

    namespace frontend\controllers;

    use Yii;
    use app\models\TblFeedback;
    use frontend\models\Product;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;

    class TblFeedbackController extends \yii\web\Controller
    {
        public function behaviors()
        {
            return [
            'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
            'delete' => ['POST'],
            ],
            ],
            ];
        }

        public function actionIndex()
        {

            return $this->render('index');
        }


        public function actionCreate()
        {
            $feedback = new TblFeedback();

            if ($feedback->load(Yii::$app->request->post()) && $feedback->validate())
            {
                var_dump($feedback);
                die();
                $feedback->save();
                Yii::$app->session->setFlash('success', '<h5>Thanks for review!</h5>');
                return $this->redirect(['index']);
            }
            else {
                return $this->render('index',
                    ['feedback' => $feedback,
                 ]);
                Yii::$app->session->setFlash('error', 'Error Found!');
            }
        }
    }

model file: TblFeedback.php

    <?php

    namespace frontend\models;

    use Yii;

    /**
     * This is the model class for table "tbl_feedback".
     *
     * @property integer $id
     * @property integer $product_id
     * @property integer $cust_id
     * @property string $cust_name
     * @property integer $rating
     * @property string $feedback
     *
     * @property Product $product
     * @property User $cust
     */
    class TblFeedback extends \yii\db\ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'tbl_feedback';
        }

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['product_id', 'cust_id', 'cust_name', 'rating', 'feedback'], 'required'],
                [['product_id', 'cust_id', 'rating'], 'integer'],
                [['rating'], 'integer', 'min' => 1, 'max' => 5],
                [['cust_name'], 'string', 'max' => 200],
                [['feedback'], 'string', 'max' => 1000],
                [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'id']],
                [['cust_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['cust_id' => 'id']],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => 'ID',
                'product_id' => 'Product ID',
                'cust_id' => 'Cust ID',
                'cust_name' => 'Enter Name:',
                'rating' => 'Rating',
                'feedback' => 'Description',
            ];
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getProduct()
        {
            return $this->hasOne(Product::className(), ['id' => 'product_id']);
        }

        /**
         * @return \yii\db\ActiveQuery
         */
        public function getCust()
        {
            return $this->hasOne(User::className(), ['id' => 'cust_id']);
        }
    }
1

1 Answers

0
votes

The naming convention for controller action are based on - separator when a part is uppercase so you should use

 <?php $form = ActiveForm::begin(['id' => 'reviewForm', 
         'action' => Yii::$app->urlManager->createUrl(['Tbl-feedback/create'])]); ?>

the naming convention,for the controllers, it is to start with a lowercase character.

so you should use

   class tblFeedbackController extends \yii\web\Controller

and

      <?php $form = ActiveForm::begin(['id' => 'reviewForm', 
         'action' => Yii::$app->urlManager->createUrl(['tbl-feedback/create'])]); ?>

and last suggestion you could use urlHelper

use yii\helpers\Url;

      <?php $form = ActiveForm::begin(['id' => 'reviewForm', 
         'action' => Url::to(['tbl-feedback/create'])]); ?>