Reputation: 573
I'm creating a simple form by Yii2 but instead of form field page, I only get the codes I added into view part.
the code for controller part:
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]);
}
}
}
the codes for model part:
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']
];
}
}
and the codes for view part is:
<?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(); ?>
but this is the output I get:
output photo
and this is the status of files and folders in sublime environment
Upvotes: 0
Views: 160
Reputation: 133410
You missing = in <?=
<?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(); ?>
You have also a wrong value in rules : required and not requiered try with the correct value
[['title','content','date_added'],'required'],
Upvotes: 1