简体   繁体   English

如何使用Yii2框架显示表格?

[英]How to show a form using Yii2 framwork?

I'm trying to make a simple form using yii2 framework. 我正在尝试使用yii2框架制作一个简单的表格。

在此处输入图片说明

This code is in backend/controller/PostController.php : 这段代码在backend / controller / PostController.php中:

<?php
namespace backend\controllers;
use yii\web\Controller;
use Yii;
use backend\models\PostForm;
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]);
        }
    }
}
?>

This one is in backend/models/PostForm.php : 这是在backend / models / PostForm.php中:

<?php
namespace backend\models; 
use yii\base\Model;
class PostForm extends Model
{
public $title;
public $content;
public $date_add;
public function rules()
{
    return [
        [['title','content','date_add'], 'required '],
        ['date_add' , 'integer']
    ];
}
}
?>

and this is in backend/views/post/_form.php: 这是在backend / views / post / _form.php中:

<?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_add') ; ?>
<? Html::submitButton('submit'); ?>
<?php ActiveForm::end(); ?>

but when I type backend.projectname.loc/post/new in my browser the page is shown like this image: 但是当我在浏览器中键入backend.projectname.loc / post / new时,页面显示如下图所示:

uupload.ir/files/e6xf_screenshot_from_2017-02-08_19:32:15.png uupload.ir/files/e6xf_screenshot_from_2017-02-08_19:32:15.png

How can I fix this? 我怎样才能解决这个问题?

Simply try echoing your field. 只需尝试呼应您的领域即可。 USe <?= or <?php echo 使用<?= or <?php echo

<?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_add') ; ?>
<?= Html::submitButton('submit'); ?>
<? php ActiveForm::end(); ?>

you are using html helper for submit button so at the top of you view you should add 您正在使用html helper提交按钮,因此在视图顶部应添加

you have a lot of errors 你有很多错误

<? $form -> field(.....) ?>

• First of nothing... this code must be started with: •首先……此代码必须以以下内容开头:

$form -> field(...) ;

Must be: 一定是:

$form->field(...);

Tell me if that solves your problems, greetings. 告诉我是否能解决您的问题,问候。

Sorry for my bad english xD 对不起,我的英语不好xD

Edit, you can replace your actionNew, now use this: 编辑,您可以替换您的actionNew,现在使用它:

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

The same for _form.php _form.php相同

<?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_add'); ?>
<?= Html::submitButton('submit'); ?>
<?php ActiveForm::end(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM