SajadGD
SajadGD

Reputation: 31

How to show a form using Yii2 framwork?

I'm trying to make a simple form using yii2 framework.

enter image description here

This code is in 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 :

<?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:

<?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:

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

How can I fix this?

Upvotes: 0

Views: 279

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

Simply try echoing your field. USe <?= 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

Upvotes: 2

Daniel D&#237;az
Daniel D&#237;az

Reputation: 1

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

Edit, you can replace your actionNew, now use this:

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

<?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(); ?>

Upvotes: 0

Related Questions