How to troubleshoot form stuck with no errors?

Hi All,

I am really stuck, the form is not sending with no apparent errors. All the fields are green and fine, but the page reloads on itself.

I am pasting the view, model and controller code, maybe someone can show me the right direction for the error as I looked everywhere and just cannot pinpoint why this is happening.

When I try to submit the form I am logged in as an admin.

VIEW




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\helpers\ArrayHelper;

use frontend\models\categories;

use yii\models\user;

use yii\web\IdentityInterface;




/* @var $this yii\web\View */

/* @var $model frontend\models\Posts */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="posts-form">


    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'title')->textInput(['maxlength' => 160]) ?>


    <?= $form->field($model, 'posts')->textarea(['rows' => 6]) ?>


    <?= $form->field($model, 'keywords')->textInput(['maxlength' => 45]) ?>







    

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

    ArrayHelper::map(Categories::find()->all(),'name','name'),

    ['prompt'=>'Select a category']) ?>

   

    

    <?= $form->field($model, 'user_id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>

   

  


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


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


</div>



MODEL




<?php


namespace frontend\models;


use Yii;

use yii\behaviors\TimestampBehavior;

use yii\db\Expression;

/**

 * This is the model class for table "posts".

 *

 * @property integer $id

 * @property string $title

 * @property string $posts

 * @property string $keywords

 * @property string $created_at

 * @property string $updated_at

 * @property integer $categories_id

 * @property integer $user_id

 *

 * @property Categories $categories

 * @property User $user

 */

class Posts extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'posts';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['title', 'posts', 'categories_id', 'user_id'], 'required'],

            [['posts'], 'string'],

            [['created_at', 'updated_at'], 'safe'],

            [['categories_id', 'user_id'], 'integer'],

            [['title'], 'string', 'max' => 160],

            [['keywords'], 'string', 'max' => 45]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => Yii::t('app', 'ID'),

            'title' => Yii::t('app', 'Title'),

            'posts' => Yii::t('app', 'Posts'),

            'keywords' => Yii::t('app', 'Keywords'),

            'created_at' => Yii::t('app', 'Created At'),

            'updated_at' => Yii::t('app', 'Updated At'),

            'categories_id' => Yii::t('app', 'Categories ID'),

            'user_id' => Yii::t('app', 'User ID'),

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCategories()

    {

        return $this->hasOne(Categories::className(), ['id' => 'categories_id']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getUser()

    {

        return $this->hasOne(User::className(), ['id' => 'user_id']);

    }

    

    public function behaviors()

{

    return [

        [

            'class' => TimestampBehavior::className(),

            'createdAtAttribute' => 'created_at',

            'updatedAtAttribute' => 'updated_at',

            'value' => new Expression('NOW()'),

        ],

    ];

}

    

    

}



CONTROLLER




<?php


namespace frontend\controllers;


use Yii;

use frontend\models\Posts;

use frontend\models\PostsSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


/**

 * PostsController implements the CRUD actions for Posts model.

 */

class PostsController extends Controller

{

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['post'],

                ],

            ],

        ];

    }


    /**

     * Lists all Posts models.

     * @return mixed

     */

    public function actionIndex()

    {

        $searchModel = new PostsSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single Posts model.

     * @param integer $id

     * @return mixed

     */

    public function actionView($id)

    {

        return $this->render('view', [

            'model' => $this->findModel($id),

        ]);

    }


    /**

     * Creates a new Posts model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Posts();


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

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

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }


    /**

     * Updates an existing Posts model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @return mixed

     */

    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


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

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

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }

    }


    /**

     * Deletes an existing Posts model.

     * If deletion is successful, the browser will be redirected to the 'index' page.

     * @param integer $id

     * @return mixed

     */

    public function actionDelete($id)

    {

        $this->findModel($id)->delete();


        return $this->redirect(['index']);

    }


    /**

     * Finds the Posts model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param integer $id

     * @return Posts the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id)

    {

        if (($model = Posts::findOne($id)) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

    }

}




THANKS!

Ben

You are missing required field (in model rules) categories_id.

You are not showing categories_id control, so you can’t get errors from form.




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

    ArrayHelper::map(Categories::find()->all(),'name','name'),

    ['prompt'=>'Select a category']) ?>



Here I think that should be categories_id and not categories.

Thanks Fabrizio you were right, now it works, I did not notice my mistake.