簡體   English   中英

yii2中表單的字段顯示不正確

[英]Not proper field show for forms in yii2

我正在通過Yii2創建一個簡單的表單,但是我只獲得了添加到視圖部分的代碼,而不是表單字段頁面。
控制器部分的代碼:

namespace backend\controllers;

use yii\web\Controller;
use backend\models\PostForm;
use Yii;

class PostController extends Controller
{
    public function actionIndex(){

        return $this->render('index');
    }
    public function actionNew()
    {
        $model = new PostForm;
        if($model->load(Yii::$app->request->post()) && $model->validate())
        {
            return $this->render('_show', ['model'=>$model]);
        }
        else
        {
            return $this->render('_form', ['model'=>$model]);
        }
    }
}  

模型部分的代碼:

namespace backend\models;

use yii\base\Model;

/**
* 
*/
class PostForm extends Model
{
    public $title;
    public $content;
    public $date_added;

    public function rules()
    {   
        return [
            [['title','content','date_added'],'requiered'],
            ['date_added','integer']

        ];

    }

}  

並且視圖部分的代碼是:

<?php

use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>

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

<? $form->field($model, 'title'); ?>
<? $form->field($model, 'content'); ?>
<? $form->field($model, 'date_added'); ?>

<? Html::submitButton('register'); ?>
<?php ActiveForm::end(); ?>  

但這是我得到的輸出:
輸出照片

這是崇高環境中文件和文件夾的狀態

跟蹤文件和文件夾:

您在<?=缺少<?=

    <?php

          use yii\widgets\ActiveForm;
          use yii\helpers\Html;
    ?>

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

          <?= $form->field($model, 'title'); ?>
          <?= $form->field($model, 'content'); ?>
          <?= $form->field($model, 'date_added'); ?>

          <? Html::submitButton('register'); ?>
  <?php ActiveForm::end(); ?>  

您在規則中也有錯誤的值:必需且不需要,請嘗試使用正確的值

 [['title','content','date_added'],'required'],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM